11/// A simple CommandType using a closure
22struct AnonymousCommand : CommandType {
3- var closure : ArgumentParser -> ( )
3+ var closure : ArgumentParser throws -> ( )
44
5- init ( _ closure: ( ArgumentParser -> ( ) ) ) {
5+ init ( _ closure: ( ArgumentParser throws -> ( ) ) ) {
66 self . closure = closure
77 }
88
9- func run( parser: ArgumentParser ) {
10- closure ( parser)
9+ func run( parser: ArgumentParser ) throws {
10+ try closure ( parser)
1111 }
1212}
1313
14+ enum CommandError : ErrorType {
15+ case InvalidArgument
16+ }
17+
1418/// Create a command using a closure
1519public func command( closure: ( ) -> ( ) ) -> CommandType {
1620 return AnonymousCommand { parser in
@@ -23,6 +27,36 @@ public func command<A : ArgumentConvertible>(closure:A -> ()) -> CommandType {
2327 return AnonymousCommand { parser in
2428 if let argument = A ( parser: parser) {
2529 closure ( argument)
30+ } else {
31+ throw CommandError . InvalidArgument
32+ }
33+ }
34+ }
35+
36+ /// Create a command which takes two arguments using a closure
37+ public func command< A : ArgumentConvertible , B : ArgumentConvertible > ( closure: ( A , B ) -> ( ) ) -> CommandType {
38+ return AnonymousCommand { parser in
39+ if let argumentA = A ( parser: parser) ,
40+ argumentB = B ( parser: parser)
41+ {
42+ closure ( argumentA, argumentB)
43+ } else {
44+ throw CommandError . InvalidArgument
45+ }
46+ }
47+ }
48+
49+
50+ /// Create a command which takes three arguments using a closure
51+ public func command< A : ArgumentConvertible , B : ArgumentConvertible , C : ArgumentConvertible > ( closure: ( A , B , C ) -> ( ) ) -> CommandType {
52+ return AnonymousCommand { parser in
53+ if let argumentA = A ( parser: parser) ,
54+ argumentB = B ( parser: parser) ,
55+ argumentC = C ( parser: parser)
56+ {
57+ closure ( argumentA, argumentB, argumentC)
58+ } else {
59+ throw CommandError . InvalidArgument
2660 }
2761 }
2862}
0 commit comments