Skip to content

Commit ba87617

Browse files
committed
Merge pull request #370 from abertelrud/option-cleanup
Clean up options and flags for `package`, `build`, and `test` subcommands
1 parent 62c9268 commit ba87617

File tree

6 files changed

+84
-72
lines changed

6 files changed

+84
-72
lines changed

Sources/Commands/Error.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ extension Error: CustomStringConvertible {
4848
case OptionParserError.multipleModesSpecified(let modes):
4949
print(error: error)
5050

51-
if isTTY(.stdErr)
52-
&& (modes.contains{ ["--help", "-h", "--usage"].contains($0) }) {
51+
if isTTY(.stdErr) && (modes.contains{ ["--help", "-h"].contains($0) }) {
5352
print("", to: &stderr)
5453
usage { print($0, to: &stderr) }
5554
}

Sources/Commands/SwiftBuildTool.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ private enum Mode: Argument, Equatable, CustomStringConvertible {
3737

3838
init?(argument: String, pop: () -> String?) throws {
3939
switch argument {
40-
case "--configuration", "--conf", "-c":
40+
case "--configuration", "--config", "-c":
4141
self = try .build(Configuration(pop()), UserToolchain())
4242
case "--clean":
4343
self = try .clean(CleanMode(pop()))
44-
case "--help", "--usage", "-h":
44+
case "--help", "-h":
4545
self = .usage
4646
case "--version":
4747
self = .version
@@ -52,8 +52,8 @@ private enum Mode: Argument, Equatable, CustomStringConvertible {
5252

5353
var description: String {
5454
switch self {
55-
case .build(let conf, _): return "--configuration=\(conf)"
56-
case .clean(let mode): return "--clean=\(mode)"
55+
case .build(let conf, _): return "--configuration \(conf)"
56+
case .clean(let mode): return "--clean \(mode)"
5757
case .usage: return "--help"
5858
case .version: return "--version"
5959
}
@@ -84,8 +84,6 @@ private enum BuildToolFlag: Argument {
8484
self = try .chdir(forcePop())
8585
case "--verbose", "-v":
8686
self = .verbose(1)
87-
case "-vv":
88-
self = .verbose(2)
8987
case "-Xcc":
9088
self = try .xcc(forcePop())
9189
case "-Xlinker":
@@ -213,17 +211,17 @@ public struct SwiftBuildTool {
213211
print("USAGE: swift build [mode] [options]")
214212
print("")
215213
print("MODES:")
216-
print(" --configuration <value> Build with configuration (debug|release) [-c]")
217-
print(" --clean[=<mode>] Delete artifacts (build|dist)")
214+
print(" -c, --configuration <value> Build with configuration (debug|release)")
215+
print(" --clean <mode> Delete artifacts (build|dist)")
218216
print("")
219217
print("OPTIONS:")
220-
print(" --chdir <path> Change working directory before any other operation [-C]")
221-
print(" --build-path <path> Specify build directory")
222-
print(" --color <mode> Specify color mode (auto|always|never)")
223-
print(" -v[v] Increase verbosity of informational output")
224-
print(" -Xcc <flag> Pass flag through to all C compiler instantiations")
225-
print(" -Xlinker <flag> Pass flag through to all linker instantiations")
226-
print(" -Xswiftc <flag> Pass flag through to all Swift compiler instantiations")
218+
print(" -C, --chdir <path> Change working directory before any other operation")
219+
print(" --build-path <path> Specify build directory")
220+
print(" --color <mode> Specify color mode (auto|always|never)")
221+
print(" -v, --verbose Increase verbosity of informational output")
222+
print(" -Xcc <flag> Pass flag through to all C compiler invocations")
223+
print(" -Xlinker <flag> Pass flag through to all linker invocations")
224+
print(" -Xswiftc <flag> Pass flag through to all Swift compiler invocations")
227225
print("")
228226
print("NOTE: Use `swift package` to perform other functions on packages")
229227
}

Sources/Commands/SwiftPackageTool.swift

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,57 +30,60 @@ import func POSIX.chdir
3030
extension PackageToolOptions: XcodeprojOptions {}
3131

3232
private enum Mode: Argument, Equatable, CustomStringConvertible {
33-
case Init(InitMode)
3433
case doctor
35-
case showDependencies(ShowDependenciesMode)
34+
case dumpPackage
3635
case fetch
36+
case generateXcodeproj
37+
case initPackage
38+
case showDependencies
3739
case update
3840
case usage
3941
case version
40-
case generateXcodeproj(String?)
41-
case dumpPackage(String?)
4242

4343
init?(argument: String, pop: () -> String?) throws {
4444
switch argument {
45-
case "init", "initialize":
46-
self = try .Init(InitMode(pop()))
4745
case "doctor":
4846
self = .doctor
49-
case "show-dependencies", "-D":
50-
self = try .showDependencies(ShowDependenciesMode(pop()))
47+
case "dump-package":
48+
self = .dumpPackage
5149
case "fetch":
5250
self = .fetch
51+
case "generate-xcodeproj":
52+
self = .generateXcodeproj
53+
case "init":
54+
self = .initPackage
55+
case "show-dependencies":
56+
self = .showDependencies
5357
case "update":
5458
self = .update
55-
case "help", "usage", "--help", "-h":
59+
case "--help", "-h":
5660
self = .usage
57-
case "version":
61+
case "--version":
5862
self = .version
59-
case "generate-xcodeproj":
60-
self = .generateXcodeproj(pop())
61-
case "dump-package":
62-
self = .dumpPackage(pop())
6363
default:
6464
return nil
6565
}
6666
}
6767

6868
var description: String {
6969
switch self {
70-
case .Init(let type): return "init=\(type)"
7170
case .doctor: return "doctor"
72-
case .showDependencies: return "show-dependencies"
73-
case .generateXcodeproj: return "generate-xcodeproj"
71+
case .dumpPackage: return "dump-package"
7472
case .fetch: return "fetch"
73+
case .generateXcodeproj: return "generate-xcodeproj"
74+
case .initPackage: return "initPackage"
75+
case .showDependencies: return "show-dependencies"
7576
case .update: return "update"
76-
case .usage: return "help"
77-
case .version: return "version"
78-
case .dumpPackage: return "dump-package"
77+
case .usage: return "--help"
78+
case .version: return "--version"
7979
}
8080
}
8181
}
8282

8383
private enum PackageToolFlag: Argument {
84+
case initMode(String)
85+
case showDepsMode(String)
86+
case outputPath(String)
8487
case chdir(String)
8588
case colorMode(ColorWrap.Mode)
8689
case xcc(String)
@@ -100,10 +103,14 @@ private enum PackageToolFlag: Argument {
100103
switch argument {
101104
case Flag.chdir, Flag.C:
102105
self = try .chdir(forcePop())
106+
case "--type":
107+
self = try .initMode(forcePop())
108+
case "--format":
109+
self = try .showDepsMode(forcePop())
110+
case "--output":
111+
self = try .outputPath(forcePop())
103112
case "--verbose", "-v":
104113
self = .verbose(1)
105-
case "-vv":
106-
self = .verbose(2)
107114
case "--color":
108115
let rawValue = try forcePop()
109116
guard let mode = ColorWrap.Mode(rawValue) else {
@@ -119,6 +126,9 @@ private enum PackageToolFlag: Argument {
119126
}
120127

121128
private class PackageToolOptions: Options {
129+
var initMode: InitMode = InitMode.library
130+
var showDepsMode: ShowDependenciesMode = ShowDependenciesMode.text
131+
var outputPath: String? = nil
122132
var verbosity: Int = 0
123133
var colorMode: ColorWrap.Mode = .Auto
124134
var Xcc: [String] = []
@@ -164,8 +174,8 @@ public struct SwiftPackageTool {
164174
}
165175

166176
switch mode {
167-
case .Init(let initMode):
168-
let initPackage = try InitPackage(mode: initMode)
177+
case .initPackage:
178+
let initPackage = try InitPackage(mode: opts.initMode)
169179
try initPackage.writePackageStructure()
170180

171181
case .update:
@@ -181,9 +191,9 @@ public struct SwiftPackageTool {
181191
case .doctor:
182192
doctor()
183193

184-
case .showDependencies(let mode):
194+
case .showDependencies:
185195
let (rootPackage, _) = try fetch(opts.path.root)
186-
dumpDependenciesOf(rootPackage: rootPackage, mode: mode)
196+
dumpDependenciesOf(rootPackage: rootPackage, mode: opts.showDepsMode)
187197

188198
case .version:
189199
#if HasCustomVersionString
@@ -192,7 +202,7 @@ public struct SwiftPackageTool {
192202
print("Swift Package Manager – Swift 3.0")
193203
#endif
194204

195-
case .generateXcodeproj(let outpath):
205+
case .generateXcodeproj:
196206
let (rootPackage, externalPackages) = try fetch(opts.path.root)
197207
let (modules, externalModules, products) = try transmute(rootPackage, externalPackages: externalPackages)
198208

@@ -203,7 +213,7 @@ public struct SwiftPackageTool {
203213
let dstdir: String
204214
let packageName = rootPackage.name
205215

206-
switch outpath {
216+
switch opts.outputPath {
207217
case let outpath? where outpath.hasSuffix(".xcodeproj"):
208218
// if user specified path ending with .xcodeproj, use that
209219
projectName = String(outpath.basename.characters.dropLast(10))
@@ -219,9 +229,8 @@ public struct SwiftPackageTool {
219229

220230
print("generated:", outpath.prettyPath)
221231

222-
case .dumpPackage(let packagePath):
223-
224-
let root = packagePath ?? opts.path.root
232+
case .dumpPackage:
233+
let root = opts.outputPath ?? opts.path.root
225234
let manifest = try parseManifest(path: root, baseURL: root)
226235
let package = manifest.package
227236
let json = try jsonString(package: package)
@@ -240,21 +249,21 @@ public struct SwiftPackageTool {
240249
print("USAGE: swift package [command] [options]")
241250
print("")
242251
print("COMMANDS:")
243-
print(" init[=<type>] Initialize a new package (executable|library)")
244-
print(" fetch Fetch package dependencies")
245-
print(" update Update package dependencies")
246-
print(" generate-xcodeproj[=<path>] Generates an Xcode project")
247-
print(" show-dependencies[=<format>] Print dependency graph (text|dot|json)")
248-
print(" dump-package[=<path>] Print Package.swift as JSON")
252+
print(" init [--type <type>] Initialize package (library|executable)")
253+
print(" fetch Fetch package dependencies")
254+
print(" update Update package dependencies")
255+
print(" generate-xcodeproj [--output <path>] Generates an Xcode project")
256+
print(" show-dependencies [--format <format>] Print dependency graph (text|dot|json)")
257+
print(" dump-package [--output <path>] Print Package.swift as JSON")
249258
print("")
250259
print("OPTIONS:")
251-
print(" --chdir <path> Change working directory before any command [-C]")
252-
print(" --color <mode> Specify color mode (auto|always|never)")
253-
print(" --verbose Increase verbosity of informational output [-v]")
254-
print(" -Xcc <flag> Pass flag through to all C compiler instantiations")
255-
print(" -Xlinker <flag> Pass flag through to all linker instantiations")
256-
print(" -Xswiftc <flag> Pass flag through to all Swift compiler instantiations")
257-
print("")
260+
print(" -C, --chdir <path> Change working directory before any other operation")
261+
print(" --color <mode> Specify color mode (auto|always|never)")
262+
print(" -v, --verbose Increase verbosity of informational output")
263+
print(" --version Print the Swift Package Manager version")
264+
print(" -Xcc <flag> Pass flag through to all C compiler invocations")
265+
print(" -Xlinker <flag> Pass flag through to all linker invocations")
266+
print(" -Xswiftc <flag> Pass flag through to all Swift compiler invocations")
258267
print("")
259268
print("NOTE: Use `swift build` to build packages, and `swift test` to test packages")
260269
}
@@ -265,6 +274,12 @@ public struct SwiftPackageTool {
265274
let opts = PackageToolOptions()
266275
for flag in flags {
267276
switch flag {
277+
case .initMode(let value):
278+
opts.initMode = try InitMode(value)
279+
case .showDepsMode(let value):
280+
opts.showDepsMode = try ShowDependenciesMode(value)
281+
case .outputPath(let path):
282+
opts.outputPath = path
268283
case .chdir(let path):
269284
opts.chdir = path
270285
case .xcc(let value):

Sources/Commands/SwiftTestTool.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ private enum Mode: Argument, Equatable, CustomStringConvertible {
3535

3636
init?(argument: String, pop: () -> String?) throws {
3737
switch argument {
38-
case "--help", "--usage", "-h":
38+
case "--help", "-h":
3939
self = .usage
40-
case "-s":
40+
case "-s", "--specifier":
4141
guard let specifier = pop() else { throw OptionParserError.expectedAssociatedValue(argument) }
4242
self = .run(specifier)
4343
default:
@@ -140,12 +140,12 @@ public struct SwiftTestTool {
140140
print("USAGE: swift test [specifier] [options]")
141141
print("")
142142
print("SPECIFIER:")
143-
print(" -s TestModule.TestCase Run a test case subclass")
144-
print(" -s TestModule.TestCase/test1 Run a specific test method")
143+
print(" -s, --specifier <test-module>.<test-case> Run a test case subclass")
144+
print(" -s, --specifier <test-module>.<test-case>/<test> Run a specific test method")
145145
print("")
146146
print("OPTIONS:")
147-
print(" --chdir Change working directory before any other operation [-C]")
148-
print(" --build-path <path> Specify build directory")
147+
print(" -C, --chdir <path> Change working directory before any other operation")
148+
print(" --build-path <path> Specify build directory")
149149
print("")
150150
print("NOTE: Use `swift package` to perform other functions on packages")
151151
}

Sources/Commands/init.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ final class InitPackage {
182182
enum InitMode: CustomStringConvertible {
183183
case library, executable
184184

185-
init(_ rawValue: String?) throws {
186-
switch rawValue?.lowercased() {
187-
case "library"?, "lib"?:
185+
init(_ rawValue: String) throws {
186+
switch rawValue.lowercased() {
187+
case "library":
188188
self = .library
189-
case nil, "executable"?, "exec"?, "exe"?:
189+
case "executable":
190190
self = .executable
191191
default:
192192
throw OptionParserError.invalidUsage("invalid initialization type: \(rawValue)")

Sources/Commands/show-dependencies.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ enum ShowDependenciesMode: CustomStringConvertible {
134134
case "json":
135135
self = .json
136136
default:
137-
throw OptionParserError.invalidUsage("invalid show dependencies mode: \(rawValue)")
137+
throw OptionParserError.invalidUsage("invalid show-dependencies mode: \(rawValue)")
138138
}
139139
}
140140

0 commit comments

Comments
 (0)