Skip to content

Commit 6c7ec36

Browse files
authored
Fix unrecognized -help flag with default command (#612)
When there's a default subcommand that captures pass-through args, only a standalone help flag should trigger help (since the flag should otherwise go to the subcommand). This fix makes that true for single- dash help flag names, which were previously being skipped, due to single-dash flags being lexed as both one whole flag and as a group of individual short flags. Re: swiftlang/swift-package-manager#7218 Re: rdar://120422808
1 parent 40d2342 commit 6c7ec36

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Sources/ArgumentParser/Parsing/CommandParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ extension CommandParser {
8484
_ split: SplitArguments,
8585
requireSoloArgument: Bool = false
8686
) throws {
87-
guard !requireSoloArgument || split.count == 1 else { return }
87+
guard !requireSoloArgument || split.originalInput.count == 1 else { return }
8888

8989
// Look for help flags
9090
guard !split.contains(anyOf: self.commandStack.getHelpNames(visibility: .default)) else {

Tests/ArgumentParserEndToEndTests/DefaultSubcommandEndToEndTests.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import XCTest
1313
import ArgumentParserTestHelpers
14-
import ArgumentParser
14+
@testable import ArgumentParser
1515

1616
final class DefaultSubcommandEndToEndTests: XCTestCase {
1717
}
@@ -191,3 +191,28 @@ extension DefaultSubcommandEndToEndTests {
191191
XCTAssertThrowsError(try MyCommand.parseAsRoot(["plugin", "--verbose", "my-plugin"]))
192192
}
193193
}
194+
195+
extension DefaultSubcommandEndToEndTests {
196+
struct RootWithPassthroughDefault: ParsableCommand {
197+
static let configuration = CommandConfiguration(
198+
subcommands: [PassthroughDefault.self],
199+
defaultSubcommand: PassthroughDefault.self,
200+
helpNames: [.short, .long, .customLong("help", withSingleDash: true)]
201+
)
202+
}
203+
204+
struct PassthroughDefault: ParsableCommand {
205+
@Argument(parsing: .captureForPassthrough)
206+
var remaining: [String] = []
207+
}
208+
209+
// Test fix for https://github.com/apple/swift-package-manager/issues/7218
210+
func testHelpWithPassthroughDefault() throws {
211+
AssertParseCommand(
212+
RootWithPassthroughDefault.self, HelpCommand.self, ["-h"]) { _ in }
213+
AssertParseCommand(
214+
RootWithPassthroughDefault.self, HelpCommand.self, ["-help"]) { _ in }
215+
AssertParseCommand(
216+
RootWithPassthroughDefault.self, HelpCommand.self, ["--help"]) { _ in }
217+
}
218+
}

0 commit comments

Comments
 (0)