Skip to content
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

Fix error message for @Option #435

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/ArgumentParser/Parsable Properties/Option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ extension Option where Value: ExpressibleByArgument {

/// The strategy to use when parsing a single value from `@Option` arguments.
///
/// - SeeAlso: `ArrayParsingStrategy``
/// - SeeAlso: ``ArrayParsingStrategy``
public struct SingleValueParsingStrategy: Hashable {
internal var base: ArgumentDefinition.ParsingStrategy

Expand Down
8 changes: 8 additions & 0 deletions Sources/ArgumentParser/Parsing/ArgumentSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ extension ArgumentSet {
usedOrigins.formUnion(origin)
inputArguments.removeAll(in: origin)

// Fix incorrect error message
// for @Option array without values (see issue #434).
guard let first = inputArguments.elements.first,
first.isValue
else {
throw ParserError.missingValueForOption(origin, parsed.name)
}

// ...and then consume the arguments until hitting an option
while let (origin2, value) = inputArguments.popNextElementIfValue() {
let origins = origin.inserting(origin2)
Expand Down
21 changes: 21 additions & 0 deletions Tests/ArgumentParserUnitTests/ErrorMessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,27 @@ extension ErrorMessageTests {
}
}

// (see issue #434).
private struct EmptyArray: ParsableArguments {
@Option(parsing: .upToNextOption)
var array: [String] = []

@Flag(name: [.short, .long])
var verbose = false
}

extension ErrorMessageTests {
func testEmptyArrayOption() {
AssertErrorMessage(EmptyArray.self, ["--array"], "Missing value for '--array <array>'")

AssertErrorMessage(EmptyArray.self, ["--array", "--verbose"], "Missing value for '--array <array>'")
AssertErrorMessage(EmptyArray.self, ["-verbose", "--array"], "Missing value for '--array <array>'")

AssertErrorMessage(EmptyArray.self, ["--array", "-v"], "Missing value for '--array <array>'")
AssertErrorMessage(EmptyArray.self, ["-v", "--array"], "Missing value for '--array <array>'")
}
}

// MARK: -

fileprivate struct Repeat: ParsableArguments {
Expand Down