diff --git a/Examples/math/main.swift b/Examples/math/main.swift index d4c191e19..b6bf00442 100644 --- a/Examples/math/main.swift +++ b/Examples/math/main.swift @@ -20,6 +20,9 @@ struct Math: ParsableCommand { // Commands can define a version for automatic '--version' support. version: "1.0.0", + + // Show help on no input. + defaultToHelp: true, // Pass an array to `subcommands` to set up a nested tree of subcommands. // With language support for type-level introspection, this could be diff --git a/Sources/ArgumentParser/Parsable Types/CommandConfiguration.swift b/Sources/ArgumentParser/Parsable Types/CommandConfiguration.swift index 78af8bcb8..6be00bf90 100644 --- a/Sources/ArgumentParser/Parsable Types/CommandConfiguration.swift +++ b/Sources/ArgumentParser/Parsable Types/CommandConfiguration.swift @@ -30,6 +30,10 @@ public struct CommandConfiguration { /// A Boolean value indicating whether this command should be shown in /// the extended help display. public var shouldDisplay: Bool + + /// A Boolean value indicating whether this command should display help + /// when executed without further user input. + public var defaultToHelp: Bool /// An array of the types that define subcommands for this command. public var subcommands: [ParsableCommand.Type] @@ -51,6 +55,8 @@ public struct CommandConfiguration { /// - version: The version number for this command. When you provide a /// non-empty string, the arguemnt parser prints it if the user provides /// a `--version` flag. + /// - defaultToHelp: A Boolean value indicating whether the command should + /// display help in the absence of other user input. /// - shouldDisplay: A Boolean value indicating whether the command /// should be shown in the extended help display. /// - subcommands: An array of the types that define subcommands for the @@ -65,6 +71,7 @@ public struct CommandConfiguration { discussion: String = "", version: String = "", shouldDisplay: Bool = true, + defaultToHelp: Bool = false, subcommands: [ParsableCommand.Type] = [], defaultSubcommand: ParsableCommand.Type? = nil, helpNames: NameSpecification = [.short, .long] @@ -74,6 +81,7 @@ public struct CommandConfiguration { self.discussion = discussion self.version = version self.shouldDisplay = shouldDisplay + self.defaultToHelp = defaultToHelp self.subcommands = subcommands self.defaultSubcommand = defaultSubcommand self.helpNames = helpNames diff --git a/Sources/ArgumentParser/Parsing/CommandParser.swift b/Sources/ArgumentParser/Parsing/CommandParser.swift index cf1ebea02..d5a619bc0 100644 --- a/Sources/ArgumentParser/Parsing/CommandParser.swift +++ b/Sources/ArgumentParser/Parsing/CommandParser.swift @@ -191,6 +191,7 @@ extension CommandParser { /// - Parameter arguments: The array of arguments to parse. This should not /// include the command name as the first argument. mutating func parse(arguments: [String]) -> Result { + var split: SplitArguments do { split = try SplitArguments(arguments: arguments) @@ -201,6 +202,10 @@ extension CommandParser { } do { + if arguments.isEmpty && commandStack.contains(where: { $0.configuration.defaultToHelp }) { + throw HelpRequested() + } + try descendingParse(&split) let result = try extractLastParsedValue(split) diff --git a/Tests/ArgumentParserExampleTests/MathExampleTests.swift b/Tests/ArgumentParserExampleTests/MathExampleTests.swift index e171ff61a..354a7ce5d 100644 --- a/Tests/ArgumentParserExampleTests/MathExampleTests.swift +++ b/Tests/ArgumentParserExampleTests/MathExampleTests.swift @@ -38,6 +38,7 @@ final class MathExampleTests: XCTestCase { AssertExecuteCommand(command: "math -h", expected: helpText) AssertExecuteCommand(command: "math --help", expected: helpText) AssertExecuteCommand(command: "math help", expected: helpText) + AssertExecuteCommand(command: "math", expected: helpText) } func testMath_AddHelp() throws {