Skip to content

Commit b82c0ed

Browse files
committed
Add up to 5 argument support
1 parent 392eb16 commit b82c0ed

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

Commander/ArgumentDescription.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,34 @@ public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescri
240240
closure(try descriptorA.parse(parser), try descriptorB.parse(parser), try descriptorC.parse(parser))
241241
}
242242
}
243+
244+
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor, D:ArgumentDescriptor>(a:A, _ b:B, _ c:C, _ d:D, closure:((A.ValueType, B.ValueType, C.ValueType, D.ValueType) -> ())) -> CommandType {
245+
return AnonymousCommand { parser in
246+
if parser.hasOption("help") {
247+
throw Help([
248+
BoxedArgumentDescriptor(value: a),
249+
BoxedArgumentDescriptor(value: b),
250+
BoxedArgumentDescriptor(value: c),
251+
BoxedArgumentDescriptor(value: d),
252+
])
253+
}
254+
255+
closure(try a.parse(parser), try b.parse(parser), try c.parse(parser), try d.parse(parser))
256+
}
257+
}
258+
259+
public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor, D:ArgumentDescriptor, E:ArgumentDescriptor>(a:A, _ b:B, _ c:C, _ d:D, _ e:E, closure:((A.ValueType, B.ValueType, C.ValueType, D.ValueType, E.ValueType) -> ())) -> CommandType {
260+
return AnonymousCommand { parser in
261+
if parser.hasOption("help") {
262+
throw Help([
263+
BoxedArgumentDescriptor(value: a),
264+
BoxedArgumentDescriptor(value: b),
265+
BoxedArgumentDescriptor(value: c),
266+
BoxedArgumentDescriptor(value: d),
267+
BoxedArgumentDescriptor(value: e),
268+
])
269+
}
270+
271+
closure(try a.parse(parser), try b.parse(parser), try c.parse(parser), try d.parse(parser), try e.parse(parser))
272+
}
273+
}

Commander/Command.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,36 @@ public func command(closure:() -> ()) -> CommandType {
2323
}
2424

2525
/// Create a command which takes one argument using a closure
26-
public func command<A : ArgumentConvertible>(closure:A -> ()) -> CommandType {
26+
public func command<A:ArgumentConvertible>(closure:A -> ()) -> CommandType {
2727
return AnonymousCommand { parser in
2828
closure(try A(parser: parser))
2929
}
3030
}
3131

3232
/// Create a command which takes two arguments using a closure
33-
public func command<A : ArgumentConvertible, B : ArgumentConvertible>(closure:(A, B) -> ()) -> CommandType {
33+
public func command<A:ArgumentConvertible, B:ArgumentConvertible>(closure:(A, B) -> ()) -> CommandType {
3434
return AnonymousCommand { parser in
3535
closure(try A(parser: parser), try B(parser: parser))
3636
}
3737
}
3838

39-
4039
/// Create a command which takes three arguments using a closure
41-
public func command<A : ArgumentConvertible, B : ArgumentConvertible, C : ArgumentConvertible>(closure:(A, B, C) -> ()) -> CommandType {
40+
public func command<A:ArgumentConvertible, B:ArgumentConvertible, C:ArgumentConvertible>(closure:(A, B, C) -> ()) -> CommandType {
4241
return AnonymousCommand { parser in
4342
closure(try A(parser: parser), try B(parser: parser), try C(parser: parser))
4443
}
4544
}
45+
46+
/// Create a command which takes four arguments using a closure
47+
public func command<A:ArgumentConvertible, B:ArgumentConvertible, C:ArgumentConvertible, D:ArgumentConvertible>(closure:(A, B, C, D) -> ()) -> CommandType {
48+
return AnonymousCommand { parser in
49+
closure(try A(parser: parser), try B(parser: parser), try C(parser: parser), try D(parser: parser))
50+
}
51+
}
52+
53+
/// Create a command which takes five arguments using a closure
54+
public func command<A:ArgumentConvertible, B:ArgumentConvertible, C:ArgumentConvertible, D:ArgumentConvertible, E:ArgumentConvertible>(closure:(A, B, C, D, E) -> ()) -> CommandType {
55+
return AnonymousCommand { parser in
56+
closure(try A(parser: parser), try B(parser: parser), try C(parser: parser), try D(parser: parser), try E(parser: parser))
57+
}
58+
}

Commander/Group.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ extension Group {
7676
addCommand(name, AnonymousCommand { parser in closure() })
7777
}
7878

79+
// MARK: Argument Commands
80+
7981
/// Add a command which takes one argument using a closure
8082
public func command<A:ArgumentConvertible>(name:String, closure:A -> ()) {
8183
addCommand(name, Commander.command(closure))
@@ -90,4 +92,41 @@ extension Group {
9092
public func command<A:ArgumentConvertible, B:ArgumentConvertible, C:ArgumentConvertible>(name:String, closure:(A,B,C) -> ()) {
9193
addCommand(name, Commander.command(closure))
9294
}
95+
96+
/// Add a command which takes four argument using a closure
97+
public func command<A:ArgumentConvertible, B:ArgumentConvertible, C:ArgumentConvertible, D:ArgumentConvertible>(name:String, closure:(A,B,C,D) -> ()) {
98+
addCommand(name, Commander.command(closure))
99+
}
100+
101+
/// Add a command which takes five argument using a closure
102+
public func command<A:ArgumentConvertible, B:ArgumentConvertible, C:ArgumentConvertible, D:ArgumentConvertible, E:ArgumentConvertible>(name:String, closure:(A,B,C,D,E) -> ()) {
103+
addCommand(name, Commander.command(closure))
104+
}
105+
106+
// MARK: Argument Description Commands
107+
108+
/// Add a command which takes one argument using a closure
109+
public func command<A:ArgumentDescriptor>(name:String, a:A, closure:(A.ValueType) -> ()) {
110+
addCommand(name, Commander.command(a, closure: closure))
111+
}
112+
113+
/// 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) -> ()) {
115+
addCommand(name, Commander.command(a, b, closure: closure))
116+
}
117+
118+
/// 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) -> ()) {
120+
addCommand(name, Commander.command(a, b, c, closure: closure))
121+
}
122+
123+
/// 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) -> ()) {
125+
addCommand(name, Commander.command(a, b, c, d, closure: closure))
126+
}
127+
128+
/// 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) -> ()) {
130+
addCommand(name, Commander.command(a, b, c, d, e, closure: closure))
131+
}
93132
}

0 commit comments

Comments
 (0)