Skip to content

Commit 77ad487

Browse files
authored
output default value in help for Flags built from CaseIterable enums (#63)
1 parent b3a77c4 commit 77ad487

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Sources/ArgumentParser/Parsable Properties/Flag.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,11 @@ extension Flag where Value: CaseIterable, Value: RawRepresentable, Value.RawValu
240240
// This gets flipped to `true` the first time one of these flags is
241241
// encountered.
242242
var hasUpdated = false
243-
243+
let defaultValue = initial.map(String.init(describing:))
244+
244245
let args = Value.allCases.map { value -> ArgumentDefinition in
245246
let caseKey = InputKey(rawValue: value.rawValue)
246-
let help = ArgumentDefinition.Help(options: initial != nil ? .isOptional : [], help: help, key: key)
247+
let help = ArgumentDefinition.Help(options: initial != nil ? .isOptional : [], help: help, defaultValue: defaultValue, key: key)
247248
return ArgumentDefinition.flag(name: name, key: key, caseKey: caseKey, help: help, parsingStrategy: .nextAsValue, initialValue: initial, update: .nullary({ (origin, name, values) in
248249
// TODO: We should catch duplicate flags that hit a single part of
249250
// an exclusive argument set in the value parsing, not here.

Tests/UnitTests/HelpGenerationTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ extension HelpGenerationTests {
100100
""")
101101
}
102102

103+
enum OptionFlags: String, CaseIterable { case optional, required }
103104
struct D: ParsableCommand {
104105

105106
@Option(default: "John", help: "Your name.")
@@ -110,16 +111,20 @@ extension HelpGenerationTests {
110111

111112
@Option(default: false, help: "Whether logging is enabled.")
112113
var logging: Bool
114+
115+
@Flag(default: .optional, help: "Vegan diet.")
116+
var nda: OptionFlags
113117
}
114118

115119
func testHelpWithDefaultValues() {
116120
AssertHelp(for: D.self, equals: """
117-
USAGE: d [--name <name>] [--age <age>] [--logging <logging>]
121+
USAGE: d [--name <name>] [--age <age>] [--logging <logging>] [--optional] [--required]
118122
119123
OPTIONS:
120124
--name <name> Your name. (default: John)
121125
--age <age> Your age. (default: 20)
122126
--logging <logging> Whether logging is enabled. (default: false)
127+
--optional/--required Vegan diet. (default: optional)
123128
-h, --help Show help information.
124129
125130
""")
@@ -154,7 +159,7 @@ extension HelpGenerationTests {
154159
USAGE: f [-s] [-c] [-l]
155160
156161
OPTIONS:
157-
-s/-c/-l Change the program output
162+
-s/-c/-l Change the program output (default: list)
158163
-h, --help Show help information.
159164
160165
""")

0 commit comments

Comments
 (0)