Skip to content

Commit 74f3470

Browse files
author
Miguel Perez
committed
Rebasing
1 parent f314199 commit 74f3470

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
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
@@ -232,7 +232,7 @@ protocol ArgumentSetProvider {
232232
}
233233

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

237237
#if DEBUG
238238
do {
@@ -247,6 +247,10 @@ extension ArgumentSet {
247247
.compactMap { child in
248248
guard var codingKey = child.label else { return nil }
249249

250+
if creatingHelp {
251+
guard !String(describing: child.value).contains("_hidden: true") else { return nil }
252+
}
253+
250254
if let parsed = child.value as? ArgumentSetProvider {
251255
// Property wrappers have underscore-prefixed names
252256
codingKey = String(codingKey.first == "_"

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(_hidden: 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)