Ecosyste.ms: Awesome

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

https://github.com/kylef/Commander

Compose beautiful command line interfaces in Swift
https://github.com/kylef/Commander

Last synced: about 2 months ago
JSON representation

Compose beautiful command line interfaces in Swift

Lists

README

        

Commander

# Commander

[![Build Status](https://img.shields.io/travis/kylef/Commander/master.svg?style=flat)](https://travis-ci.org/kylef/Commander)

Commander is a small Swift framework allowing you to craft beautiful command
line interfaces in a composable way.

## Usage

##### Simple Hello World

```swift
import Commander

let main = command { (filename:String) in
print("Reading file \(filename)...")
}

main.run()
```

##### Type-safe argument handling

The closure passed to the command function takes any arguments that
conform to `ArgumentConvertible`, Commander will automatically convert the
arguments to these types. If they can't be converted the user will receive a
nice error message informing them that their argument doesn't match the
expected type.

`String`, `Int`, `Double`, and `Float` are extended to conform to
`ArgumentConvertible`, you can easily extend any other class or structure
so you can use it as an argument to your command.

```swift
command { (hostname:String, port:Int) in
print("Connecting to \(hostname) on port \(port)...")
}
```

##### Grouping commands

You can group a collection of commands together.

```swift
Group {
$0.command("login") { (name:String) in
print("Hello \(name)")
}

$0.command("logout") {
print("Goodbye.")
}
}
```

Usage:

```shell
$ auth
Usage:

$ auth COMMAND

Commands:

+ login
+ logout

$ auth login Kyle
Hello Kyle
$ auth logout
Goodbye.
```

#### Describing arguments

You can describe positional arguments and options for a command to auto-generate help.
This is done by passing in descriptors of these arguments.

For example, for fixed positional arguments with descriptions, you can use:

```swift
command(
Argument("name", description: "Your name"),
Argument("surname", description: "Your surname"),
Argument("count", description: "Number of times to print")
) { name, surname, count in
for _ in 0..