Skip to content

Commit 774de9c

Browse files
authored
Fix help display for non-String RawRepresentables (#494)
RawRepresentable types that have a non-String raw value are having values displayed in the help screen by converting the RawRep value into a string. However, these values are by default parsed by their raw value, so we should use that for display instead. This is accomplished by adding a defaultValueDescription implementation for all ExpressibleByArgument-conforming RawValue types, and then basing the allValues implementation on that. This generalizes the existing overloads for String-based RawRep types, while also allowing users who customize their ExpressibleByArgument implementation to provide the correct help value for clients.
1 parent 841b853 commit 774de9c

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ extension ExpressibleByArgument where Self: CaseIterable {
5454
}
5555
}
5656

57-
extension ExpressibleByArgument where Self: CaseIterable, Self: RawRepresentable, RawValue == String {
57+
extension ExpressibleByArgument where Self: CaseIterable, Self: RawRepresentable, RawValue: ExpressibleByArgument {
5858
public static var allValueStrings: [String] {
59-
self.allCases.map { $0.rawValue }
59+
self.allCases.map(\.rawValue.defaultValueDescription)
6060
}
6161
}
6262

63-
extension ExpressibleByArgument where Self: RawRepresentable, RawValue == String {
63+
extension ExpressibleByArgument where Self: RawRepresentable, RawValue: ExpressibleByArgument {
6464
public var defaultValueDescription: String {
65-
rawValue
65+
rawValue.defaultValueDescription
6666
}
6767
}
6868

Tests/ArgumentParserUnitTests/ErrorMessageTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ fileprivate enum Name: String, Equatable, Decodable, ExpressibleByArgument, Case
8080
case tony
8181
}
8282

83+
fileprivate enum Counter: Int, ExpressibleByArgument, CaseIterable {
84+
case one = 1
85+
case two, three, four
86+
}
87+
8388
fileprivate struct Foo: ParsableArguments {
8489
@Option(name: [.short, .long])
8590
var format: Format
@@ -97,6 +102,10 @@ fileprivate struct EnumWithManyCasesArrayArgument: ParsableArguments {
97102
var names: [Name]
98103
}
99104

105+
fileprivate struct EnumWithIntRawValue: ParsableArguments {
106+
@Option var counter: Counter
107+
}
108+
100109
extension ErrorMessageTests {
101110
func testWrongEnumValue() {
102111
AssertErrorMessage(Foo.self, ["--format", "png"], "The value 'png' is invalid for '--format <format>'. Please provide one of 'text', 'json' or 'csv'.")
@@ -135,6 +144,11 @@ extension ErrorMessageTests {
135144
- thor
136145
- tony
137146
""")
147+
148+
AssertErrorMessage(EnumWithIntRawValue.self, ["--counter", "one"], """
149+
The value 'one' is invalid for '--counter <counter>'. \
150+
Please provide one of '1', '2', '3' or '4'.
151+
""")
138152
}
139153
}
140154

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ extension HelpGenerationTests {
211211
--degree <degree> Your degree.
212212
--directory <directory> Directory. (default: current directory)
213213
--manual <manual> Manual Option. (default: default-value)
214-
--unspecial <unspecial> Unspecialized Synthesized (default: one)
214+
--unspecial <unspecial> Unspecialized Synthesized (default: 0)
215215
--special <special> Specialized Synthesized (default: Apple)
216216
-h, --help Show help information.
217217
@@ -633,7 +633,7 @@ extension HelpGenerationTests {
633633

634634
func testAllValueStrings() throws {
635635
XCTAssertEqual(AllValues.Manual.allValueStrings, ["bar"])
636-
XCTAssertEqual(AllValues.UnspecializedSynthesized.allValueStrings, ["one", "two"])
636+
XCTAssertEqual(AllValues.UnspecializedSynthesized.allValueStrings, ["0", "1"])
637637
XCTAssertEqual(AllValues.SpecializedSynthesized.allValueStrings, ["Apple", "Banana"])
638638
}
639639

0 commit comments

Comments
 (0)