Refactor argument parsing a bit so we can reuse filtering between commands
This commit is contained in:
parent
05daa61692
commit
5cb09956d8
|
@ -5,6 +5,8 @@ A description of this package.
|
|||
## TODO
|
||||
|
||||
* `install-ca` command
|
||||
* `remove-ca` command
|
||||
* Filter for os types
|
||||
* Find out why the argument parsing errors are not printed correctly
|
||||
* `remove` command
|
||||
* `--dry-run` option
|
||||
* `remove` command that removes simulators
|
||||
|
|
|
@ -18,6 +18,18 @@ struct FilteringOptions {
|
|||
var availability: Availability = .yes
|
||||
}
|
||||
|
||||
extension ArgumentBinder where Options == FilteringOptions {
|
||||
func bind(to options: inout FilteringOptions, parser: ArgumentParser) {
|
||||
bind(option: parser.add(
|
||||
option: "--availability",
|
||||
kind: FilteringOptions.Availability.self,
|
||||
usage: "Only list available devices? yes|no|all, defaults to all"
|
||||
), to: { options, availability in
|
||||
options.availability = availability
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct CommandLineOptions {
|
||||
enum SubCommand {
|
||||
case noCommand
|
||||
|
@ -92,7 +104,7 @@ struct CommandLineOptions {
|
|||
|
||||
guard
|
||||
let subcommandName = result.subparser(parser),
|
||||
var command = allCommands.first(where: { $0.name == subcommandName })
|
||||
let command = allCommands.first(where: { $0.name == subcommandName })
|
||||
else {
|
||||
throw Error(
|
||||
underlyingError: NoCommandError(),
|
||||
|
|
|
@ -13,6 +13,6 @@ protocol Command {
|
|||
var overview: String { get }
|
||||
|
||||
func addOptions(to parser: ArgumentParser)
|
||||
mutating func fillParseResult(_ parseResult: ArgumentParser.Result) throws
|
||||
func fillParseResult(_ parseResult: ArgumentParser.Result) throws
|
||||
func run() throws
|
||||
}
|
||||
|
|
|
@ -5,29 +5,41 @@
|
|||
import Foundation
|
||||
import SPMUtility
|
||||
|
||||
struct InstallCACommand: Command {
|
||||
class InstallCACommand: Command {
|
||||
private struct Options {
|
||||
var path: String? = nil
|
||||
}
|
||||
let name = "install-ca"
|
||||
let overview = "Install a Certificate Authority"
|
||||
|
||||
private let binder = ArgumentBinder<InstallCACommand>()
|
||||
private var path: String?
|
||||
private let binder = ArgumentBinder<Options>()
|
||||
private var options = Options()
|
||||
|
||||
private let filteringBinder = ArgumentBinder<FilteringOptions>()
|
||||
private var filteringOptions = FilteringOptions()
|
||||
|
||||
func addOptions(to parser: ArgumentParser) {
|
||||
binder.bind(positional: parser.add(
|
||||
positional: "path",
|
||||
kind: String.self
|
||||
), to: { command, path in
|
||||
command.path = path
|
||||
kind: String.self,
|
||||
usage: "Path for the certificate to install"
|
||||
), to: { options, path in
|
||||
options.path = path
|
||||
})
|
||||
|
||||
filteringBinder.bind(to: &filteringOptions, parser: parser)
|
||||
}
|
||||
|
||||
mutating func fillParseResult(_ parseResult: ArgumentParser.Result) throws {
|
||||
try binder.fill(parseResult: parseResult, into: &self)
|
||||
func fillParseResult(_ parseResult: ArgumentParser.Result) throws {
|
||||
try binder.fill(parseResult: parseResult, into: &options)
|
||||
try filteringBinder.fill(parseResult: parseResult, into: &filteringOptions)
|
||||
}
|
||||
|
||||
func run() throws {
|
||||
let url = URL(fileURLWithPath: path!)
|
||||
let url = URL(fileURLWithPath: options.path!)
|
||||
let certificate = try Certificate.load(from: url)
|
||||
print(certificate)
|
||||
|
||||
print(filteringOptions)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,25 +5,21 @@
|
|||
import Foundation
|
||||
import SPMUtility
|
||||
|
||||
struct ListDevicesCommand: Command {
|
||||
class ListDevicesCommand: Command {
|
||||
let name = "list-devices"
|
||||
let overview = "List available Xcode Simulator devices"
|
||||
|
||||
private let binder = ArgumentBinder<ListDevicesCommand>()
|
||||
private var filteringOptions = FilteringOptions()
|
||||
private let filteringBinder = ArgumentBinder<FilteringOptions>()
|
||||
|
||||
func addOptions(to parser: ArgumentParser) {
|
||||
binder.bind(option: parser.add(
|
||||
option: "--availability",
|
||||
kind: FilteringOptions.Availability.self,
|
||||
usage: "Only list available devices? yes|no|all, defaults to all"
|
||||
), to: { command, availability in
|
||||
command.filteringOptions.availability = availability
|
||||
})
|
||||
filteringBinder.bind(to: &filteringOptions, parser: parser)
|
||||
}
|
||||
|
||||
mutating func fillParseResult(_ parseResult: ArgumentParser.Result) throws {
|
||||
try binder.fill(parseResult: parseResult, into: &self)
|
||||
func fillParseResult(_ parseResult: ArgumentParser.Result) throws {
|
||||
try filteringBinder.fill(parseResult: parseResult, into: &filteringOptions)
|
||||
// try binder.fill(parseResult: parseResult, into: &self)
|
||||
}
|
||||
|
||||
func run() throws {
|
||||
|
|
Loading…
Reference in New Issue