Skip to content

Commit ccfe0af

Browse files
committed
Add command groups
1 parent e112a21 commit ccfe0af

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

Commander.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
279256AC1BB322FA00E66B9E /* Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279256AB1BB322FA00E66B9E /* Command.swift */; settings = {ASSET_TAGS = (); }; };
1515
279256AE1BB3237300E66B9E /* CommandTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279256AD1BB3237300E66B9E /* CommandTypeTests.swift */; settings = {ASSET_TAGS = (); }; };
1616
279256B01BB3237D00E66B9E /* CommandTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279256AF1BB3237D00E66B9E /* CommandTests.swift */; settings = {ASSET_TAGS = (); }; };
17+
279256B21BB325F400E66B9E /* GroupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279256B11BB325F400E66B9E /* GroupTests.swift */; settings = {ASSET_TAGS = (); }; };
18+
279256B41BB3260D00E66B9E /* Group.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279256B31BB3260D00E66B9E /* Group.swift */; settings = {ASSET_TAGS = (); }; };
1719
27B1A0FF1BACD69500B1260F /* ArgumentParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27B1A0FE1BACD69500B1260F /* ArgumentParserTests.swift */; settings = {ASSET_TAGS = (); }; };
1820
/* End PBXBuildFile section */
1921

@@ -38,6 +40,8 @@
3840
279256AB1BB322FA00E66B9E /* Command.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Command.swift; sourceTree = "<group>"; };
3941
279256AD1BB3237300E66B9E /* CommandTypeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommandTypeTests.swift; sourceTree = "<group>"; };
4042
279256AF1BB3237D00E66B9E /* CommandTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommandTests.swift; sourceTree = "<group>"; };
43+
279256B11BB325F400E66B9E /* GroupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GroupTests.swift; sourceTree = "<group>"; };
44+
279256B31BB3260D00E66B9E /* Group.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Group.swift; sourceTree = "<group>"; };
4145
27B1A0FE1BACD69500B1260F /* ArgumentParserTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArgumentParserTests.swift; sourceTree = "<group>"; };
4246
/* End PBXFileReference section */
4347

@@ -85,6 +89,7 @@
8589
2768A23A1BACC3B600F994EE /* ArgumentParser.swift */,
8690
279256A91BB3226E00E66B9E /* CommandType.swift */,
8791
279256AB1BB322FA00E66B9E /* Command.swift */,
92+
279256B31BB3260D00E66B9E /* Group.swift */,
8893
2768A2251BACC38C00F994EE /* Info.plist */,
8994
);
9095
path = Commander;
@@ -96,6 +101,7 @@
96101
27B1A0FE1BACD69500B1260F /* ArgumentParserTests.swift */,
97102
279256AD1BB3237300E66B9E /* CommandTypeTests.swift */,
98103
279256AF1BB3237D00E66B9E /* CommandTests.swift */,
104+
279256B11BB325F400E66B9E /* GroupTests.swift */,
99105
2768A2311BACC38C00F994EE /* Info.plist */,
100106
);
101107
path = CommanderTests;
@@ -212,6 +218,7 @@
212218
2768A23B1BACC3B600F994EE /* ArgumentParser.swift in Sources */,
213219
279256AA1BB3226E00E66B9E /* CommandType.swift in Sources */,
214220
279256AC1BB322FA00E66B9E /* Command.swift in Sources */,
221+
279256B41BB3260D00E66B9E /* Group.swift in Sources */,
215222
);
216223
runOnlyForDeploymentPostprocessing = 0;
217224
};
@@ -222,6 +229,7 @@
222229
279256B01BB3237D00E66B9E /* CommandTests.swift in Sources */,
223230
27B1A0FF1BACD69500B1260F /* ArgumentParserTests.swift in Sources */,
224231
279256AE1BB3237300E66B9E /* CommandTypeTests.swift in Sources */,
232+
279256B21BB325F400E66B9E /* GroupTests.swift in Sources */,
225233
);
226234
runOnlyForDeploymentPostprocessing = 0;
227235
};

Commander/Group.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// Represents a group of commands
2+
public class Group : CommandType {
3+
private var commands = [String:CommandType]()
4+
5+
/// Create a new group
6+
public init() {}
7+
8+
/// Add a named sub-command to the group
9+
public func addCommand(name:String, command:CommandType) {
10+
commands[name] = command
11+
}
12+
13+
/// Run the group command
14+
public func run(parser:ArgumentParser) {
15+
if let name = parser.shift() {
16+
if let command = commands[name] {
17+
command.run(parser)
18+
}
19+
}
20+
}
21+
}
22+
23+
extension Group {
24+
public convenience init(closure:Group -> ()) {
25+
self.init()
26+
closure(self)
27+
}
28+
}

CommanderTests/GroupTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import XCTest
2+
import Commander
3+
4+
5+
class GroupTests : XCTestCase {
6+
func testGroupDispatchesSubCommands() {
7+
var didRunHelpCommand = false
8+
9+
let group = Group()
10+
group.addCommand("help", command: command {
11+
didRunHelpCommand = true
12+
})
13+
14+
XCTAssertFalse(didRunHelpCommand)
15+
16+
group.run(["unknown"])
17+
XCTAssertFalse(didRunHelpCommand)
18+
19+
group.run(["help"])
20+
XCTAssertTrue(didRunHelpCommand)
21+
}
22+
23+
// MARK: Extension
24+
25+
func testClosureConvinienceInitialiser() {
26+
var didRunHelpCommand = false
27+
28+
let group = Group {
29+
$0.addCommand("help", command: command {
30+
didRunHelpCommand = true
31+
})
32+
}
33+
34+
group.run(["unknown"])
35+
XCTAssertFalse(didRunHelpCommand)
36+
37+
group.run(["help"])
38+
XCTAssertTrue(didRunHelpCommand)
39+
}
40+
}

0 commit comments

Comments
 (0)