Skip to content

Commit ee32b80

Browse files
authored
Hide option group with new OptionGroup constructor (#301)
1 parent 47bd06e commit ee32b80

File tree

7 files changed

+55
-5
lines changed

7 files changed

+55
-5
lines changed

Sources/ArgumentParser/Parsable Properties/Argument.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public struct Argument<Value>:
2727
Decodable, ParsedWrapper
2828
{
2929
internal var _parsedValue: Parsed<Value>
30-
30+
internal var _hiddenFromHelp: Bool = false
31+
3132
internal init(_parsedValue: Parsed<Value>) {
3233
self._parsedValue = _parsedValue
3334
}

Sources/ArgumentParser/Parsable Properties/Flag.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
@propertyWrapper
4040
public struct Flag<Value>: Decodable, ParsedWrapper {
4141
internal var _parsedValue: Parsed<Value>
42-
42+
internal var _hiddenFromHelp: Bool = false
43+
4344
internal init(_parsedValue: Parsed<Value>) {
4445
self._parsedValue = _parsedValue
4546
}

Sources/ArgumentParser/Parsable Properties/Option.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
@propertyWrapper
3030
public struct Option<Value>: Decodable, ParsedWrapper {
3131
internal var _parsedValue: Parsed<Value>
32-
32+
internal var _hiddenFromHelp: Bool = false
33+
3334
internal init(_parsedValue: Parsed<Value>) {
3435
self._parsedValue = _parsedValue
3536
}

Sources/ArgumentParser/Parsable Properties/OptionGroup.swift

Lines changed: 9 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 _hiddenFromHelp: Bool = false
3435

3536
internal init(_parsedValue: Parsed<Value>) {
3637
self._parsedValue = _parsedValue
@@ -88,3 +89,11 @@ extension OptionGroup: CustomStringConvertible {
8889
}
8990
}
9091
}
92+
93+
// Experimental use with caution
94+
extension OptionGroup {
95+
public init(_hiddenFromHelp: Bool) {
96+
self.init()
97+
self._hiddenFromHelp = _hiddenFromHelp
98+
}
99+
}

Sources/ArgumentParser/Parsable Types/ParsableArguments.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,12 @@ func nilOrValue(_ value: Any) -> Any? {
229229
/// the argument set that they define.
230230
protocol ArgumentSetProvider {
231231
func argumentSet(for key: InputKey) -> ArgumentSet
232+
233+
var _hiddenFromHelp: Bool { get }
232234
}
233235

234236
extension ArgumentSet {
235-
init(_ type: ParsableArguments.Type) {
237+
init(_ type: ParsableArguments.Type, creatingHelp: Bool = false) {
236238

237239
#if DEBUG
238240
do {
@@ -248,6 +250,10 @@ extension ArgumentSet {
248250
guard var codingKey = child.label else { return nil }
249251

250252
if let parsed = child.value as? ArgumentSetProvider {
253+
if creatingHelp {
254+
guard !parsed._hiddenFromHelp else { return nil }
255+
}
256+
251257
// Property wrappers have underscore-prefixed names
252258
codingKey = String(codingKey.first == "_"
253259
? codingKey.dropFirst(1)

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ internal struct HelpGenerator {
149149
return []
150150
}
151151

152-
let args = Array(ArgumentSet(commandType))
152+
let args = Array(ArgumentSet(commandType, creatingHelp: true))
153153

154154
var i = 0
155155
while i < args.count {

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

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

0 commit comments

Comments
 (0)