Skip to content

Commit 6c93284

Browse files
committed
README updates
1 parent 1d6ae49 commit 6c93284

File tree

1 file changed

+73
-11
lines changed

1 file changed

+73
-11
lines changed

README.md

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ main.run()
2323

2424
##### Type-safe argument handling
2525

26+
The closure passed to the command function takes any arguments that
27+
conform to `ArgumentConvertible`, Commander will automatically convert the
28+
arguments to these types. If they can't be converted the user will receive a
29+
nice error message informing them that their argument doesn't match the
30+
expected type.
31+
32+
`String`, `Int`, `Double`, and `Float` are extended to conform to
33+
`ArgumentConvertible`, you can easily extend any other class or structure
34+
so you can use it as an argument to your command.
35+
2636
```swift
2737
command { (hostname:String, port:Int) in
2838
print("Connecting to \(hostname) on port \(port)...")
@@ -48,19 +58,30 @@ Group {
4858
Usage:
4959

5060
```shell
51-
$ tool login Kyle
61+
$ auth
62+
Usage:
63+
64+
$ auth COMMAND
65+
66+
Commands:
67+
68+
+ login
69+
+ logout
70+
71+
$ auth login Kyle
5272
Hello Kyle
53-
$ tool logout
73+
$ auth logout
5474
Goodbye.
5575
```
5676

5777
#### Describing arguments
5878

59-
You can describe arguments and options for a command to auto-generate help.
79+
You can describe arguments and options for a command to auto-generate help,
80+
this is done by passing in descriptors of these arguments.
6081

6182
For example, to describe a command which takes two options, `--name` and
62-
`--count` where the default value for name is "world" and the default value for
63-
count is 1.
83+
`--count` where the default value for name is `world` and the default value for
84+
count is `1`.
6485

6586
```swift
6687
command(
@@ -74,31 +95,38 @@ command(
7495
```
7596

7697
```shell
77-
./hello --help
98+
$ hello --help
7899
Usage:
79100

80-
$ ./hello
101+
$ hello
81102

82103
Options:
83104
--name
84105
--count - The number of times to print.
85106

86-
./hello
107+
$ hello
87108
Hello world
88109

89-
./hello --name Kyle
110+
$ hello --name Kyle
90111
Hello Kyle
91112

92-
./hello --name Kyle --count 4
113+
$ hello --name Kyle --count 4
93114
Hello Kyle
94115
Hello Kyle
95116
Hello Kyle
96117
Hello Kyle
97118
```
98119

120+
##### Types of descriptors
121+
122+
- Argument - A positional argument.
123+
- Option - An optional option with a value.
124+
- Flag - A boolean, on/off flag.
125+
99126
#### Using the argument parser
100127

101-
You can get hold of the argument parser to do custom argument handling
128+
**NOTE**: *`ArgumentParser` itself is `ArgumentConvertible` so you can also
129+
get hold of the raw argument parser to perform any custom parsing.*
102130

103131
```swift
104132
command { (name:String, parser:ArgumentParser) in
@@ -149,6 +177,40 @@ let main = command {
149177
main.run()
150178
```
151179

180+
### Architecture
181+
182+
##### `CommandType`
183+
184+
`CommandType` is the core protocol behind commands, it's an object or
185+
structure that has a `run` method which takes an `ArgumentParser`.
186+
187+
Both the `command` functions and `Group` return a command that conforms to
188+
`CommandType` which can easily be interchanged.
189+
190+
```swift
191+
protocol CommandType {
192+
func run(parser:ArgumentParser) throws
193+
}
194+
```
195+
196+
##### `ArgumentConvertible`
197+
198+
The convenience `command` function takes a closure for your command that
199+
takes arguments which conform to the `ArgumentConvertible` protocol. This
200+
allows Commander to easily convert arguments to the types you would like
201+
to use for your command.
202+
203+
```swift
204+
protocol ArgumentConvertible {
205+
init(parser: ArgumentParser) throws
206+
}
207+
```
208+
209+
##### `ArgumentParser`
210+
211+
The `ArgumentParser` is an object that allowing you to pull out options,
212+
flags and positional arguments.
213+
152214
## License
153215

154216
Commander is available under the BSD license. See the [LICENSE file](LICENSE)

0 commit comments

Comments
 (0)