Skip to content

Make @OptionGroup(visibility:) a public API #419

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 1 commit into from
Mar 15, 2022
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
21 changes: 12 additions & 9 deletions Sources/ArgumentParser/Parsable Properties/OptionGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ public struct OptionGroup<Value: ParsableArguments>: Decodable, ParsedWrapper {
}
}

/// Creates a property that represents another parsable type.
public init() {
/// Creates a property that represents another parsable type, using the
/// specified visibility.
public init(visibility: ArgumentVisibility = .default) {
self.init(_parsedValue: .init { _ in
ArgumentSet(Value.self, visibility: .private)
})
self._visibility = visibility
}

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

// Experimental use with caution
extension OptionGroup {
@available(*, deprecated, message: "Use init(_visibility:) instead.")
@available(*, deprecated, renamed: "init(visibility:)")
public init(_hiddenFromHelp: Bool) {
self.init()
self._visibility = .hidden
self.init(visibility: .hidden)
}

public init(_visibility: ArgumentVisibility) {
self.init()
self._visibility = _visibility

/// Creates a property that represents another parsable type.
@available(*, deprecated, renamed: "init(visibility:)")
@_disfavoredOverload
public init() {
self.init(visibility: .default)
}
}
85 changes: 57 additions & 28 deletions Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -491,17 +491,25 @@ extension HelpGenerationTests {
""")
}

struct optionsToHide: ParsableArguments {
}

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

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

@Option(help: .hidden)
var hiddenOption: String?

@Argument(help: .private)
var privateArg: String?
}

@available(*, deprecated)
struct HideOptionGroupLegacyDriver: ParsableCommand {
private struct HideOptionGroupLegacyDriver: ParsableCommand {
static let configuration = CommandConfiguration(commandName: "driver", abstract: "Demo hiding option groups")

@OptionGroup(_hiddenFromHelp: true)
Expand All @@ -511,51 +519,72 @@ extension HelpGenerationTests {
var timeout: Int?
}

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

@OptionGroup(_visibility: .hidden)
@OptionGroup(visibility: .hidden)
var hideMe: optionsToHide

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

@available(*, deprecated)
func testHidingOptionGroup() throws {
let helpMessage = """
OVERVIEW: Demo hiding option groups
private struct PrivateOptionGroupDriver: ParsableCommand {
static let configuration = CommandConfiguration(commandName: "driver", abstract: "Demo hiding option groups")

USAGE: driver [--timeout <timeout>]
@OptionGroup(visibility: .private)
var hideMe: optionsToHide

OPTIONS:
--timeout <timeout> Time to wait before timeout (in seconds)
-h, --help Show help information.
@Option(help: "Time to wait before timeout (in seconds)")
var timeout: Int?
}

"""
AssertHelp(.default, for: HideOptionGroupLegacyDriver.self, equals: helpMessage)
AssertHelp(.default, for: HideOptionGroupDriver.self, equals: helpMessage)
private var helpMessage: String { """
OVERVIEW: Demo hiding option groups
USAGE: driver [--timeout <timeout>]
OPTIONS:
--timeout <timeout> Time to wait before timeout (in seconds)
-h, --help Show help information.
"""
}

@available(*, deprecated)
func testHelpHiddenShowsAll() throws {
let helpHiddenMessage = """
OVERVIEW: Demo hiding option groups
private var helpHiddenMessage: String { """
OVERVIEW: Demo hiding option groups
USAGE: driver [--verbose] [--custom-name <custom-name>] [--hidden-option <hidden-option>] [--timeout <timeout>]
USAGE: driver [--verbose] [--custom-name <custom-name>] [--timeout <timeout>]
OPTIONS:
--verbose Verbose
--custom-name <custom-name>
Custom Name
--hidden-option <hidden-option>
--timeout <timeout> Time to wait before timeout (in seconds)
-h, --help Show help information.
"""
}

OPTIONS:
--verbose Verbose
--custom-name <custom-name>
Custom Name
--timeout <timeout> Time to wait before timeout (in seconds)
-h, --help Show help information.
@available(*, deprecated)
func testHidingOptionGroup() throws {
AssertHelp(.default, for: HideOptionGroupLegacyDriver.self, equals: helpMessage)
AssertHelp(.default, for: HideOptionGroupDriver.self, equals: helpMessage)
AssertHelp(.default, for: PrivateOptionGroupDriver.self, equals: helpMessage)
}

"""
@available(*, deprecated)
func testHelpHiddenShowsDefaultAndHidden() throws {
AssertHelp(.hidden, for: HideOptionGroupLegacyDriver.self, equals: helpHiddenMessage)
AssertHelp(.hidden, for: HideOptionGroupDriver.self, equals: helpHiddenMessage)

// Note: Private option groups are not visible at `.hidden` help level.
AssertHelp(.hidden, for: PrivateOptionGroupDriver.self, equals: helpMessage)
}
}

extension HelpGenerationTests {
struct AllValues: ParsableCommand {
enum Manual: Int, ExpressibleByArgument {
case foo
Expand Down