Skip to content

Commit 34714c0

Browse files
committed
Merge pull request #370 from abertelrud/option-cleanup
Clean up options and flags for `package`, `build`, and `test` subcommands
2 parents 7aca33a + 14578b2 commit 34714c0

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
@@ -51,8 +51,7 @@ extension Error: CustomStringConvertible {
5151
case OptionParserError.multipleModesSpecified(let modes):
5252
print(error: error)
5353

54-
if isTTY(.stdErr)
55-
&& (modes.contains{ ["--help", "-h", "--usage"].contains($0) }) {
54+
if isTTY(.stdErr) && (modes.contains{ ["--help", "-h"].contains($0) }) {
5655
print("", to: &stderr)
5756
usage { print($0, to: &stderr) }
5857
}

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:
@@ -197,9 +207,9 @@ public struct SwiftPackageTool {
197207
case .doctor:
198208
doctor()
199209

200-
case .showDependencies(let mode):
210+
case .showDependencies:
201211
let (rootPackage, _) = try fetch(opts.path.root)
202-
dumpDependenciesOf(rootPackage: rootPackage, mode: mode)
212+
dumpDependenciesOf(rootPackage: rootPackage, mode: opts.showDepsMode)
203213

204214
case .version:
205215
#if HasCustomVersionString
@@ -208,7 +218,7 @@ public struct SwiftPackageTool {
208218
print("Swift Package Manager – Swift 3.0")
209219
#endif
210220

211-
case .generateXcodeproj(let outpath):
221+
case .generateXcodeproj:
212222
let (rootPackage, externalPackages) = try fetch(opts.path.root)
213223
let (modules, externalModules, products) = try transmute(rootPackage, externalPackages: externalPackages)
214224

@@ -219,7 +229,7 @@ public struct SwiftPackageTool {
219229
let dstdir: String
220230
let packageName = rootPackage.name
221231

222-
switch outpath {
232+
switch opts.outputPath {
223233
case let outpath? where outpath.hasSuffix(".xcodeproj"):
224234
// if user specified path ending with .xcodeproj, use that
225235
projectName = String(outpath.basename.characters.dropLast(10))
@@ -235,9 +245,8 @@ public struct SwiftPackageTool {
235245

236246
print("generated:", outpath.prettyPath)
237247

238-
case .dumpPackage(let packagePath):
239-
240-
let root = packagePath ?? opts.path.root
248+
case .dumpPackage:
249+
let root = opts.outputPath ?? opts.path.root
241250
let manifest = try parseManifest(path: root, baseURL: root)
242251
let package = manifest.package
243252
let json = try jsonString(package: package)
@@ -256,21 +265,21 @@ public struct SwiftPackageTool {
256265
print("USAGE: swift package [command] [options]")
257266
print("")
258267
print("COMMANDS:")
259-
print(" init[=<type>] Initialize a new package (executable|library)")
260-
print(" fetch Fetch package dependencies")
261-
print(" update Update package dependencies")
262-
print(" generate-xcodeproj[=<path>] Generates an Xcode project")
263-
print(" show-dependencies[=<format>] Print dependency graph (text|dot|json)")
264-
print(" dump-package[=<path>] Print Package.swift as JSON")
268+
print(" init [--type <type>] Initialize package (library|executable)")
269+
print(" fetch Fetch package dependencies")
270+
print(" update Update package dependencies")
271+
print(" generate-xcodeproj [--output <path>] Generates an Xcode project")
272+
print(" show-dependencies [--format <format>] Print dependency graph (text|dot|json)")
273+
print(" dump-package [--output <path>] Print Package.swift as JSON")
265274
print("")
266275
print("OPTIONS:")
267-
print(" --chdir <path> Change working directory before any command [-C]")
268-
print(" --color <mode> Specify color mode (auto|always|never)")
269-
print(" --verbose Increase verbosity of informational output [-v]")
270-
print(" -Xcc <flag> Pass flag through to all C compiler instantiations")
271-
print(" -Xlinker <flag> Pass flag through to all linker instantiations")
272-
print(" -Xswiftc <flag> Pass flag through to all Swift compiler instantiations")
273-
print("")
276+
print(" -C, --chdir <path> Change working directory before any other operation")
277+
print(" --color <mode> Specify color mode (auto|always|never)")
278+
print(" -v, --verbose Increase verbosity of informational output")
279+
print(" --version Print the Swift Package Manager version")
280+
print(" -Xcc <flag> Pass flag through to all C compiler invocations")
281+
print(" -Xlinker <flag> Pass flag through to all linker invocations")
282+
print(" -Xswiftc <flag> Pass flag through to all Swift compiler invocations")
274283
print("")
275284
print("NOTE: Use `swift build` to build packages, and `swift test` to test packages")
276285
}
@@ -281,6 +290,12 @@ public struct SwiftPackageTool {
281290
let opts = PackageToolOptions()
282291
for flag in flags {
283292
switch flag {
293+
case .initMode(let value):
294+
opts.initMode = try InitMode(value)
295+
case .showDepsMode(let value):
296+
opts.showDepsMode = try ShowDependenciesMode(value)
297+
case .outputPath(let path):
298+
opts.outputPath = path
284299
case .chdir(let path):
285300
opts.chdir = path
286301
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)