Skip to content

Commit 88af18f

Browse files
authored
Change ArgumentVisibility into a struct (#413)
- Changes ArgumentVisibility from an enum to a struct. This will allow ArgumentParser to add cases in the future without breaking clients that could have been exhaustively switching across all cases. It also allows us to implement protocol conformances on the internal type and avoid exposing them on the public type.
1 parent e7765e1 commit 88af18f

File tree

10 files changed

+37
-25
lines changed

10 files changed

+37
-25
lines changed

Sources/ArgumentParser/Completions/BashCompletionsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ struct BashCompletionsGenerator {
180180
extension ArgumentDefinition {
181181
/// Returns the different completion names for this argument.
182182
fileprivate func bashCompletionWords() -> [String] {
183-
return help.visibility == .default
183+
return help.visibility.base == .default
184184
? names.map { $0.synopsisString }
185185
: []
186186
}

Sources/ArgumentParser/Completions/FishCompletionsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ extension Name {
100100

101101
extension ArgumentDefinition {
102102
fileprivate func argumentSegments(_ commandChain: [String]) -> [([String], String)] {
103-
guard help.visibility == .default else { return [] }
103+
guard help.visibility.base == .default else { return [] }
104104

105105
var results = [([String], String)]()
106106
var formattedFlags = [String]()

Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ extension ArgumentDefinition {
134134
}
135135

136136
func zshCompletionString(_ commands: [ParsableCommand.Type]) -> String? {
137-
guard help.visibility == .default else { return nil }
137+
guard help.visibility.base == .default else { return nil }
138138

139139
var inputs: String
140140
switch update {

Sources/ArgumentParser/Parsable Properties/ArgumentHelp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public struct ArgumentHelp {
3333
@available(*, deprecated, message: "Use visibility level instead.")
3434
public var shouldDisplay: Bool {
3535
get {
36-
return visibility == .default
36+
return visibility.base == .default
3737
}
3838
set {
3939
visibility = newValue ? .default : .hidden

Sources/ArgumentParser/Parsable Properties/ArgumentVisibility.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,35 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
/// Visibility level of an argument's help.
13-
public enum ArgumentVisibility {
14-
/// Show help for this argument whenever appropriate.
13+
public struct ArgumentVisibility {
14+
/// Internal implementation of `ArgumentVisibility` to allow for easier API
15+
/// evolution.
16+
internal enum Representation {
1517
case `default`
16-
17-
/// Only show help for this argument in the extended help screen.
1818
case hidden
19-
20-
/// Never show help for this argument.
2119
case `private`
20+
}
21+
22+
internal var base: Representation
23+
24+
/// Show help for this argument whenever appropriate.
25+
public static let `default` = Self(base: .default)
26+
27+
/// Only show help for this argument in the extended help screen.
28+
public static let hidden = Self(base: .hidden)
29+
30+
/// Never show help for this argument.
31+
public static let `private` = Self(base: .private)
2232
}
2333

24-
extension ArgumentVisibility {
34+
extension ArgumentVisibility.Representation {
2535
/// A raw Integer value that represents each visibility level.
2636
///
2737
/// `_comparableLevel` can be used to test if a Visibility case is more or
2838
/// less visible than another, without committing this behavior to API.
2939
/// A lower `_comparableLevel` indicates that the case is less visible (more
3040
/// secret).
31-
private var _comparableLevel: Int {
41+
internal var _comparableLevel: Int {
3242
switch self {
3343
case .default:
3444
return 2
@@ -38,9 +48,11 @@ extension ArgumentVisibility {
3848
return 0
3949
}
4050
}
51+
}
4152

53+
extension ArgumentVisibility {
4254
/// - Returns: true if `self` is at least as visible as the supplied argument.
43-
func isAtLeastAsVisible(as other: Self) -> Bool {
44-
self._comparableLevel >= other._comparableLevel
55+
internal func isAtLeastAsVisible(as other: Self) -> Bool {
56+
self.base._comparableLevel >= other.base._comparableLevel
4557
}
4658
}

Sources/ArgumentParser/Parsable Types/ParsableArguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ extension ArgumentSet {
286286

287287
if let parsed = child.value as? ArgumentSetProvider {
288288
guard parsed._visibility.isAtLeastAsVisible(as: visibility)
289-
else { return nil }
289+
else { return nil }
290290

291291
// Property wrappers have underscore-prefixed names
292292
codingKey = String(codingKey.first == "_"

Sources/ArgumentParser/Usage/DumpHelpGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fileprivate extension ArgumentInfoV0 {
125125
guard let kind = ArgumentInfoV0.KindV0(argument: argument) else { return nil }
126126
self.init(
127127
kind: kind,
128-
shouldDisplay: argument.help.visibility == .default,
128+
shouldDisplay: argument.help.visibility.base == .default,
129129
isOptional: argument.help.options.contains(.isOptional),
130130
isRepeating: argument.help.options.contains(.isRepeating),
131131
names: argument.names.map(ArgumentInfoV0.NameInfoV0.init),

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ internal struct HelpGenerator {
140140
/// more elements at a time.
141141
var args = commandStack.argumentsForHelp(visibility: visibility)[...]
142142
while let arg = args.popFirst() {
143-
guard arg.help.visibility == .default else { continue }
143+
guard arg.help.visibility.base == .default else { continue }
144144

145145
let synopsis: String
146146
let description: String
@@ -154,7 +154,7 @@ internal struct HelpGenerator {
154154

155155
synopsis = groupedArgs
156156
.lazy
157-
.filter { $0.help.visibility == .default }
157+
.filter { $0.help.visibility.base == .default }
158158
.map { $0.synopsisForHelp }
159159
.joined(separator: "/")
160160

@@ -172,7 +172,7 @@ internal struct HelpGenerator {
172172
.filter { !$0.isEmpty }
173173
.joined(separator: " ")
174174
} else {
175-
synopsis = arg.help.visibility == .default
175+
synopsis = arg.help.visibility.base == .default
176176
? arg.synopsisForHelp
177177
: ""
178178

@@ -275,10 +275,10 @@ fileprivate extension NameSpecification {
275275
self
276276
.makeNames(InputKey(rawValue: "help"))
277277
.compactMap { name in
278-
guard visibility != .default else { return name }
278+
guard visibility.base != .default else { return name }
279279
switch name {
280280
case .long(let helpName):
281-
return .long("\(helpName)-\(visibility)")
281+
return .long("\(helpName)-\(visibility.base)")
282282
case .longWithSingleDash(let helpName):
283283
return .longWithSingleDash("\(helpName)-\(visibility)")
284284
case .short:

Sources/ArgumentParser/Usage/UsageGenerator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension UsageGenerator {
4040
var synopsis: String {
4141
// Filter out options that should not be displayed.
4242
var options = definition
43-
.filter { $0.help.visibility == .default }
43+
.filter { $0.help.visibility.base == .default }
4444
switch options.count {
4545
case 0:
4646
return toolName
@@ -346,7 +346,7 @@ extension ErrorMessageGenerator {
346346
func noValueMessage(key: InputKey) -> String? {
347347
let args = arguments(for: key)
348348
let possibilities: [String] = args.compactMap {
349-
$0.help.visibility == .default
349+
$0.help.visibility.base == .default
350350
? $0.nonOptional.synopsis
351351
: nil
352352
}

Sources/ArgumentParserTestHelpers/TestHelpers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public func AssertHelp<T: ParsableArguments>(
122122
let flag: String
123123
let includeHidden: Bool
124124

125-
switch visibility {
125+
switch visibility.base {
126126
case .default:
127127
flag = "--help"
128128
includeHidden = false
@@ -158,7 +158,7 @@ public func AssertHelp<T: ParsableCommand, U: ParsableCommand>(
158158
) {
159159
let includeHidden: Bool
160160

161-
switch visibility {
161+
switch visibility.base {
162162
case .default:
163163
includeHidden = false
164164
case .hidden:

0 commit comments

Comments
 (0)