Skip to content

Fix parsing for single-dash names with .upToNextOption parsing #353

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

Merged
merged 3 commits into from
Sep 10, 2021
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
7 changes: 7 additions & 0 deletions Sources/ArgumentParser/Parsing/ArgumentSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ extension ArgumentSet {
if let value = parsed.value {
// This was `--foo=bar` style:
try update(origin, parsed.name, value, &result)
usedOrigins.formUnion(origin)
} else if argument.allowsJoinedValue,
let (origin2, value) = inputArguments.extractJoinedElement(at: originElement)
{
Expand All @@ -243,6 +244,7 @@ extension ArgumentSet {
if let value = parsed.value {
// This was `--foo=bar` style:
try update(origin, parsed.name, value, &result)
usedOrigins.formUnion(origin)
} else if argument.allowsJoinedValue,
let (origin2, value) = inputArguments.extractJoinedElement(at: originElement) {
// Found a joined argument
Expand Down Expand Up @@ -320,6 +322,11 @@ extension ArgumentSet {
inputArguments.removeAll(in: usedOrigins)
}

// Clear out the initial origin first, since it can include
// the exploded elements of an options group (see issue #327).
usedOrigins.formUnion(origin)
inputArguments.removeAll(in: origin)

// ...and then consume the arguments until hitting an option
while let (origin2, value) = inputArguments.popNextElementIfValue() {
let origins = origin.inserting(origin2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,28 @@ extension LongNameWithSingleDashEndToEndTests {
XCTAssertThrowsError(try Bar.parse(["--file"]))
}
}

extension LongNameWithSingleDashEndToEndTests {
private struct Issue327: ParsableCommand {
@Option(name: .customLong("argWithAnH", withSingleDash: true),
parsing: .upToNextOption)
var args: [String]
}

func testIssue327() {
AssertParse(Issue327.self, ["-argWithAnH", "03ade86c0", "8f2058e3ade86c84ec5b"]) { issue327 in
XCTAssertEqual(issue327.args, ["03ade86c0", "8f2058e3ade86c84ec5b"])
}
}

private struct JoinedItem: ParsableCommand {
@Option(name: .customLong("argWithAnH", withSingleDash: true))
var arg: String
}

func testJoinedItem_Issue327() {
AssertParse(JoinedItem.self, ["-argWithAnH=foo"]) { joinedItem in
XCTAssertEqual(joinedItem.arg, "foo")
}
}
}