An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Yaap [![version 1.0.0](https://img.shields.io/badge/version-1.0.0-brightgreen)](#installation) [![swift 5.1](https://img.shields.io/badge/swift-5.1-orange)](https://developer.apple.com/swift/) [![license MIT](https://img.shields.io/badge/license-MIT-blue)](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.