Skip to content

Commit 12d1c1a

Browse files
committed
Add examples
1 parent ed112f2 commit 12d1c1a

File tree

7 files changed

+100
-12
lines changed

7 files changed

+100
-12
lines changed

Commander/Group.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,27 @@ extension Group {
106106
// MARK: Argument Description Commands
107107

108108
/// Add a command which takes one argument using a closure
109-
public func command<A:ArgumentDescriptor>(name:String, a:A, closure:(A.ValueType) -> ()) {
109+
public func command<A:ArgumentDescriptor>(name:String, _ a:A, closure:(A.ValueType) -> ()) {
110110
addCommand(name, Commander.command(a, closure: closure))
111111
}
112112

113113
/// Add a command which takes two argument using a closure
114-
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor>(name:String, a:A, b:B, closure:(A.ValueType,B.ValueType) -> ()) {
114+
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor>(name:String, _ a:A, _ b:B, closure:(A.ValueType,B.ValueType) -> ()) {
115115
addCommand(name, Commander.command(a, b, closure: closure))
116116
}
117117

118118
/// Add a command which takes three argument using a closure
119-
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor>(name:String, a:A, b:B, c:C, closure:(A.ValueType,B.ValueType,C.ValueType) -> ()) {
119+
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor>(name:String, _ a:A, _ b:B, _ c:C, closure:(A.ValueType,B.ValueType,C.ValueType) -> ()) {
120120
addCommand(name, Commander.command(a, b, c, closure: closure))
121121
}
122122

123123
/// Add a command which takes four argument using a closure
124-
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor, D:ArgumentDescriptor>(name:String, a:A, b:B, c:C, d:D, closure:(A.ValueType,B.ValueType,C.ValueType,D.ValueType) -> ()) {
124+
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor, D:ArgumentDescriptor>(name:String, _ a:A, _ b:B, _ c:C, _ d:D, closure:(A.ValueType,B.ValueType,C.ValueType,D.ValueType) -> ()) {
125125
addCommand(name, Commander.command(a, b, c, d, closure: closure))
126126
}
127127

128128
/// Add a command which takes five argument using a closure
129-
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor, D:ArgumentDescriptor, E:ArgumentDescriptor>(name:String, a:A, b:B, c:C, d:D, e:E, closure:(A.ValueType,B.ValueType,C.ValueType,D.ValueType,E.ValueType) -> ()) {
129+
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor, D:ArgumentDescriptor, E:ArgumentDescriptor>(name:String, _ a:A, _ b:B, _ c:C, _ d:D, _ e:E, closure:(A.ValueType,B.ValueType,C.ValueType,D.ValueType,E.ValueType) -> ()) {
130130
addCommand(name, Commander.command(a, b, c, d, e, closure: closure))
131131
}
132132
}

CommanderTests/CommandTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,41 @@ class CommandTests : XCTestCase {
2424

2525
XCTAssertEqual(ranName, "Kyle")
2626
}
27+
28+
// MARK: Described Arguments
29+
30+
func testCommandWithDescribedArgument() {
31+
var givenName = ""
32+
33+
let c = command(
34+
Argument("name")
35+
) { name in
36+
givenName = name
37+
}
38+
39+
try! c.run(["Kyle"])
40+
XCTAssertEqual(givenName, "Kyle")
41+
}
42+
//
43+
// func testCommandWithDescribedArgumentShowsUsage() {
44+
// let c = command(
45+
// Argument("name")
46+
// ) { name in
47+
// }
48+
//
49+
// try! c.run(["Kyle"])
50+
// }
51+
//
52+
// func testCommandWithDescribedArgumentHelp() {
53+
// var givenName = ""
54+
//
55+
// let c = command(
56+
// Argument("name", `default`: "World")
57+
// ) { name in
58+
// givenName = name
59+
// }
60+
//
61+
// try! c.run(["--help"])
62+
// XCTAssertEqual(givenName, "Kyle")
63+
// }
2764
}

Examples/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build:
2+
pod install --no-repo-update

Examples/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Commander Examples
2+
3+
To use the examples, first run `make` with CocoaPods and CocoaPods-Rome
4+
installed.
5+
6+
```bash
7+
$ make
8+
$ ./hello.swift
9+
$ ./pod.swift
10+
```
11+

Examples/hello.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/swift -FRome
2+
3+
import Commander
4+
5+
command(
6+
Option("name", "world"),
7+
Option("count", 1, description: "The number of times to print.")
8+
) { name, count in
9+
for _ in 0..<count {
10+
print("Hello \(name)")
11+
}
12+
}.run()
13+

Examples/pod.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/swift -FRome
2+
3+
import Commander
4+
5+
Group {
6+
$0.command("install") {
7+
print("Installing Pods")
8+
}
9+
10+
$0.command("upgrade") { (name:String) in
11+
print("Updating \(name)")
12+
}
13+
14+
$0.command("search",
15+
Flag("web", description: "Searches on cocoapods.org"),
16+
Argument<String>("query")
17+
) { web, query in
18+
if web {
19+
print("Searching for \(query) on the web.")
20+
} else {
21+
print("Locally searching for \(query).")
22+
}
23+
}
24+
}.run()
25+

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,30 @@ count is 1.
6666
command(
6767
Option("name", "world"),
6868
Option("count", 1, description: "The number of times to print.")
69-
) { name, count
70-
for _ in (0..<count) {
69+
) { name, count in
70+
for _ in 0..<count {
7171
print("Hello \(name)")
7272
}
7373
}
7474
```
7575

7676
```shell
77-
./command --help
77+
./hello --help
7878
Usage:
7979

80-
$ ./command
80+
$ ./hello
8181

8282
Options:
8383
--name
8484
--count - The number of times to print.
8585

86-
./command
86+
./hello
8787
Hello world
8888

89-
./command --name Kyle
89+
./hello --name Kyle
9090
Hello Kyle
9191

92-
./command --name Kyle --count 4
92+
./hello --name Kyle --count 4
9393
Hello Kyle
9494
Hello Kyle
9595
Hello Kyle

0 commit comments

Comments
 (0)