Skip to content

Commit dd6efd0

Browse files
authored
Fix error message for @option array without values (#435)
1 parent 18b0039 commit dd6efd0

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Sources/ArgumentParser/Parsable Properties/Option.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ extension Option where Value: ExpressibleByArgument {
200200

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

Sources/ArgumentParser/Parsing/ArgumentSet.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ extension ArgumentSet {
356356
usedOrigins.formUnion(origin)
357357
inputArguments.removeAll(in: origin)
358358

359+
// Fix incorrect error message
360+
// for @Option array without values (see issue #434).
361+
guard let first = inputArguments.elements.first,
362+
first.isValue
363+
else {
364+
throw ParserError.missingValueForOption(origin, parsed.name)
365+
}
366+
359367
// ...and then consume the arguments until hitting an option
360368
while let (origin2, value) = inputArguments.popNextElementIfValue() {
361369
let origins = origin.inserting(origin2)

Tests/ArgumentParserUnitTests/ErrorMessageTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,27 @@ extension ErrorMessageTests {
210210
}
211211
}
212212

213+
// (see issue #434).
214+
private struct EmptyArray: ParsableArguments {
215+
@Option(parsing: .upToNextOption)
216+
var array: [String] = []
217+
218+
@Flag(name: [.short, .long])
219+
var verbose = false
220+
}
221+
222+
extension ErrorMessageTests {
223+
func testEmptyArrayOption() {
224+
AssertErrorMessage(EmptyArray.self, ["--array"], "Missing value for '--array <array>'")
225+
226+
AssertErrorMessage(EmptyArray.self, ["--array", "--verbose"], "Missing value for '--array <array>'")
227+
AssertErrorMessage(EmptyArray.self, ["-verbose", "--array"], "Missing value for '--array <array>'")
228+
229+
AssertErrorMessage(EmptyArray.self, ["--array", "-v"], "Missing value for '--array <array>'")
230+
AssertErrorMessage(EmptyArray.self, ["-v", "--array"], "Missing value for '--array <array>'")
231+
}
232+
}
233+
213234
// MARK: -
214235

215236
fileprivate struct Repeat: ParsableArguments {

0 commit comments

Comments
 (0)