Skip to content

Commit 4315849

Browse files
committed
Fix handling of visibility with nested OptionGroups.
The visibility of OptionGroups is ignored if they are nested inside another OptionGroup. Instead of trying to filter groups when creating an ArgumentSet, use the OptionGroup's visibility to modify the arguments within the group.
1 parent cc61a4c commit 4315849

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

Sources/ArgumentParser/Parsable Properties/ArgumentVisibility.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,28 @@ extension ArgumentVisibility {
5757
internal func isAtLeastAsVisible(as other: Self) -> Bool {
5858
self.base._comparableLevel >= other.base._comparableLevel
5959
}
60+
61+
/// Reduce the visibility to a specified level if it is more restricted than the current value.
62+
internal mutating func reduce(to: ArgumentVisibility) {
63+
switch to.base {
64+
case .default:
65+
break // No effect
66+
case .hidden:
67+
if case .default = self.base {
68+
self.base = .hidden
69+
}
70+
case .private:
71+
self.base = .private
72+
}
73+
}
74+
}
75+
76+
extension ArgumentDefinition {
77+
internal func reducingHelpVisibility(to visibility: ArgumentVisibility)
78+
-> Self
79+
{
80+
var result = self
81+
result.help.visibility.reduce(to: visibility)
82+
return result
83+
}
6084
}

Sources/ArgumentParser/Parsable Properties/OptionGroup.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public struct OptionGroup<Value: ParsableArguments>: Decodable, ParsedWrapper {
8888
$0.help.parentTitle = title
8989
}
9090
}
91+
args.content = args.content.map { arg in
92+
arg.reducingHelpVisibility(to: visibility)
93+
}
9194
return args
9295
})
9396
self._visibility = visibility

Sources/ArgumentParser/Parsable Types/ParsableArguments.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,6 @@ extension ArgumentSet {
316316
guard let codingKey = child.label else { return nil }
317317

318318
if let parsed = child.value as? ArgumentSetProvider {
319-
guard parsed._visibility.isAtLeastAsVisible(as: visibility)
320-
else { return nil }
321-
322319
let key = InputKey(name: codingKey, parent: parent)
323320
return parsed.argumentSet(for: key)
324321
} else {

Tests/ArgumentParserUnitTests/HelpGenerationTests+GroupName.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,43 @@ extension HelpGenerationTests {
233233
""")
234234
}
235235

236+
fileprivate struct NestedGroups: ParsableArguments {
237+
@OptionGroup(visibility: .hidden)
238+
var flagsAndOptions: FlagsAndOptions
239+
240+
@OptionGroup(visibility: .private)
241+
var argsAndFlags: ArgsAndFlags
242+
}
243+
244+
fileprivate struct NestedHiddenGroups: ParsableCommand {
245+
@OptionGroup()
246+
var nested: NestedGroups
247+
}
248+
249+
func testNestedHiddenGroups() {
250+
AssertHelp(
251+
.default, for: NestedHiddenGroups.self,
252+
equals: """
253+
USAGE: nested-hidden-groups
254+
255+
OPTIONS:
256+
-h, --help Show help information.
257+
258+
""")
259+
260+
AssertHelp(
261+
.hidden, for: NestedHiddenGroups.self,
262+
equals: """
263+
USAGE: nested-hidden-groups [--experimental] --prefix <prefix>
264+
265+
OPTIONS:
266+
--experimental example
267+
--prefix <prefix> example
268+
-h, --help Show help information.
269+
270+
""")
271+
}
272+
236273
fileprivate struct ParentWithGroups: ParsableCommand {
237274
static var configuration: CommandConfiguration {
238275
.init(subcommands: [ChildWithGroups.self])

0 commit comments

Comments
 (0)