-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf7afc3
commit c0d2002
Showing
21 changed files
with
3,313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
Targone.xcodeproj/xcuserdata | ||
Carthage | ||
Targone.xcodeproj/project.xcworkspace/xcuserdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/swift -F Carthage/Build/Mac | ||
// Copyright (c) 2015 Marco Conti | ||
// | ||
// | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
|
||
import Targone | ||
|
||
// Define parser | ||
var parser = ArgumentParser(summary: "Echoes some text on stdin") | ||
|
||
// Method 1: add and retrieve final value by argument | ||
let repetitionsArg = OptionalArgument<Int>("num", shortLabel: "n", defaultValue: 1) | ||
parser.addArgument(repetitionsArg) | ||
let textArg = PositionalArgument<String>("text") | ||
parser.addArgument(textArg) | ||
|
||
// Method 2: add and retrieve final value by label | ||
parser.addArgument(FlagArgument("quotes")) | ||
|
||
// Parse | ||
let args = parser.parse() | ||
|
||
|
||
// Method 1: | ||
let repetitions = args.value(repetitionsArg)! | ||
let text = args.value(textArg)! | ||
|
||
// Method 2: (requires explicit casting) | ||
let quotes = args.labelsToValues["--quotes"] as! Bool | ||
|
||
for i in 0..<repetitions { | ||
print(quotes ? "\"\(text)\"" : text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Targone | ||
--------------------- | ||
|
||
Targone is a command line argument parser for Swift scripts, inspired by [Python argparse](https://docs.python.org/2/library/argparse.html). It allows script authors to easily define what kind of arguments are expected, whether they are optional, positional or flags, and automatically generate usage description. | ||
|
||
Its public API is designed with ease of use within a script. To achieve this, most classes have two ways of being initialized, one with explicit argument names and error reporting (throws), and a simplified version that doesn't require the fist label and that does not throw but asserts instead. | ||
|
||
The code and the tests are documented. | ||
|
||
A simple example Swift script usage can be found in the Example folder. | ||
|
||
## How to use Targone in your script | ||
|
||
```import Targone``` | ||
|
||
In order to be able to import it, you need to have the `Targone.framework` in the Swift search path. You can achieve this by compiling it yourself or downloading a binary version from a release, and then invoking Swift with the `-F` argument, pointing to the folder where Targone is stored. | ||
|
||
### Carthage integration | ||
Targone can be downloaded locally with [Carthage](https://github.com/Carthage/Carthage). | ||
|
||
Just add | ||
|
||
```github "marcoconti83/targone"``` | ||
|
||
to your `Cartfile`. After running | ||
|
||
```carthage update``` | ||
|
||
you would be able to run any swift file with | ||
|
||
```swift -F Carthage/Build/Mac``` | ||
|
||
The `-F` flag can also be included in the [shebang](https://en.wikipedia.org/wiki/Shebang_%28Unix%29) line of the script, so that you can just invoke the script directly (e.g. ```$> do.swift```). This is the approach used in the examples included with this project. | ||
|
||
### Without Carthage | ||
You can download the framework binary from the GitHub latest release |
Oops, something went wrong.