Skip to content

Commit 4cdcc17

Browse files
authored
ArgumentHelp.Visibility levels API (#390)
1 parent cc481f7 commit 4cdcc17

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

Sources/ArgumentParser/Documentation.docc/Articles/CustomizingHelp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ OPTIONS:
185185

186186
## Hiding Arguments and Commands
187187

188-
You may want to suppress features under development or experimental flags from the generated help screen. You can hide an argument or a subcommand by passing `shouldDisplay: false` to the property wrapper or `CommandConfiguration` initializers, respectively.
188+
You may want to suppress features under development or experimental flags from the generated help screen. You can hide an argument or a subcommand by passing `visibility: .hidden` to the property wrapper or `CommandConfiguration` initializers, respectively.
189189

190190
`ArgumentHelp` includes a `.hidden` static property that makes it even simpler to hide arguments:
191191

Sources/ArgumentParser/Parsable Properties/ArgumentHelp.swift

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
/// Help information for a command-line argument.
1313
public struct ArgumentHelp {
14+
/// Visibility level of an argument's help.
15+
public enum Visibility {
16+
/// Show help for this argument whenever appropriate.
17+
case `default`
18+
19+
/// Only show help for this argument in the extended help screen.
20+
case hidden
21+
22+
/// Never show help for this argument.
23+
case `private`
24+
}
25+
1426
/// A short description of the argument.
1527
public var abstract: String = ""
1628

@@ -24,26 +36,57 @@ public struct ArgumentHelp {
2436
/// flags don't include a value.
2537
public var valueName: String?
2638

39+
/// A visibility level indicating whether this argument should be shown in
40+
/// the extended help display.
41+
public var visibility: Visibility = .default
42+
2743
/// A Boolean value indicating whether this argument should be shown in
2844
/// the extended help display.
29-
public var shouldDisplay: Bool = true
45+
@available(*, deprecated, message: "Use visibility level instead.")
46+
public var shouldDisplay: Bool {
47+
get {
48+
return visibility == .default
49+
}
50+
set {
51+
visibility = newValue ? .default : .hidden
52+
}
53+
}
3054

3155
/// Creates a new help instance.
56+
@available(*, deprecated, message: "Use init(_:discussion:valueName:visibility:) instead.")
3257
public init(
3358
_ abstract: String = "",
3459
discussion: String = "",
3560
valueName: String? = nil,
36-
shouldDisplay: Bool = true)
61+
shouldDisplay: Bool)
3762
{
3863
self.abstract = abstract
3964
self.discussion = discussion
4065
self.valueName = valueName
4166
self.shouldDisplay = shouldDisplay
4267
}
43-
44-
/// A `Help` instance that hides an argument from the extended help display.
68+
69+
/// Creates a new help instance.
70+
public init(
71+
_ abstract: String = "",
72+
discussion: String = "",
73+
valueName: String? = nil,
74+
visibility: Visibility = .default)
75+
{
76+
self.abstract = abstract
77+
self.discussion = discussion
78+
self.valueName = valueName
79+
self.visibility = visibility
80+
}
81+
82+
/// A `Help` instance that shows an argument only in the extended help display.
4583
public static var hidden: ArgumentHelp {
46-
ArgumentHelp(shouldDisplay: false)
84+
ArgumentHelp(visibility: .hidden)
85+
}
86+
87+
/// A `Help` instance that hides an argument from the extended help display.
88+
public static var `private`: ArgumentHelp {
89+
ArgumentHelp(visibility: .private)
4790
}
4891
}
4992

Sources/ArgumentParser/Parsing/ArgumentDefinition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct ArgumentDefinition {
7979
self.abstract = help?.abstract ?? ""
8080
self.discussion = help?.discussion ?? ""
8181
self.valueName = help?.valueName ?? ""
82-
self.shouldDisplay = help?.shouldDisplay ?? true
82+
self.shouldDisplay = (help?.visibility ?? .default) == .default
8383
}
8484
}
8585

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,29 @@ extension HelpGenerationTests {
547547
XCTAssertEqual(AllValues.SpecializedSynthesized.allValueStrings, opts[4].help.allValues)
548548
XCTAssertEqual(AllValues.SpecializedSynthesized.allValueStrings, opts[5].help.allValues)
549549
}
550+
551+
struct Q: ParsableArguments {
552+
@Option(help: "Your name") var name: String
553+
@Option(help: "Your title") var title: String?
554+
555+
@Argument(help: .private) var privateName: String?
556+
@Option(help: .private) var privateTitle: String?
557+
@Flag(help: .private) var privateFlag: Bool = false
558+
@Flag(inversion: .prefixedNo, help: .private) var privateInvertedFlag: Bool = true
559+
}
560+
561+
func testHelpWithPrivate() {
562+
// For now, hidden and private have the same behaviour
563+
AssertHelp(for: Q.self, equals: """
564+
USAGE: q --name <name> [--title <title>]
565+
566+
OPTIONS:
567+
--name <name> Your name
568+
--title <title> Your title
569+
-h, --help Show help information.
570+
571+
""")
572+
}
550573
}
551574

552575
// MARK: - Issue #278 https://github.com/apple/swift-argument-parser/issues/278

0 commit comments

Comments
 (0)