mirror of
https://github.com/apple/swift-argument-parser.git
synced 2026-06-06 20:18:23 +00:00
Store mutations made by validate() (#239)
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user