From 74f3470e6d348c9400275dc6627843598c39aee4 Mon Sep 17 00:00:00 2001 From: Miguel Perez Date: Mon, 19 Apr 2021 18:40:49 -0400 Subject: [PATCH 1/2] Rebasing --- .../Parsable Properties/OptionGroup.swift | 11 +++++++ .../Parsable Types/ParsableArguments.swift | 6 +++- .../ArgumentParser/Usage/HelpGenerator.swift | 2 +- .../HelpGenerationTests.swift | 32 +++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Sources/ArgumentParser/Parsable Properties/OptionGroup.swift b/Sources/ArgumentParser/Parsable Properties/OptionGroup.swift index 65f90d3b6..80ec9641a 100644 --- a/Sources/ArgumentParser/Parsable Properties/OptionGroup.swift +++ b/Sources/ArgumentParser/Parsable Properties/OptionGroup.swift @@ -31,6 +31,7 @@ @propertyWrapper public struct OptionGroup: Decodable, ParsedWrapper { internal var _parsedValue: Parsed + internal var _hidden: Bool? internal init(_parsedValue: Parsed) { self._parsedValue = _parsedValue @@ -84,7 +85,17 @@ extension OptionGroup: CustomStringConvertible { case .value(let v): return String(describing: v) case .definition: + if let hidden = _hidden { + return "OptionGroup(*definition*) _hidden: \(hidden)" + } return "OptionGroup(*definition*)" } } } + +extension OptionGroup { + public init(_hidden: Bool) { + self.init() + self._hidden = _hidden + } +} diff --git a/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift b/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift index d4e0d0b76..7b42f0ba7 100644 --- a/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift +++ b/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift @@ -232,7 +232,7 @@ protocol ArgumentSetProvider { } extension ArgumentSet { - init(_ type: ParsableArguments.Type) { + init(_ type: ParsableArguments.Type, creatingHelp: Bool = false) { #if DEBUG do { @@ -247,6 +247,10 @@ extension ArgumentSet { .compactMap { child in guard var codingKey = child.label else { return nil } + if creatingHelp { + guard !String(describing: child.value).contains("_hidden: true") else { return nil } + } + if let parsed = child.value as? ArgumentSetProvider { // Property wrappers have underscore-prefixed names codingKey = String(codingKey.first == "_" diff --git a/Sources/ArgumentParser/Usage/HelpGenerator.swift b/Sources/ArgumentParser/Usage/HelpGenerator.swift index 9407a26e6..5984a99a8 100644 --- a/Sources/ArgumentParser/Usage/HelpGenerator.swift +++ b/Sources/ArgumentParser/Usage/HelpGenerator.swift @@ -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 { diff --git a/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift b/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift index 6f028f2fa..08527c75f 100644 --- a/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift +++ b/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift @@ -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(_hidden: 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 ] [--timeout ] + + OPTIONS: + --timeout Time to wait before timeout (in seconds) + -h, --help Show help information. + + """ + ) + } } From 6428e27105ae80eb4800d4c7432e6858885b2b66 Mon Sep 17 00:00:00 2001 From: Miguel Perez Date: Wed, 21 Apr 2021 19:59:57 -0400 Subject: [PATCH 2/2] Refinements from feedback --- .../ArgumentParser/Parsable Properties/Argument.swift | 3 ++- Sources/ArgumentParser/Parsable Properties/Flag.swift | 3 ++- .../ArgumentParser/Parsable Properties/Option.swift | 3 ++- .../Parsable Properties/OptionGroup.swift | 10 ++++------ .../Parsable Types/ParsableArguments.swift | 10 ++++++---- .../ArgumentParserUnitTests/HelpGenerationTests.swift | 2 +- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Sources/ArgumentParser/Parsable Properties/Argument.swift b/Sources/ArgumentParser/Parsable Properties/Argument.swift index dd8b120ae..f64cf8910 100644 --- a/Sources/ArgumentParser/Parsable Properties/Argument.swift +++ b/Sources/ArgumentParser/Parsable Properties/Argument.swift @@ -27,7 +27,8 @@ public struct Argument: Decodable, ParsedWrapper { internal var _parsedValue: Parsed - + internal var _hiddenFromHelp: Bool = false + internal init(_parsedValue: Parsed) { self._parsedValue = _parsedValue } diff --git a/Sources/ArgumentParser/Parsable Properties/Flag.swift b/Sources/ArgumentParser/Parsable Properties/Flag.swift index 0c61c6540..31e17084c 100644 --- a/Sources/ArgumentParser/Parsable Properties/Flag.swift +++ b/Sources/ArgumentParser/Parsable Properties/Flag.swift @@ -39,7 +39,8 @@ @propertyWrapper public struct Flag: Decodable, ParsedWrapper { internal var _parsedValue: Parsed - + internal var _hiddenFromHelp: Bool = false + internal init(_parsedValue: Parsed) { self._parsedValue = _parsedValue } diff --git a/Sources/ArgumentParser/Parsable Properties/Option.swift b/Sources/ArgumentParser/Parsable Properties/Option.swift index 72f0153af..15565cce3 100644 --- a/Sources/ArgumentParser/Parsable Properties/Option.swift +++ b/Sources/ArgumentParser/Parsable Properties/Option.swift @@ -29,7 +29,8 @@ @propertyWrapper public struct Option: Decodable, ParsedWrapper { internal var _parsedValue: Parsed - + internal var _hiddenFromHelp: Bool = false + internal init(_parsedValue: Parsed) { self._parsedValue = _parsedValue } diff --git a/Sources/ArgumentParser/Parsable Properties/OptionGroup.swift b/Sources/ArgumentParser/Parsable Properties/OptionGroup.swift index 80ec9641a..6028f62f7 100644 --- a/Sources/ArgumentParser/Parsable Properties/OptionGroup.swift +++ b/Sources/ArgumentParser/Parsable Properties/OptionGroup.swift @@ -31,7 +31,7 @@ @propertyWrapper public struct OptionGroup: Decodable, ParsedWrapper { internal var _parsedValue: Parsed - internal var _hidden: Bool? + internal var _hiddenFromHelp: Bool = false internal init(_parsedValue: Parsed) { self._parsedValue = _parsedValue @@ -85,17 +85,15 @@ extension OptionGroup: CustomStringConvertible { case .value(let v): return String(describing: v) case .definition: - if let hidden = _hidden { - return "OptionGroup(*definition*) _hidden: \(hidden)" - } return "OptionGroup(*definition*)" } } } +// Experimental use with caution extension OptionGroup { - public init(_hidden: Bool) { + public init(_hiddenFromHelp: Bool) { self.init() - self._hidden = _hidden + self._hiddenFromHelp = _hiddenFromHelp } } diff --git a/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift b/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift index 7b42f0ba7..cccdde910 100644 --- a/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift +++ b/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift @@ -229,6 +229,8 @@ 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 { @@ -247,11 +249,11 @@ extension ArgumentSet { .compactMap { child in guard var codingKey = child.label else { return nil } - if creatingHelp { - guard !String(describing: child.value).contains("_hidden: true") 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) diff --git a/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift b/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift index 08527c75f..a833ca280 100644 --- a/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift +++ b/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift @@ -485,7 +485,7 @@ extension HelpGenerationTests { struct HideDriver: ParsableCommand { static let configuration = CommandConfiguration(commandName: "driver", abstract: "Demo hiding option groups") - @OptionGroup(_hidden: true) + @OptionGroup(_hiddenFromHelp: true) var hideMe: optionsToHide @Option(help: "Time to wait before timeout (in seconds)")