Skip to content

Don't trigger help for commands w/ unconditional remaining #417

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 1 commit into from
Mar 12, 2022
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
18 changes: 14 additions & 4 deletions Sources/ArgumentParser/Parsing/CommandParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,19 @@ extension CommandParser {
return subcommandNode
}

/// Throws a `HelpRequested` error if the user has specified either of the
/// built in help flags.
func checkForBuiltInFlags(_ split: SplitArguments) throws {
/// Throws a `HelpRequested` error if the user has specified any of the
/// built-in flags.
///
/// - Parameters:
/// - split: The remaining arguments to examine.
/// - requireSoloArgument: `true` if the built-in flag must be the only
/// one remaining for this to catch it.
func checkForBuiltInFlags(
_ split: SplitArguments,
requireSoloArgument: Bool = false
) throws {
guard !requireSoloArgument || split.count == 1 else { return }

// Look for help flags
guard !split.contains(anyOf: self.commandStack.getHelpNames(visibility: .default)) else {
throw HelpRequested(visibility: .default)
Expand Down Expand Up @@ -187,7 +197,7 @@ extension CommandParser {
}

// Look for the help flag before falling back to a default command.
try checkForBuiltInFlags(split)
try checkForBuiltInFlags(split, requireSoloArgument: true)

// No command was found, so fall back to the default subcommand.
if let defaultSubcommand = currentNode.element.configuration.defaultSubcommand {
Expand Down
4 changes: 4 additions & 0 deletions Sources/ArgumentParser/Parsing/SplitArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ struct SplitArguments {
var elements: ArraySlice<Element> {
_elements[firstUnused...]
}

var count: Int {
elements.count
}
}

extension SplitArguments.Element: CustomDebugStringConvertible {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ extension DefaultSubcommandEndToEndTests {
XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
XCTAssertEqual(plugin.options.verbose, true)
}
AssertParseCommand(MyCommand.self, Plugin.self, ["my-plugin", "--help"]) { plugin in
XCTAssertEqual(plugin.pluginName, "my-plugin")
XCTAssertEqual(plugin.pluginArguments, ["--help"])
XCTAssertEqual(plugin.options.verbose, false)
}
}

func testRemainingDefaultExplicit() throws {
Expand All @@ -141,6 +146,11 @@ extension DefaultSubcommandEndToEndTests {
XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
XCTAssertEqual(plugin.options.verbose, true)
}
AssertParseCommand(MyCommand.self, Plugin.self, ["--verbose", "plugin", "my-plugin", "--help"]) { plugin in
XCTAssertEqual(plugin.pluginName, "my-plugin")
XCTAssertEqual(plugin.pluginArguments, ["--help"])
XCTAssertEqual(plugin.options.verbose, true)
}
}

func testRemainingNonDefault() throws {
Expand All @@ -159,6 +169,11 @@ extension DefaultSubcommandEndToEndTests {
XCTAssertEqual(nondef.pluginArguments, ["--verbose"])
XCTAssertEqual(nondef.options.verbose, true)
}
AssertParseCommand(MyCommand.self, NonDefault.self, ["--verbose", "non-default", "my-plugin", "--help"]) { nondef in
XCTAssertEqual(nondef.pluginName, "my-plugin")
XCTAssertEqual(nondef.pluginArguments, ["--help"])
XCTAssertEqual(nondef.options.verbose, true)
}
}

func testRemainingDefaultOther() throws {
Expand Down