Skip to content
Closed
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
3 changes: 3 additions & 0 deletions Examples/math/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Sources/ArgumentParser/Parsing/CommandParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParsableCommand, CommandError> {

var split: SplitArguments
do {
split = try SplitArguments(arguments: arguments)
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions Tests/ArgumentParserExampleTests/MathExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down