Skip to content

Commit 02a95bd

Browse files
Help message for default subcommands (#183)
* Update label for default subcommand * Add test for default subcommand * Fix test fixtures with default subcommand help message
1 parent 59f39ec commit 02a95bd

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,16 @@ internal struct HelpGenerator {
213213
optionElements.append(.init(label: helpLabels, abstract: "Show help information."))
214214
}
215215

216+
let configuration = commandStack.last!.configuration
216217
let subcommandElements: [Section.Element] =
217-
commandStack.last!.configuration.subcommands.compactMap { command in
218+
configuration.subcommands.compactMap { command in
218219
guard command.configuration.shouldDisplay else { return nil }
220+
var label = command._commandName
221+
if command == configuration.defaultSubcommand {
222+
label += " (default)"
223+
}
219224
return Section.Element(
220-
label: command._commandName,
225+
label: label,
221226
abstract: command.configuration.abstract)
222227
}
223228

Tests/ArgumentParserExampleTests/MathExampleTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class MathExampleTests: XCTestCase {
3030
-h, --help Show help information.
3131
3232
SUBCOMMANDS:
33-
add Print the sum of the values.
33+
add (default) Print the sum of the values.
3434
multiply Print the product of the values.
3535
stats Calculate descriptive statistics.
3636

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,24 @@ extension HelpGenerationTests {
377377
378378
""")
379379
}
380+
381+
struct M: ParsableCommand {
382+
}
383+
struct N: ParsableCommand {
384+
static var configuration = CommandConfiguration(subcommands: [M.self], defaultSubcommand: M.self)
385+
}
386+
387+
func testHelpWithDefaultCommand() {
388+
AssertHelp(for: N.self, equals: """
389+
USAGE: n <subcommand>
390+
391+
OPTIONS:
392+
-h, --help Show help information.
393+
394+
SUBCOMMANDS:
395+
m (default)
396+
397+
See 'n help <subcommand>' for detailed help.
398+
""")
399+
}
380400
}

0 commit comments

Comments
 (0)