Skip to content

Commit

Permalink
Fix error message for @option array without values (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
KeithBird committed Apr 4, 2022
1 parent 18b0039 commit dd6efd0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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

0 comments on commit dd6efd0

Please sign in to comment.