File tree Expand file tree Collapse file tree 3 files changed +57
-4
lines changed
Expand file tree Collapse file tree 3 files changed +57
-4
lines changed Original file line number Diff line number Diff 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
2222public func command< A : ArgumentConvertible > ( closure: A -> ( ) ) -> CommandType {
2323 return AnonymousCommand { parser in
2424 if let argument = A ( parser: parser) {
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments