Skip to content

Commit 4c519da

Browse files
committed
Make commands failable
1 parent acc869f commit 4c519da

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

Commander.playground/Contents.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ let group = Group {
1010
}
1111
}
1212

13-
group.run([])
14-
group.run(["login", "kyle"])
15-
group.run(["logout"])
13+
try group.run([])
14+
try group.run(["login", "kyle"])
15+
try group.run(["logout"])

Commander/CommandType.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/// Represents a command that can be run, given an argument parser
22
public protocol CommandType {
3-
func run(parser:ArgumentParser)
3+
func run(parser:ArgumentParser) throws
44
}
55

66

77
/// Extensions to CommandType to provide convinience running methods
88
extension CommandType {
99
/// Run the command with an array of arguments
10-
public func run(arguments:[String]) {
11-
run(ArgumentParser(arguments: arguments))
10+
public func run(arguments:[String]) throws {
11+
try run(ArgumentParser(arguments: arguments))
1212
}
1313

1414
/// Run the command using the `Process.argument`, removing the executable name
15-
public func run() {
15+
public func run() throws {
1616
let parser = ArgumentParser(arguments: Process.arguments)
1717
parser.shift() // Executable Name
18-
run(parser)
18+
try run(parser)
1919
}
2020
}

Commander/Group.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public class Group : CommandType {
1111
}
1212

1313
/// Run the group command
14-
public func run(parser:ArgumentParser) {
14+
public func run(parser:ArgumentParser) throws {
1515
if let name = parser.shift() {
1616
if let command = commands[name] {
17-
command.run(parser)
17+
try command.run(parser)
1818
}
1919
}
2020
}

CommanderTests/CommandTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class CommandTests : XCTestCase {
77
let parser = ArgumentParser(arguments: [])
88
var didRun = false
99

10-
command {
10+
try! command {
1111
didRun = true
1212
}.run(parser)
1313

@@ -18,7 +18,7 @@ class CommandTests : XCTestCase {
1818
let parser = ArgumentParser(arguments: ["Kyle", "Fuller"])
1919
var ranName:String? = nil
2020

21-
command { (name:String) in
21+
try! command { (name:String) in
2222
ranName = name
2323
}.run(parser)
2424

CommanderTests/CommandTypeTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class CommandTypeTests : XCTestCase {
66
func testRunWithArgumentsArray() {
77
var firstArgument:String? = nil
88

9-
command { (parser:ArgumentParser) in
9+
try! command { (parser:ArgumentParser) in
1010
firstArgument = parser.shift()
1111
}.run(["test"])
1212

CommanderTests/GroupTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class GroupTests : XCTestCase {
1313

1414
XCTAssertFalse(didRunHelpCommand)
1515

16-
group.run(["unknown"])
16+
try! group.run(["unknown"])
1717
XCTAssertFalse(didRunHelpCommand)
1818

19-
group.run(["help"])
19+
try! group.run(["help"])
2020
XCTAssertTrue(didRunHelpCommand)
2121
}
2222

@@ -31,17 +31,17 @@ class GroupTests : XCTestCase {
3131
})
3232
}
3333

34-
group.run(["unknown"])
34+
try! group.run(["unknown"])
3535
XCTAssertFalse(didRunHelpCommand)
3636

37-
group.run(["help"])
37+
try! group.run(["help"])
3838
XCTAssertTrue(didRunHelpCommand)
3939
}
4040

4141
func testSubGroup() {
4242
var didRun = false
4343

44-
Group {
44+
try! Group {
4545
$0.group("group") {
4646
$0.command("test") {
4747
didRun = true
@@ -55,19 +55,19 @@ class GroupTests : XCTestCase {
5555
func testSubCommand() {
5656
var didRun = false
5757

58-
Group {
58+
try! Group {
5959
$0.command("test") {
6060
didRun = true
6161
}
62-
}.run(["test"])
62+
}.run(["test"])
6363

6464
XCTAssertTrue(didRun)
6565
}
6666

6767
func testSubCommandWithArgument() {
6868
var givenName:String? = nil
6969

70-
Group {
70+
try! Group {
7171
$0.command("test") { (name:String) in
7272
givenName = name
7373
}

0 commit comments

Comments
 (0)