https://github.com/hartbit/yaap
Yet Another (Swift) Argument Parser
https://github.com/hartbit/yaap
argparse argparser argument-parser argument-parsers argument-parsing cli linux macos shorthand-syntax swift swift-package-manager swiftp yaap yet-another
Last synced: 3 months ago
JSON representation
Yet Another (Swift) Argument Parser
- Host: GitHub
- URL: https://github.com/hartbit/yaap
- Owner: hartbit
- License: mit
- Created: 2018-02-16T18:35:46.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-02-04T06:29:54.000Z (almost 6 years ago)
- Last Synced: 2025-10-21T10:53:48.575Z (3 months ago)
- Topics: argparse, argparser, argument-parser, argument-parsers, argument-parsing, cli, linux, macos, shorthand-syntax, swift, swift-package-manager, swiftp, yaap, yet-another
- Language: Swift
- Homepage:
- Size: 105 KB
- Stars: 117
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Yaap [](#installation) [](https://developer.apple.com/swift/) [](LICENSE)
Yaap is Yet Another (Swift) Argument Parser that represents executable commands as types, and arguments as properties of those types. It supports:
* Strongly-typed argument and option parsing
* Automatic help and usage message generation
* Multiple command routing
* Smart error messages with suggestion on typos
Here's a self-contained example of a `rand` executable that generates random numbers in a configurable interval to standard output, with everything from `--help` documentation, usage generation, and `--version` printing.
```swift
class RandomCommand: Command {
let name = "rand"
let documentation = "Generates a random number that lies in an interval."
@Argument(documentation: "Exclusive maximum value")
var maximum: Int
@Option(shorthand: "m", documentation: "Inclusive minimum value")
var minimum: Int = 0
let help = Help()
let version = Version("0.1.0")
func run(outputStream: inout TextOutputStream, errorStream: inout TextOutputStream) throws {
guard maximum > minimum else {
throw InvalidIntervalError(minimum: minimum, maximum: maximum)
}
outputStream.write(Int.random(in: minimum..
ARGUMENTS:
maximum Exclusive maximum value
OPTIONS:
--help, -h Display available options [default: false]
--minimum, -m Inclusive minimum value [default: 0]
```
### Version
Yaap also comes with a built-in `Version` property that allows commands to respond to a `--version/-v` argument by printing their version number to standard output. The property can be customized to respond to a different argument name and optional shorthand syntax:
```swift
class MyCommand: Command {
let name = "program"
let version = Version("4.2", name: "ver", shorthand: nil)
func run(outputStream: inout TextOutputStream, errorStream: inout TextOutputStream) throws {
// ...
}
}
```
```
$ program --ver
4.2
```
## Thanks
I'd like to thank [SwiftCLI](https://github.com/jakeheis/SwiftCLI) for being a major influence in designing Yaap.