Skip to content

Commit 0ce1c0e

Browse files
committed
[Group] Add command convinience methods
1 parent 333ea29 commit 0ce1c0e

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

Commander/Command.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public func command(closure:() -> ()) -> CommandType {
1818
}
1919
}
2020

21-
/// Create a command which takes the argument parser using a closure
21+
/// Create a command which takes one argument using a closure
2222
public func command<A : ArgumentConvertible>(closure:A -> ()) -> CommandType {
2323
return AnonymousCommand { parser in
2424
if let argument = A(parser: parser) {

Commander/Group.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Group : CommandType {
66
public init() {}
77

88
/// Add a named sub-command to the group
9-
public func addCommand(name:String, command:CommandType) {
9+
public func addCommand(name:String, _ command:CommandType) {
1010
commands[name] = command
1111
}
1212

@@ -25,4 +25,19 @@ extension Group {
2525
self.init()
2626
closure(self)
2727
}
28+
29+
/// Add a sub-group using a closure
30+
public func group(name:String, closure:Group -> ()) {
31+
addCommand(name, Group(closure: closure))
32+
}
33+
34+
/// Add a command using a closure
35+
public func command(name:String, closure:() -> ()) {
36+
addCommand(name, AnonymousCommand { parser in closure() })
37+
}
38+
39+
/// Add a command which takes one argument using a closure
40+
public func command<A : ArgumentConvertible>(name:String, closure:A -> ()) {
41+
addCommand(name, Commander.command(closure))
42+
}
2843
}

CommanderTests/GroupTests.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class GroupTests : XCTestCase {
77
var didRunHelpCommand = false
88

99
let group = Group()
10-
group.addCommand("help", command: command {
10+
group.addCommand("help", command {
1111
didRunHelpCommand = true
1212
})
1313

@@ -26,7 +26,7 @@ class GroupTests : XCTestCase {
2626
var didRunHelpCommand = false
2727

2828
let group = Group {
29-
$0.addCommand("help", command: command {
29+
$0.addCommand("help", command {
3030
didRunHelpCommand = true
3131
})
3232
}
@@ -37,4 +37,42 @@ class GroupTests : XCTestCase {
3737
group.run(["help"])
3838
XCTAssertTrue(didRunHelpCommand)
3939
}
40+
41+
func testSubGroup() {
42+
var didRun = false
43+
44+
Group {
45+
$0.group("group") {
46+
$0.command("test") {
47+
didRun = true
48+
}
49+
}
50+
}.run(["group", "test"])
51+
52+
XCTAssertTrue(didRun)
53+
}
54+
55+
func testSubCommand() {
56+
var didRun = false
57+
58+
Group {
59+
$0.command("test") {
60+
didRun = true
61+
}
62+
}.run(["test"])
63+
64+
XCTAssertTrue(didRun)
65+
}
66+
67+
func testSubCommandWithArgument() {
68+
var givenName:String? = nil
69+
70+
Group {
71+
$0.command("test") { (name:String) in
72+
givenName = name
73+
}
74+
}.run(["test", "kyle"])
75+
76+
XCTAssertEqual(givenName, "kyle")
77+
}
4078
}

0 commit comments

Comments
 (0)