Skip to content

Commit 72bf212

Browse files
authored
extend ToolInfoV0 with command visibility (#669)
- resolves #668 by extending the ToolInfoV0 (help dump) struct to include visibility information for commands (already exists for arguments). - updates test output to verify existing examples extend with the additional key.
1 parent 83c5134 commit 72bf212

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Sources/ArgumentParser/Usage/DumpHelpGenerator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ fileprivate extension CommandInfoV0 {
135135
self = CommandInfoV0(
136136
superCommands: superCommands,
137137
commandName: command._commandName,
138+
shouldDisplay: command.configuration.shouldDisplay,
138139
abstract: command.configuration.abstract,
139140
discussion2: .init(command.configuration.discussion),
140141
defaultSubcommand: defaultSubcommand,

Sources/ArgumentParserToolInfo/ToolInfo.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public struct CommandInfoV0: Codable, Hashable {
111111
/// Custom CodingKeys names.
112112
enum CodingKeys: String, CodingKey {
113113
case discussion2 = "discussion"
114-
case superCommands, commandName, abstract, defaultSubcommand, subcommands, arguments
114+
case superCommands, commandName, abstract, defaultSubcommand, subcommands, arguments, shouldDisplay
115115
}
116116

117117
/// Super commands and tools.
@@ -143,6 +143,9 @@ public struct CommandInfoV0: Codable, Hashable {
143143
/// for a custom `CaseIterable` type), or can describe
144144
/// a static block of text that extends the description of the argument.
145145
public var discussion2: Discussion?
146+
147+
/// Command should appear in help displays.
148+
public var shouldDisplay: Bool = true
146149

147150
/// Optional name of the subcommand invoked when the command is invoked with
148151
/// no arguments.
@@ -155,6 +158,7 @@ public struct CommandInfoV0: Codable, Hashable {
155158
public init(
156159
superCommands: [String],
157160
commandName: String,
161+
shouldDisplay: Bool,
158162
abstract: String,
159163
discussion2: Discussion?,
160164
defaultSubcommand: String?,
@@ -164,6 +168,7 @@ public struct CommandInfoV0: Codable, Hashable {
164168
self.superCommands = superCommands.nonEmpty
165169

166170
self.commandName = commandName
171+
self.shouldDisplay = shouldDisplay
167172
self.abstract = abstract.nonEmpty
168173
self.discussion2 = discussion2
169174
self.defaultSubcommand = defaultSubcommand?.nonEmpty
@@ -186,6 +191,7 @@ public struct CommandInfoV0: Codable, Hashable {
186191
self.init(
187192
superCommands: superCommands,
188193
commandName: commandName,
194+
shouldDisplay: true,
189195
abstract: abstract,
190196
discussion2: discussion,
191197
defaultSubcommand: defaultSubcommand,
@@ -244,7 +250,7 @@ public struct ArgumentInfoV0: Codable, Hashable {
244250
public var shouldDisplay: Bool
245251
/// Custom name of argument's section.
246252
public var sectionTitle: String?
247-
253+
248254
/// Argument can be omitted.
249255
public var isOptional: Bool
250256
/// Argument can be specified multiple times.
@@ -311,7 +317,7 @@ public struct ArgumentInfoV0: Codable, Hashable {
311317

312318
self.shouldDisplay = shouldDisplay
313319
self.sectionTitle = sectionTitle
314-
320+
315321
self.isOptional = isOptional
316322
self.isRepeating = isRepeating
317323

Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ extension DumpHelpGenerationTests {
9090

9191
@Option(help: .init(discussion: "A discussion."))
9292
var discussion: String
93+
94+
static var configuration = CommandConfiguration(shouldDisplay: false)
9395
}
9496

9597
public func testDumpA() throws {
@@ -277,7 +279,8 @@ extension DumpHelpGenerationTests {
277279
"valueName" : "help"
278280
}
279281
],
280-
"commandName" : "a"
282+
"commandName" : "a",
283+
"shouldDisplay" : true
281284
},
282285
"serializationVersion" : 0
283286
}
@@ -346,7 +349,8 @@ extension DumpHelpGenerationTests {
346349
"valueName" : "help"
347350
}
348351
],
349-
"commandName" : "b"
352+
"commandName" : "b",
353+
"shouldDisplay" : true
350354
},
351355
"serializationVersion" : 0
352356
}
@@ -594,7 +598,8 @@ extension DumpHelpGenerationTests {
594598
"valueName" : "help"
595599
}
596600
],
597-
"commandName" : "c"
601+
"commandName" : "c",
602+
"shouldDisplay" : false
598603
},
599604
"serializationVersion" : 0
600605
}
@@ -647,6 +652,7 @@ extension DumpHelpGenerationTests {
647652
}
648653
],
649654
"commandName" : "math",
655+
"shouldDisplay" : true,
650656
"subcommands" : [
651657
{
652658
"abstract" : "Print the sum of the values.",
@@ -723,6 +729,7 @@ extension DumpHelpGenerationTests {
723729
}
724730
],
725731
"commandName" : "add",
732+
"shouldDisplay" : true,
726733
"superCommands" : [
727734
"math"
728735
]
@@ -802,6 +809,7 @@ extension DumpHelpGenerationTests {
802809
}
803810
],
804811
"commandName" : "multiply",
812+
"shouldDisplay" : true,
805813
"superCommands" : [
806814
"math"
807815
]
@@ -851,6 +859,7 @@ extension DumpHelpGenerationTests {
851859
}
852860
],
853861
"commandName" : "stats",
862+
"shouldDisplay" : true,
854863
"subcommands" : [
855864
{
856865
"abstract" : "Print the average of the values.",
@@ -929,6 +938,7 @@ extension DumpHelpGenerationTests {
929938
}
930939
],
931940
"commandName" : "average",
941+
"shouldDisplay" : true,
932942
"superCommands" : [
933943
"math",
934944
"stats"
@@ -987,6 +997,7 @@ extension DumpHelpGenerationTests {
987997
}
988998
],
989999
"commandName" : "stdev",
1000+
"shouldDisplay" : true,
9901001
"superCommands" : [
9911002
"math",
9921003
"stats"
@@ -1195,6 +1206,7 @@ extension DumpHelpGenerationTests {
11951206
}
11961207
],
11971208
"commandName" : "quantiles",
1209+
"shouldDisplay" : true,
11981210
"superCommands" : [
11991211
"math",
12001212
"stats"
@@ -1289,6 +1301,7 @@ extension DumpHelpGenerationTests {
12891301
}
12901302
],
12911303
"commandName" : "add",
1304+
"shouldDisplay" : true,
12921305
"superCommands" : [
12931306
"math"
12941307
]
@@ -1375,6 +1388,7 @@ extension DumpHelpGenerationTests {
13751388
}
13761389
],
13771390
"commandName" : "multiply",
1391+
"shouldDisplay" : true,
13781392
"superCommands" : [
13791393
"math"
13801394
]
@@ -1431,6 +1445,7 @@ extension DumpHelpGenerationTests {
14311445
}
14321446
],
14331447
"commandName" : "stats",
1448+
"shouldDisplay" : true,
14341449
"subcommands" : [
14351450
{
14361451
"abstract" : "Print the average of the values.",
@@ -1509,6 +1524,7 @@ extension DumpHelpGenerationTests {
15091524
}
15101525
],
15111526
"commandName" : "average",
1527+
"shouldDisplay" : true,
15121528
"superCommands" : [
15131529
"math",
15141530
"stats"
@@ -1567,6 +1583,7 @@ extension DumpHelpGenerationTests {
15671583
}
15681584
],
15691585
"commandName" : "stdev",
1586+
"shouldDisplay" : true,
15701587
"superCommands" : [
15711588
"math",
15721589
"stats"
@@ -1775,6 +1792,7 @@ extension DumpHelpGenerationTests {
17751792
}
17761793
],
17771794
"commandName" : "quantiles",
1795+
"shouldDisplay" : true,
17781796
"superCommands" : [
17791797
"math",
17801798
"stats"

0 commit comments

Comments
 (0)