Skip to content

Commit 7478e44

Browse files
author
Miguel Perez
committed
Added test and removed flag search loop
1 parent adcc366 commit 7478e44

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

Sources/ArgumentParser/Parsable Types/CommandConfiguration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public struct CommandConfiguration {
7171
/// with a simulated Boolean property named `help`. If `helpNames` is
7272
/// `nil`, the names are inherited from the parent command, if any, or
7373
/// `-h` and `--help`.
74+
/// - includeSuperCommandInHelp: When set to false the super command will not be included in
75+
/// the help printout.
7476
public init(
7577
commandName: String? = nil,
7678
abstract: String = "",

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,10 @@ internal struct HelpGenerator {
146146
var alreadySeenElements = Set<Section.Element>()
147147

148148
var commandsToShowHelp = [ParsableCommand.Type]()
149-
for commandType in commandStack {
150-
// This will remove all super commands in the event that a subcommand does not
151-
// want to include their respective help
152-
if !commandType.includeSuperCommandInHelp { commandsToShowHelp.removeAll() }
153-
commandsToShowHelp.append(commandType)
149+
if let commandType = commandStack.last, !commandType.includeSuperCommandInHelp {
150+
commandsToShowHelp.append(commandType)
151+
} else {
152+
commandsToShowHelp = commandStack
154153
}
155154

156155
for commandType in commandsToShowHelp {

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,48 @@ extension HelpGenerationTests {
431431
432432
""")
433433
}
434+
435+
struct Foo: ParsableCommand {
436+
public static var configuration = CommandConfiguration(
437+
commandName: "foo",
438+
abstract: "Perform some foo",
439+
subcommands: [
440+
Bar.self
441+
],
442+
helpNames: [.short, .long, .customLong("help", withSingleDash: true)])
443+
444+
@Option(help: "Name for foo")
445+
var fooName: String?
446+
447+
public init() {}
448+
}
449+
450+
struct Bar: ParsableCommand {
451+
static let configuration = CommandConfiguration(
452+
commandName: "bar",
453+
_superCommandName: "foo",
454+
abstract: "Perform bar operations",
455+
helpNames: [.short, .long, .customLong("help", withSingleDash: true)])
456+
457+
static let includeSuperCommandInHelp: Bool = false
458+
459+
@Option(help: "Bar Strength")
460+
var barStrength: String?
461+
462+
public init() {}
463+
}
464+
465+
func testHelpExcludingSuperCommand() throws {
466+
AssertHelp(for: Bar.self, root: Foo.self, equals: """
467+
OVERVIEW: Perform bar operations
468+
469+
USAGE: foo bar [--bar-strength <bar-strength>]
470+
471+
OPTIONS:
472+
--bar-strength <bar-strength>
473+
Bar Strength
474+
-help, -h, --help Show help information.
475+
476+
""")
477+
}
434478
}

0 commit comments

Comments
 (0)