Skip to content

Commit 22bec4a

Browse files
author
Miguel Perez
committed
Add ability to hide OptionGroup()
With test
1 parent 7478e44 commit 22bec4a

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

Sources/ArgumentParser/Parsable Properties/OptionGroup.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
@propertyWrapper
3232
public struct OptionGroup<Value: ParsableArguments>: Decodable, ParsedWrapper {
3333
internal var _parsedValue: Parsed<Value>
34+
internal var _hidden: Bool?
3435

3536
internal init(_parsedValue: Parsed<Value>) {
3637
self._parsedValue = _parsedValue
@@ -84,7 +85,17 @@ extension OptionGroup: CustomStringConvertible {
8485
case .value(let v):
8586
return String(describing: v)
8687
case .definition:
88+
if let hidden = _hidden {
89+
return "OptionGroup(*definition*) _hidden: \(hidden)"
90+
}
8791
return "OptionGroup(*definition*)"
8892
}
8993
}
9094
}
95+
96+
extension OptionGroup {
97+
public init(_hidden: Bool) {
98+
self.init()
99+
self._hidden = _hidden
100+
}
101+
}

Sources/ArgumentParser/Parsable Types/ParsableArguments.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ protocol ArgumentSetProvider {
213213
}
214214

215215
extension ArgumentSet {
216-
init(_ type: ParsableArguments.Type) {
216+
init(_ type: ParsableArguments.Type, creatingHelp: Bool = false) {
217217

218218
#if DEBUG
219219
do {
@@ -228,6 +228,10 @@ extension ArgumentSet {
228228
.compactMap { child in
229229
guard var codingKey = child.label else { return nil }
230230

231+
if creatingHelp {
232+
guard !String(describing: child.value).contains("_hidden: true") else { return nil }
233+
}
234+
231235
if let parsed = child.value as? ArgumentSetProvider {
232236
// Property wrappers have underscore-prefixed names
233237
codingKey = String(codingKey.first == "_"

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ internal struct HelpGenerator {
147147

148148
var commandsToShowHelp = [ParsableCommand.Type]()
149149
if let commandType = commandStack.last, !commandType.includeSuperCommandInHelp {
150-
commandsToShowHelp.append(commandType)
150+
commandsToShowHelp.append(commandType)
151151
} else {
152-
commandsToShowHelp = commandStack
152+
commandsToShowHelp = commandStack
153153
}
154154

155155
for commandType in commandsToShowHelp {
156-
let args = Array(ArgumentSet(commandType))
156+
let args = Array(ArgumentSet(commandType, creatingHelp: true))
157157

158158
var i = 0
159159
while i < args.count {

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,36 @@ extension HelpGenerationTests {
475475
476476
""")
477477
}
478+
479+
struct optionsToHide: ParsableArguments {
480+
@Flag(help: "Verbose")
481+
var verbose: Bool = false
482+
483+
@Option(help: "Custom Name")
484+
var customName: String?
485+
}
486+
487+
struct HideDriver: ParsableCommand {
488+
static let configuration = CommandConfiguration(commandName: "driver", abstract: "Demo hiding option groups")
489+
490+
@OptionGroup(_hidden: true)
491+
var hideMe: optionsToHide
492+
493+
@Option(help: "Time to wait before timeout (in seconds)")
494+
var timeout: Int?
495+
}
496+
497+
func testHidingOptionGroup() throws {
498+
AssertHelp(for: HideDriver.self, equals: """
499+
OVERVIEW: Demo hiding option groups
500+
501+
USAGE: driver [--verbose] [--custom-name <custom-name>] [--timeout <timeout>]
502+
503+
OPTIONS:
504+
--timeout <timeout> Time to wait before timeout (in seconds)
505+
-h, --help Show help information.
506+
507+
"""
508+
)
509+
}
478510
}

0 commit comments

Comments
 (0)