Skip to content

Commit 26905bf

Browse files
author
Miguel Perez
committed
Ability to exclude super commands from --help
1 parent b936799 commit 26905bf

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Sources/ArgumentParser/Parsable Types/CommandConfiguration.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public struct CommandConfiguration {
4747
/// Flag names to be used for help.
4848
public var helpNames: NameSpecification?
4949

50+
public var includeSuperCommandInHelp: Bool?
51+
5052
/// Creates the configuration for a command.
5153
///
5254
/// - Parameters:
@@ -76,7 +78,8 @@ public struct CommandConfiguration {
7678
shouldDisplay: Bool = true,
7779
subcommands: [ParsableCommand.Type] = [],
7880
defaultSubcommand: ParsableCommand.Type? = nil,
79-
helpNames: NameSpecification? = nil
81+
helpNames: NameSpecification? = nil,
82+
includeSuperCommandInHelp: Bool? = nil
8083
) {
8184
self.commandName = commandName
8285
self.abstract = abstract
@@ -86,6 +89,7 @@ public struct CommandConfiguration {
8689
self.subcommands = subcommands
8790
self.defaultSubcommand = defaultSubcommand
8891
self.helpNames = helpNames
92+
self.includeSuperCommandInHelp = includeSuperCommandInHelp
8993
}
9094

9195
/// Creates the configuration for a command with a "super-command".
@@ -99,7 +103,8 @@ public struct CommandConfiguration {
99103
shouldDisplay: Bool = true,
100104
subcommands: [ParsableCommand.Type] = [],
101105
defaultSubcommand: ParsableCommand.Type? = nil,
102-
helpNames: NameSpecification? = nil
106+
helpNames: NameSpecification? = nil,
107+
includeSuperCommandInHelp: Bool? = nil
103108
) {
104109
self.commandName = commandName
105110
self._superCommandName = _superCommandName
@@ -110,5 +115,6 @@ public struct CommandConfiguration {
110115
self.subcommands = subcommands
111116
self.defaultSubcommand = defaultSubcommand
112117
self.helpNames = helpNames
118+
self.includeSuperCommandInHelp = includeSuperCommandInHelp
113119
}
114120
}

Sources/ArgumentParser/Parsable Types/ParsableCommand.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public protocol ParsableCommand: ParsableArguments {
2222
/// can pass through the wrapped type's name.
2323
static var _commandName: String { get }
2424

25+
static var includeSuperCommandInHelp: Bool { get }
2526
/// Runs this command.
2627
///
2728
/// After implementing this method, you can run your command-line
@@ -41,6 +42,10 @@ extension ParsableCommand {
4142
CommandConfiguration()
4243
}
4344

45+
public static var includeSuperCommandInHelp: Bool {
46+
configuration.includeSuperCommandInHelp ?? true
47+
}
48+
4449
public mutating func run() throws {
4550
throw CleanExit.helpRequest(self)
4651
}

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,16 @@ internal struct HelpGenerator {
144144
var optionElements: [Section.Element] = []
145145
/// Used to keep track of elements already seen from parent commands.
146146
var alreadySeenElements = Set<Section.Element>()
147-
147+
148+
var commandsToShowHelp = [ParsableCommand.Type]()
148149
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)
154+
}
155+
156+
for commandType in commandsToShowHelp {
149157
let args = Array(ArgumentSet(commandType))
150158

151159
var i = 0

0 commit comments

Comments
 (0)