Skip to content

Clean up options and flags for package, build, and test subcommands #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 26, 2016
3 changes: 1 addition & 2 deletions Sources/Commands/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ extension Error: CustomStringConvertible {
case OptionParserError.multipleModesSpecified(let modes):
print(error: error)

if isTTY(.stdErr)
&& (modes.contains{ ["--help", "-h", "--usage"].contains($0) }) {
if isTTY(.stdErr) && (modes.contains{ ["--help", "-h"].contains($0) }) {
print("", to: &stderr)
usage { print($0, to: &stderr) }
}
Expand Down
28 changes: 13 additions & 15 deletions Sources/Commands/SwiftBuildTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ private enum Mode: Argument, Equatable, CustomStringConvertible {

init?(argument: String, pop: () -> String?) throws {
switch argument {
case "--configuration", "--conf", "-c":
case "--configuration", "--config", "-c":
self = try .build(Configuration(pop()), UserToolchain())
case "--clean":
self = try .clean(CleanMode(pop()))
case "--help", "--usage", "-h":
case "--help", "-h":
self = .usage
case "--version":
self = .version
Expand All @@ -52,8 +52,8 @@ private enum Mode: Argument, Equatable, CustomStringConvertible {

var description: String {
switch self {
case .build(let conf, _): return "--configuration=\(conf)"
case .clean(let mode): return "--clean=\(mode)"
case .build(let conf, _): return "--configuration \(conf)"
case .clean(let mode): return "--clean \(mode)"
case .usage: return "--help"
case .version: return "--version"
}
Expand Down Expand Up @@ -84,8 +84,6 @@ private enum BuildToolFlag: Argument {
self = try .chdir(forcePop())
case "--verbose", "-v":
self = .verbose(1)
case "-vv":
self = .verbose(2)
case "-Xcc":
self = try .xcc(forcePop())
case "-Xlinker":
Expand Down Expand Up @@ -213,17 +211,17 @@ public struct SwiftBuildTool {
print("USAGE: swift build [mode] [options]")
print("")
print("MODES:")
print(" --configuration <value> Build with configuration (debug|release) [-c]")
print(" --clean[=<mode>] Delete artifacts (build|dist)")
print(" -c, --configuration <value> Build with configuration (debug|release)")
print(" --clean <mode> Delete artifacts (build|dist)")
print("")
print("OPTIONS:")
print(" --chdir <path> Change working directory before any other operation [-C]")
print(" --build-path <path> Specify build directory")
print(" --color <mode> Specify color mode (auto|always|never)")
print(" -v[v] Increase verbosity of informational output")
print(" -Xcc <flag> Pass flag through to all C compiler instantiations")
print(" -Xlinker <flag> Pass flag through to all linker instantiations")
print(" -Xswiftc <flag> Pass flag through to all Swift compiler instantiations")
print(" -C, --chdir <path> Change working directory before any other operation")
print(" --build-path <path> Specify build directory")
print(" --color <mode> Specify color mode (auto|always|never)")
print(" -v, --verbose Increase verbosity of informational output")
print(" -Xcc <flag> Pass flag through to all C compiler invocations")
print(" -Xlinker <flag> Pass flag through to all linker invocations")
print(" -Xswiftc <flag> Pass flag through to all Swift compiler invocations")
print("")
print("NOTE: Use `swift package` to perform other functions on packages")
}
Expand Down
103 changes: 59 additions & 44 deletions Sources/Commands/SwiftPackageTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,57 +30,60 @@ import func POSIX.chdir
extension PackageToolOptions: XcodeprojOptions {}

private enum Mode: Argument, Equatable, CustomStringConvertible {
case Init(InitMode)
case doctor
case showDependencies(ShowDependenciesMode)
case dumpPackage
case fetch
case generateXcodeproj
case initPackage
case showDependencies
case update
case usage
case version
case generateXcodeproj(String?)
case dumpPackage(String?)

init?(argument: String, pop: () -> String?) throws {
switch argument {
case "init", "initialize":
self = try .Init(InitMode(pop()))
case "doctor":
self = .doctor
case "show-dependencies", "-D":
self = try .showDependencies(ShowDependenciesMode(pop()))
case "dump-package":
self = .dumpPackage
case "fetch":
self = .fetch
case "generate-xcodeproj":
self = .generateXcodeproj
case "init":
self = .initPackage
case "show-dependencies":
self = .showDependencies
case "update":
self = .update
case "help", "usage", "--help", "-h":
case "--help", "-h":
self = .usage
case "version":
case "--version":
self = .version
case "generate-xcodeproj":
self = .generateXcodeproj(pop())
case "dump-package":
self = .dumpPackage(pop())
default:
return nil
}
}

var description: String {
switch self {
case .Init(let type): return "init=\(type)"
case .doctor: return "doctor"
case .showDependencies: return "show-dependencies"
case .generateXcodeproj: return "generate-xcodeproj"
case .dumpPackage: return "dump-package"
case .fetch: return "fetch"
case .generateXcodeproj: return "generate-xcodeproj"
case .initPackage: return "initPackage"
case .showDependencies: return "show-dependencies"
case .update: return "update"
case .usage: return "help"
case .version: return "version"
case .dumpPackage: return "dump-package"
case .usage: return "--help"
case .version: return "--version"
}
}
}

private enum PackageToolFlag: Argument {
case initMode(String)
case showDepsMode(String)
case outputPath(String)
case chdir(String)
case colorMode(ColorWrap.Mode)
case xcc(String)
Expand All @@ -100,10 +103,14 @@ private enum PackageToolFlag: Argument {
switch argument {
case Flag.chdir, Flag.C:
self = try .chdir(forcePop())
case "--type":
self = try .initMode(forcePop())
case "--format":
self = try .showDepsMode(forcePop())
case "--output":
self = try .outputPath(forcePop())
case "--verbose", "-v":
self = .verbose(1)
case "-vv":
self = .verbose(2)
case "--color":
let rawValue = try forcePop()
guard let mode = ColorWrap.Mode(rawValue) else {
Expand All @@ -119,6 +126,9 @@ private enum PackageToolFlag: Argument {
}

private class PackageToolOptions: Options {
var initMode: InitMode = InitMode.library
var showDepsMode: ShowDependenciesMode = ShowDependenciesMode.text
var outputPath: String? = nil
var verbosity: Int = 0
var colorMode: ColorWrap.Mode = .Auto
var Xcc: [String] = []
Expand Down Expand Up @@ -164,8 +174,8 @@ public struct SwiftPackageTool {
}

switch mode {
case .Init(let initMode):
let initPackage = try InitPackage(mode: initMode)
case .initPackage:
let initPackage = try InitPackage(mode: opts.initMode)
try initPackage.writePackageStructure()

case .update:
Expand All @@ -181,9 +191,9 @@ public struct SwiftPackageTool {
case .doctor:
doctor()

case .showDependencies(let mode):
case .showDependencies:
let (rootPackage, _) = try fetch(opts.path.root)
dumpDependenciesOf(rootPackage: rootPackage, mode: mode)
dumpDependenciesOf(rootPackage: rootPackage, mode: opts.showDepsMode)

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

case .generateXcodeproj(let outpath):
case .generateXcodeproj:
let (rootPackage, externalPackages) = try fetch(opts.path.root)
let (modules, externalModules, products) = try transmute(rootPackage, externalPackages: externalPackages)

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

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

print("generated:", outpath.prettyPath)

case .dumpPackage(let packagePath):

let root = packagePath ?? opts.path.root
case .dumpPackage:
let root = opts.outputPath ?? opts.path.root
let manifest = try parseManifest(path: root, baseURL: root)
let package = manifest.package
let json = try jsonString(package: package)
Expand All @@ -240,21 +249,21 @@ public struct SwiftPackageTool {
print("USAGE: swift package [command] [options]")
print("")
print("COMMANDS:")
print(" init[=<type>] Initialize a new package (executable|library)")
print(" fetch Fetch package dependencies")
print(" update Update package dependencies")
print(" generate-xcodeproj[=<path>] Generates an Xcode project")
print(" show-dependencies[=<format>] Print dependency graph (text|dot|json)")
print(" dump-package[=<path>] Print Package.swift as JSON")
print(" init [--type <type>] Initialize package (library|executable)")
print(" fetch Fetch package dependencies")
print(" update Update package dependencies")
print(" generate-xcodeproj [--output <path>] Generates an Xcode project")
print(" show-dependencies [--format <format>] Print dependency graph (text|dot|json)")
print(" dump-package [--output <path>] Print Package.swift as JSON")
print("")
print("OPTIONS:")
print(" --chdir <path> Change working directory before any command [-C]")
print(" --color <mode> Specify color mode (auto|always|never)")
print(" --verbose Increase verbosity of informational output [-v]")
print(" -Xcc <flag> Pass flag through to all C compiler instantiations")
print(" -Xlinker <flag> Pass flag through to all linker instantiations")
print(" -Xswiftc <flag> Pass flag through to all Swift compiler instantiations")
print("")
print(" -C, --chdir <path> Change working directory before any other operation")
print(" --color <mode> Specify color mode (auto|always|never)")
print(" -v, --verbose Increase verbosity of informational output")
print(" --version Print the Swift Package Manager version")
print(" -Xcc <flag> Pass flag through to all C compiler invocations")
print(" -Xlinker <flag> Pass flag through to all linker invocations")
print(" -Xswiftc <flag> Pass flag through to all Swift compiler invocations")
print("")
print("NOTE: Use `swift build` to build packages, and `swift test` to test packages")
}
Expand All @@ -265,6 +274,12 @@ public struct SwiftPackageTool {
let opts = PackageToolOptions()
for flag in flags {
switch flag {
case .initMode(let value):
opts.initMode = try InitMode(value)
case .showDepsMode(let value):
opts.showDepsMode = try ShowDependenciesMode(value)
case .outputPath(let path):
opts.outputPath = path
case .chdir(let path):
opts.chdir = path
case .xcc(let value):
Expand Down
12 changes: 6 additions & 6 deletions Sources/Commands/SwiftTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ private enum Mode: Argument, Equatable, CustomStringConvertible {

init?(argument: String, pop: () -> String?) throws {
switch argument {
case "--help", "--usage", "-h":
case "--help", "-h":
self = .usage
case "-s":
case "-s", "--specifier":
guard let specifier = pop() else { throw OptionParserError.expectedAssociatedValue(argument) }
self = .run(specifier)
default:
Expand Down Expand Up @@ -140,12 +140,12 @@ public struct SwiftTestTool {
print("USAGE: swift test [specifier] [options]")
print("")
print("SPECIFIER:")
print(" -s TestModule.TestCase Run a test case subclass")
print(" -s TestModule.TestCase/test1 Run a specific test method")
print(" -s, --specifier <test-module>.<test-case> Run a test case subclass")
print(" -s, --specifier <test-module>.<test-case>/<test> Run a specific test method")
print("")
print("OPTIONS:")
print(" --chdir Change working directory before any other operation [-C]")
print(" --build-path <path> Specify build directory")
print(" -C, --chdir <path> Change working directory before any other operation")
print(" --build-path <path> Specify build directory")
print("")
print("NOTE: Use `swift package` to perform other functions on packages")
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Commands/init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ final class InitPackage {
enum InitMode: CustomStringConvertible {
case library, executable

init(_ rawValue: String?) throws {
switch rawValue?.lowercased() {
case "library"?, "lib"?:
init(_ rawValue: String) throws {
switch rawValue.lowercased() {
case "library":
self = .library
case nil, "executable"?, "exec"?, "exe"?:
case "executable":
self = .executable
default:
throw OptionParserError.invalidUsage("invalid initialization type: \(rawValue)")
Expand Down
2 changes: 1 addition & 1 deletion Sources/Commands/show-dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ enum ShowDependenciesMode: CustomStringConvertible {
case "json":
self = .json
default:
throw OptionParserError.invalidUsage("invalid show dependencies mode: \(rawValue)")
throw OptionParserError.invalidUsage("invalid show-dependencies mode: \(rawValue)")
}
}

Expand Down