Skip to content

Commit 67fa3c0

Browse files
Fix the defaultValueDescription for string enums (#476)
defaultValueDescription is always the enum case name, but that's not a valid argument when the enum value differs from the case name. Extend ExpressibleByArgument to use rawValue for defaultValueDescription for string enums.
1 parent 0672ff8 commit 67fa3c0

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ extension ExpressibleByArgument where Self: CaseIterable, Self: RawRepresentable
6060
}
6161
}
6262

63+
extension ExpressibleByArgument where Self: RawRepresentable, RawValue == String {
64+
public var defaultValueDescription: String {
65+
rawValue
66+
}
67+
}
68+
6369
extension String: ExpressibleByArgument {
6470
public init?(argument: String) {
6571
self = argument

Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ final class DumpHelpGenerationTests: XCTestCase {
2222
extension DumpHelpGenerationTests {
2323
struct A: ParsableCommand {
2424
enum TestEnum: String, CaseIterable, ExpressibleByArgument {
25-
case a, b, c
25+
case a = "one", b = "two", c = "three"
2626
}
2727

2828
@Option
2929
var enumeratedOption: TestEnum
3030

31+
@Option
32+
var enumeratedOptionWithDefaultValue: TestEnum = .b
33+
3134
@Option
3235
var noHelpOption: Int
3336

@@ -80,9 +83,9 @@ extension DumpHelpGenerationTests {
8083
"arguments" : [
8184
{
8285
"allValues" : [
83-
"a",
84-
"b",
85-
"c"
86+
"one",
87+
"two",
88+
"three"
8689
],
8790
"isOptional" : false,
8891
"isRepeating" : false,
@@ -100,6 +103,29 @@ extension DumpHelpGenerationTests {
100103
"shouldDisplay" : true,
101104
"valueName" : "enumerated-option"
102105
},
106+
{
107+
"allValues" : [
108+
"one",
109+
"two",
110+
"three"
111+
],
112+
"defaultValue" : "two",
113+
"isOptional" : true,
114+
"isRepeating" : false,
115+
"kind" : "option",
116+
"names" : [
117+
{
118+
"kind" : "long",
119+
"name" : "enumerated-option-with-default-value"
120+
}
121+
],
122+
"preferredName" : {
123+
"kind" : "long",
124+
"name" : "enumerated-option-with-default-value"
125+
},
126+
"shouldDisplay" : true,
127+
"valueName" : "enumerated-option-with-default-value"
128+
},
103129
{
104130
"isOptional" : false,
105131
"isRepeating" : false,

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,30 @@ extension HelpGenerationTests {
174174

175175
@Option(help: "Directory.")
176176
var directory: URL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
177+
178+
enum Manual: Int, ExpressibleByArgument {
179+
case foo
180+
var defaultValueDescription: String { "default-value" }
181+
}
182+
@Option(help: "Manual Option.")
183+
var manual: Manual = .foo
184+
185+
enum UnspecializedSynthesized: Int, CaseIterable, ExpressibleByArgument {
186+
case one, two
187+
}
188+
@Option(help: "Unspecialized Synthesized")
189+
var unspecial: UnspecializedSynthesized = .one
190+
191+
enum SpecializedSynthesized: String, CaseIterable, ExpressibleByArgument {
192+
case apple = "Apple", banana = "Banana"
193+
}
194+
@Option(help: "Specialized Synthesized")
195+
var special: SpecializedSynthesized = .apple
177196
}
178197

179198
func testHelpWithDefaultValues() {
180199
AssertHelp(.default, for: D.self, equals: """
181-
USAGE: d [<occupation>] [--name <name>] [--age <age>] [--logging <logging>] [--lucky <numbers> ...] [--optional] [--required] [--degree <degree>] [--directory <directory>]
200+
USAGE: d [<occupation>] [--name <name>] [--age <age>] [--logging <logging>] [--lucky <numbers> ...] [--optional] [--required] [--degree <degree>] [--directory <directory>] [--manual <manual>] [--unspecial <unspecial>] [--special <special>]
182201
183202
ARGUMENTS:
184203
<occupation> Your occupation. (default: --)
@@ -191,6 +210,9 @@ extension HelpGenerationTests {
191210
--optional/--required Vegan diet. (default: optional)
192211
--degree <degree> Your degree. (default: bachelor)
193212
--directory <directory> Directory. (default: current directory)
213+
--manual <manual> Manual Option. (default: default-value)
214+
--unspecial <unspecial> Unspecialized Synthesized (default: one)
215+
--special <special> Specialized Synthesized (default: Apple)
194216
-h, --help Show help information.
195217
196218
""")

0 commit comments

Comments
 (0)