Name: targone
Owner: Wire Swiss GmbH
Description: A Swift command line argument parser and automatic usage description generator inspired by Python Argparse
Forked from: marcoconti83/targone
Created: 2016-09-12 12:30:06.0
Updated: 2017-10-19 08:33:35.0
Pushed: 2018-05-04 08:55:36.0
Size: 81
Language: Swift
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Targone is a command line argument parser for Swift scripts, inspired by Python argparse. 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.
See also Morione, A Swift subprocess execution library intended to be used in Swift scripts, inspired by Python subprocess
.
Targone public API is designed keeping in mind 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 (Swift throw
), and one simplified version that doesn't require the fist label and that does not throw but prints an error and aborts executon instead. See OptionalArgument
for an example.
To extract a parsed value (see ParsingResult
), one could use a function that return nil
if the expected type does not match, or a function that prints an error and aborts execution if the type doesn't match.
Aborting execution does not sound very Swift-like; on the other hand, throwing a Swift-error would make even the most simple script full of try!
and in case of error, print some (at the moment of writing, with Swift 2.1) completely cryptic stack trace that has nothing to do with the error itself. As the goal of Targone is ease of use in a script, we decided not to use throw
. We are aware that this makes it impossible to test some cases in unit tests.
The API is documented in the code and tests.
A simple example Swift script usage can be found in the Example folder.
Prints an error when command line arguments are missing or not matching type
xamples/echo.swift
e: echo.swift [-h] [-n NUM<Int>] [--quotes] text
.swift: error: too few arguments
Automatically generates and prints help
xamples/echo.swift --help
: echo.swift [-h] [--quotes] [-n NUM<Int>] text
es some text on stdin
tional arguments:
the text to print
onal arguments:
lp, -h show this help message and exit
m, -n NUM<Int> how many times to print the text
otes enclose the text within quotes
Just add `import Targone
` to your script
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. You need to invoke Swift with the -F
argument, pointing to the folder where Targone is stored.
Targone can be downloaded locally with Carthage.
Just add
our `Cartfile`. After running
you would be able to run any swift file with
`-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](https://github.com/marcoconti83/targone/tree/master/Examples) included with this project.
Without Carthage
can download the framework binary from the GitHub [latest release](https://github.com/marcoconti83/targone/releases/latest)