Skip to content

Commit d7a5a79

Browse files
authored
Make @OptionGroup(visibility:) a public API (apple#419)
1 parent 1141ed1 commit d7a5a79

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

Sources/ArgumentParser/Parsable Properties/OptionGroup.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ public struct OptionGroup<Value: ParsableArguments>: Decodable, ParsedWrapper {
6161
}
6262
}
6363

64-
/// Creates a property that represents another parsable type.
65-
public init() {
64+
/// Creates a property that represents another parsable type, using the
65+
/// specified visibility.
66+
public init(visibility: ArgumentVisibility = .default) {
6667
self.init(_parsedValue: .init { _ in
6768
ArgumentSet(Value.self, visibility: .private)
6869
})
70+
self._visibility = visibility
6971
}
7072

7173
/// The value presented by this property wrapper.
@@ -97,14 +99,15 @@ extension OptionGroup: CustomStringConvertible {
9799

98100
// Experimental use with caution
99101
extension OptionGroup {
100-
@available(*, deprecated, message: "Use init(_visibility:) instead.")
102+
@available(*, deprecated, renamed: "init(visibility:)")
101103
public init(_hiddenFromHelp: Bool) {
102-
self.init()
103-
self._visibility = .hidden
104+
self.init(visibility: .hidden)
104105
}
105-
106-
public init(_visibility: ArgumentVisibility) {
107-
self.init()
108-
self._visibility = _visibility
106+
107+
/// Creates a property that represents another parsable type.
108+
@available(*, deprecated, renamed: "init(visibility:)")
109+
@_disfavoredOverload
110+
public init() {
111+
self.init(visibility: .default)
109112
}
110113
}

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -491,17 +491,25 @@ extension HelpGenerationTests {
491491
492492
""")
493493
}
494-
495-
struct optionsToHide: ParsableArguments {
494+
}
495+
496+
extension HelpGenerationTests {
497+
private struct optionsToHide: ParsableArguments {
496498
@Flag(help: "Verbose")
497499
var verbose: Bool = false
498500

499501
@Option(help: "Custom Name")
500502
var customName: String?
503+
504+
@Option(help: .hidden)
505+
var hiddenOption: String?
506+
507+
@Argument(help: .private)
508+
var privateArg: String?
501509
}
502510

503511
@available(*, deprecated)
504-
struct HideOptionGroupLegacyDriver: ParsableCommand {
512+
private struct HideOptionGroupLegacyDriver: ParsableCommand {
505513
static let configuration = CommandConfiguration(commandName: "driver", abstract: "Demo hiding option groups")
506514

507515
@OptionGroup(_hiddenFromHelp: true)
@@ -511,51 +519,72 @@ extension HelpGenerationTests {
511519
var timeout: Int?
512520
}
513521

514-
struct HideOptionGroupDriver: ParsableCommand {
522+
private struct HideOptionGroupDriver: ParsableCommand {
515523
static let configuration = CommandConfiguration(commandName: "driver", abstract: "Demo hiding option groups")
516524

517-
@OptionGroup(_visibility: .hidden)
525+
@OptionGroup(visibility: .hidden)
518526
var hideMe: optionsToHide
519527

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

524-
@available(*, deprecated)
525-
func testHidingOptionGroup() throws {
526-
let helpMessage = """
527-
OVERVIEW: Demo hiding option groups
532+
private struct PrivateOptionGroupDriver: ParsableCommand {
533+
static let configuration = CommandConfiguration(commandName: "driver", abstract: "Demo hiding option groups")
528534

529-
USAGE: driver [--timeout <timeout>]
535+
@OptionGroup(visibility: .private)
536+
var hideMe: optionsToHide
530537

531-
OPTIONS:
532-
--timeout <timeout> Time to wait before timeout (in seconds)
533-
-h, --help Show help information.
538+
@Option(help: "Time to wait before timeout (in seconds)")
539+
var timeout: Int?
540+
}
534541

535-
"""
536-
AssertHelp(.default, for: HideOptionGroupLegacyDriver.self, equals: helpMessage)
537-
AssertHelp(.default, for: HideOptionGroupDriver.self, equals: helpMessage)
542+
private var helpMessage: String { """
543+
OVERVIEW: Demo hiding option groups
544+
545+
USAGE: driver [--timeout <timeout>]
546+
547+
OPTIONS:
548+
--timeout <timeout> Time to wait before timeout (in seconds)
549+
-h, --help Show help information.
550+
551+
"""
538552
}
539553

540-
@available(*, deprecated)
541-
func testHelpHiddenShowsAll() throws {
542-
let helpHiddenMessage = """
543-
OVERVIEW: Demo hiding option groups
554+
private var helpHiddenMessage: String { """
555+
OVERVIEW: Demo hiding option groups
556+
557+
USAGE: driver [--verbose] [--custom-name <custom-name>] [--hidden-option <hidden-option>] [--timeout <timeout>]
544558
545-
USAGE: driver [--verbose] [--custom-name <custom-name>] [--timeout <timeout>]
559+
OPTIONS:
560+
--verbose Verbose
561+
--custom-name <custom-name>
562+
Custom Name
563+
--hidden-option <hidden-option>
564+
--timeout <timeout> Time to wait before timeout (in seconds)
565+
-h, --help Show help information.
566+
567+
"""
568+
}
546569

547-
OPTIONS:
548-
--verbose Verbose
549-
--custom-name <custom-name>
550-
Custom Name
551-
--timeout <timeout> Time to wait before timeout (in seconds)
552-
-h, --help Show help information.
570+
@available(*, deprecated)
571+
func testHidingOptionGroup() throws {
572+
AssertHelp(.default, for: HideOptionGroupLegacyDriver.self, equals: helpMessage)
573+
AssertHelp(.default, for: HideOptionGroupDriver.self, equals: helpMessage)
574+
AssertHelp(.default, for: PrivateOptionGroupDriver.self, equals: helpMessage)
575+
}
553576

554-
"""
577+
@available(*, deprecated)
578+
func testHelpHiddenShowsDefaultAndHidden() throws {
555579
AssertHelp(.hidden, for: HideOptionGroupLegacyDriver.self, equals: helpHiddenMessage)
556580
AssertHelp(.hidden, for: HideOptionGroupDriver.self, equals: helpHiddenMessage)
581+
582+
// Note: Private option groups are not visible at `.hidden` help level.
583+
AssertHelp(.hidden, for: PrivateOptionGroupDriver.self, equals: helpMessage)
557584
}
585+
}
558586

587+
extension HelpGenerationTests {
559588
struct AllValues: ParsableCommand {
560589
enum Manual: Int, ExpressibleByArgument {
561590
case foo

0 commit comments

Comments
 (0)