Skip to content

Commit 03bcadc

Browse files
committed
Support up-to three command arguments
1 parent b2c5551 commit 03bcadc

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

Commander/Command.swift

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
/// A simple CommandType using a closure
22
struct 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
1519
public 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
}

Commander/Group.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,17 @@ extension Group {
7575
}
7676

7777
/// Add a command which takes one argument using a closure
78-
public func command<A : ArgumentConvertible>(name:String, closure:A -> ()) {
78+
public func command<A:ArgumentConvertible>(name:String, closure:A -> ()) {
79+
addCommand(name, Commander.command(closure))
80+
}
81+
82+
/// Add a command which takes two argument using a closure
83+
public func command<A:ArgumentConvertible, B:ArgumentConvertible>(name:String, closure:(A,B) -> ()) {
84+
addCommand(name, Commander.command(closure))
85+
}
86+
87+
/// Add a command which takes three argument using a closure
88+
public func command<A:ArgumentConvertible, B:ArgumentConvertible, C:ArgumentConvertible>(name:String, closure:(A,B,C) -> ()) {
7989
addCommand(name, Commander.command(closure))
8090
}
8191
}

0 commit comments

Comments
 (0)