Skip to content

Hide option group with new OptionGroup constructor #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/ArgumentParser/Parsable Properties/Argument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public struct Argument<Value>:
Decodable, ParsedWrapper
{
internal var _parsedValue: Parsed<Value>

internal var _hiddenFromHelp: Bool = false

internal init(_parsedValue: Parsed<Value>) {
self._parsedValue = _parsedValue
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/ArgumentParser/Parsable Properties/Flag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
@propertyWrapper
public struct Flag<Value>: Decodable, ParsedWrapper {
internal var _parsedValue: Parsed<Value>

internal var _hiddenFromHelp: Bool = false

internal init(_parsedValue: Parsed<Value>) {
self._parsedValue = _parsedValue
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/ArgumentParser/Parsable Properties/Option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
@propertyWrapper
public struct Option<Value>: Decodable, ParsedWrapper {
internal var _parsedValue: Parsed<Value>

internal var _hiddenFromHelp: Bool = false

internal init(_parsedValue: Parsed<Value>) {
self._parsedValue = _parsedValue
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/ArgumentParser/Parsable Properties/OptionGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@propertyWrapper
public struct OptionGroup<Value: ParsableArguments>: Decodable, ParsedWrapper {
internal var _parsedValue: Parsed<Value>
internal var _hiddenFromHelp: Bool = false

internal init(_parsedValue: Parsed<Value>) {
self._parsedValue = _parsedValue
Expand Down Expand Up @@ -88,3 +89,11 @@ extension OptionGroup: CustomStringConvertible {
}
}
}

// Experimental use with caution
extension OptionGroup {
public init(_hiddenFromHelp: Bool) {
self.init()
self._hiddenFromHelp = _hiddenFromHelp
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ func nilOrValue(_ value: Any) -> Any? {
/// the argument set that they define.
protocol ArgumentSetProvider {
func argumentSet(for key: InputKey) -> ArgumentSet

var _hiddenFromHelp: Bool { get }
}

extension ArgumentSet {
init(_ type: ParsableArguments.Type) {
init(_ type: ParsableArguments.Type, creatingHelp: Bool = false) {

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

if let parsed = child.value as? ArgumentSetProvider {
if creatingHelp {
guard !parsed._hiddenFromHelp else { return nil }
}

// Property wrappers have underscore-prefixed names
codingKey = String(codingKey.first == "_"
? codingKey.dropFirst(1)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ArgumentParser/Usage/HelpGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ internal struct HelpGenerator {
return []
}

let args = Array(ArgumentSet(commandType))
let args = Array(ArgumentSet(commandType, creatingHelp: true))

var i = 0
while i < args.count {
Expand Down
32 changes: 32 additions & 0 deletions Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,36 @@ extension HelpGenerationTests {

""")
}

struct optionsToHide: ParsableArguments {
@Flag(help: "Verbose")
var verbose: Bool = false

@Option(help: "Custom Name")
var customName: String?
}

struct HideDriver: ParsableCommand {
static let configuration = CommandConfiguration(commandName: "driver", abstract: "Demo hiding option groups")

@OptionGroup(_hiddenFromHelp: true)
var hideMe: optionsToHide

@Option(help: "Time to wait before timeout (in seconds)")
var timeout: Int?
}

func testHidingOptionGroup() throws {
AssertHelp(for: HideDriver.self, equals: """
OVERVIEW: Demo hiding option groups

USAGE: driver [--verbose] [--custom-name <custom-name>] [--timeout <timeout>]

OPTIONS:
--timeout <timeout> Time to wait before timeout (in seconds)
-h, --help Show help information.

"""
)
}
}