Skip to content

Commit 6522a20

Browse files
committed
Move libc runner on it's own
1 parent feccfab commit 6522a20

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

Commander.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
2714C5591BB5403400E0EB6A /* Commands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2714C5581BB5403300E0EB6A /* Commands.swift */; settings = {ASSET_TAGS = (); }; };
1111
2720BDED1BB348BC00C09984 /* ArgumentDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2720BDEC1BB348BC00C09984 /* ArgumentDescription.swift */; settings = {ASSET_TAGS = (); }; };
12+
275C0E8F1BBC5D5900F92D73 /* CommandDarwin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 275C0E8E1BBC5D5900F92D73 /* CommandDarwin.swift */; settings = {ASSET_TAGS = (); }; };
1213
2768A2241BACC38C00F994EE /* Commander.h in Headers */ = {isa = PBXBuildFile; fileRef = 2768A2231BACC38C00F994EE /* Commander.h */; settings = {ATTRIBUTES = (Public, ); }; };
1314
2768A22B1BACC38C00F994EE /* Commander.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2768A2201BACC38C00F994EE /* Commander.framework */; settings = {ASSET_TAGS = (); }; };
1415
2768A23B1BACC3B600F994EE /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2768A23A1BACC3B600F994EE /* ArgumentParser.swift */; settings = {ASSET_TAGS = (); }; };
@@ -40,6 +41,7 @@
4041
27593EE51BB335590018278E /* UniversalFramework_Base.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = UniversalFramework_Base.xcconfig; sourceTree = "<group>"; };
4142
27593EE61BB335590018278E /* UniversalFramework_Framework.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = UniversalFramework_Framework.xcconfig; sourceTree = "<group>"; };
4243
27593EE71BB335590018278E /* UniversalFramework_Test.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = UniversalFramework_Test.xcconfig; sourceTree = "<group>"; };
44+
275C0E8E1BBC5D5900F92D73 /* CommandDarwin.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommandDarwin.swift; sourceTree = "<group>"; };
4345
2768A2201BACC38C00F994EE /* Commander.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Commander.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4446
2768A2231BACC38C00F994EE /* Commander.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Commander.h; sourceTree = "<group>"; };
4547
2768A2251BACC38C00F994EE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -117,6 +119,7 @@
117119
2720BDEC1BB348BC00C09984 /* ArgumentDescription.swift */,
118120
279256A91BB3226E00E66B9E /* CommandType.swift */,
119121
279256AB1BB322FA00E66B9E /* Command.swift */,
122+
275C0E8E1BBC5D5900F92D73 /* CommandDarwin.swift */,
120123
2714C5581BB5403300E0EB6A /* Commands.swift */,
121124
279256B31BB3260D00E66B9E /* Group.swift */,
122125
2768A2251BACC38C00F994EE /* Info.plist */,
@@ -248,6 +251,7 @@
248251
files = (
249252
2768A23B1BACC3B600F994EE /* ArgumentParser.swift in Sources */,
250253
2720BDED1BB348BC00C09984 /* ArgumentDescription.swift in Sources */,
254+
275C0E8F1BBC5D5900F92D73 /* CommandDarwin.swift in Sources */,
251255
2714C5591BB5403400E0EB6A /* Commands.swift in Sources */,
252256
279256AA1BB3226E00E66B9E /* CommandType.swift in Sources */,
253257
279256AC1BB322FA00E66B9E /* Command.swift in Sources */,

Commander/CommandDarwin.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Darwin.libc
2+
3+
/// Extensions to CommandType to provide convinience running methods for CLI tools
4+
extension CommandType {
5+
/// Run the command using the `Process.argument`, removing the executable name
6+
@noreturn public func run(version:String? = nil) {
7+
let parser = ArgumentParser(arguments: Process.arguments)
8+
9+
if parser.hasOption("version") && !parser.hasOption("help") {
10+
if let version = version {
11+
print(version)
12+
exit(0)
13+
}
14+
}
15+
16+
let executableName = parser.shift()! // Executable Name
17+
18+
do {
19+
try run(parser)
20+
} catch let error as Help {
21+
let help = error.reraise("$ \(executableName)")
22+
fputs("\(help)\n", stderr)
23+
exit(1)
24+
} catch GroupError.NoCommand(let path, let group) {
25+
var usage = "$ \(executableName)"
26+
if let path = path {
27+
usage += " \(path)"
28+
}
29+
let help = Help([], command: usage, group: group)
30+
fputs("\(help)\n", stderr)
31+
exit(1)
32+
} catch {
33+
fputs("\(error)\n", stderr)
34+
exit(1)
35+
}
36+
37+
exit(0)
38+
}
39+
}

Commander/CommandType.swift

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,5 @@ extension CommandType {
1010
public func run(arguments:[String]) throws {
1111
try run(ArgumentParser(arguments: arguments))
1212
}
13-
14-
/// Run the command using the `Process.argument`, removing the executable name
15-
@noreturn public func run(version:String? = nil) {
16-
let parser = ArgumentParser(arguments: Process.arguments)
17-
18-
if parser.hasOption("version") && !parser.hasOption("help") {
19-
if let version = version {
20-
print(version)
21-
exit(0)
22-
}
23-
}
24-
25-
let executableName = parser.shift()! // Executable Name
26-
27-
do {
28-
try run(parser)
29-
} catch let error as Help {
30-
let help = error.reraise("$ \(executableName)")
31-
fputs("\(help)\n", stderr)
32-
exit(1)
33-
} catch GroupError.NoCommand(let path, let group) {
34-
var usage = "$ \(executableName)"
35-
if let path = path {
36-
usage += " \(path)"
37-
}
38-
let help = Help([], command: usage, group: group)
39-
fputs("\(help)\n", stderr)
40-
exit(1)
41-
} catch {
42-
fputs("\(error)\n", stderr)
43-
exit(1)
44-
}
45-
46-
exit(0)
47-
}
4813
}
14+

0 commit comments

Comments
 (0)