Skip to content

Commit b77ea81

Browse files
authored
Fix parsing for single-dash names with .upToNextOption parsing (#353)
* Correctly track used input origins for single-dash options When capturing the values for an option with a single-dash with the .upToNextOption parsing strategy, the parser was stopping its search for values when it encountered the "unpacked" short option candidates. This change removes the single-dash option before looking for values, which strips those short options (e.g. -h) from consideration. Fixes #327.
1 parent 397d704 commit b77ea81

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Sources/ArgumentParser/Parsing/ArgumentSet.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ extension ArgumentSet {
221221
if let value = parsed.value {
222222
// This was `--foo=bar` style:
223223
try update(origin, parsed.name, value, &result)
224+
usedOrigins.formUnion(origin)
224225
} else if argument.allowsJoinedValue,
225226
let (origin2, value) = inputArguments.extractJoinedElement(at: originElement)
226227
{
@@ -243,6 +244,7 @@ extension ArgumentSet {
243244
if let value = parsed.value {
244245
// This was `--foo=bar` style:
245246
try update(origin, parsed.name, value, &result)
247+
usedOrigins.formUnion(origin)
246248
} else if argument.allowsJoinedValue,
247249
let (origin2, value) = inputArguments.extractJoinedElement(at: originElement) {
248250
// Found a joined argument
@@ -320,6 +322,11 @@ extension ArgumentSet {
320322
inputArguments.removeAll(in: usedOrigins)
321323
}
322324

325+
// Clear out the initial origin first, since it can include
326+
// the exploded elements of an options group (see issue #327).
327+
usedOrigins.formUnion(origin)
328+
inputArguments.removeAll(in: origin)
329+
323330
// ...and then consume the arguments until hitting an option
324331
while let (origin2, value) = inputArguments.popNextElementIfValue() {
325332
let origins = origin.inserting(origin2)

Tests/ArgumentParserEndToEndTests/LongNameWithShortDashEndToEndTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,28 @@ extension LongNameWithSingleDashEndToEndTests {
107107
XCTAssertThrowsError(try Bar.parse(["--file"]))
108108
}
109109
}
110+
111+
extension LongNameWithSingleDashEndToEndTests {
112+
private struct Issue327: ParsableCommand {
113+
@Option(name: .customLong("argWithAnH", withSingleDash: true),
114+
parsing: .upToNextOption)
115+
var args: [String]
116+
}
117+
118+
func testIssue327() {
119+
AssertParse(Issue327.self, ["-argWithAnH", "03ade86c0", "8f2058e3ade86c84ec5b"]) { issue327 in
120+
XCTAssertEqual(issue327.args, ["03ade86c0", "8f2058e3ade86c84ec5b"])
121+
}
122+
}
123+
124+
private struct JoinedItem: ParsableCommand {
125+
@Option(name: .customLong("argWithAnH", withSingleDash: true))
126+
var arg: String
127+
}
128+
129+
func testJoinedItem_Issue327() {
130+
AssertParse(JoinedItem.self, ["-argWithAnH=foo"]) { joinedItem in
131+
XCTAssertEqual(joinedItem.arg, "foo")
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)