Store mutations made by validate() (#239)

This commit is contained in:
Elliott Williams
2020-09-18 11:55:07 -05:00
committed by GitHub
parent 92646c0cdb
commit c17e00a3bf
2 changed files with 28 additions and 0 deletions
@@ -157,6 +157,9 @@ extension CommandParser {
// after decoding a command, make sure to validate it
do {
try parsedCommand.validate()
var lastArgument = decodedArguments.removeLast()
lastArgument.value = parsedCommand
decodedArguments.append(lastArgument)
} catch {
try checkForBuiltInFlags(split)
throw CommandError(commandStack: commandStack, parserError: ParserError.userValidationError(error))
@@ -146,3 +146,28 @@ extension ValidationEndToEndTests {
AssertFullErrorMessage(Foo.self, ["--fail-silently", "Joe"], "")
}
}
fileprivate struct FooCommand: ParsableCommand {
@Flag(help: .hidden)
var foo = false
@Flag(help: .hidden)
var bar = false
mutating func validate() throws {
if foo {
// --foo implies --bar
bar = true
}
}
func run() throws {
XCTAssertEqual(foo, bar)
}
}
extension ValidationEndToEndTests {
func testMutationsPreserved() throws {
var foo = try FooCommand.parseAsRoot(["--foo"])
try foo.run()
}
}