Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix help display for non-String RawRepresentables #494

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -54,15 +54,15 @@ extension ExpressibleByArgument where Self: CaseIterable {
}
}

extension ExpressibleByArgument where Self: CaseIterable, Self: RawRepresentable, RawValue == String {
extension ExpressibleByArgument where Self: CaseIterable, Self: RawRepresentable, RawValue: ExpressibleByArgument {
public static var allValueStrings: [String] {
self.allCases.map { $0.rawValue }
self.allCases.map(\.rawValue.defaultValueDescription)
}
}

extension ExpressibleByArgument where Self: RawRepresentable, RawValue == String {
extension ExpressibleByArgument where Self: RawRepresentable, RawValue: ExpressibleByArgument {
public var defaultValueDescription: String {
rawValue
rawValue.defaultValueDescription
}
}

Expand Down
14 changes: 14 additions & 0 deletions Tests/ArgumentParserUnitTests/ErrorMessageTests.swift
Expand Up @@ -80,6 +80,11 @@ fileprivate enum Name: String, Equatable, Decodable, ExpressibleByArgument, Case
case tony
}

fileprivate enum Counter: Int, ExpressibleByArgument, CaseIterable {
case one = 1
case two, three, four
}

fileprivate struct Foo: ParsableArguments {
@Option(name: [.short, .long])
var format: Format
Expand All @@ -97,6 +102,10 @@ fileprivate struct EnumWithManyCasesArrayArgument: ParsableArguments {
var names: [Name]
}

fileprivate struct EnumWithIntRawValue: ParsableArguments {
@Option var counter: Counter
}

extension ErrorMessageTests {
func testWrongEnumValue() {
AssertErrorMessage(Foo.self, ["--format", "png"], "The value 'png' is invalid for '--format <format>'. Please provide one of 'text', 'json' or 'csv'.")
Expand Down Expand Up @@ -135,6 +144,11 @@ extension ErrorMessageTests {
- thor
- tony
""")

AssertErrorMessage(EnumWithIntRawValue.self, ["--counter", "one"], """
The value 'one' is invalid for '--counter <counter>'. \
Please provide one of '1', '2', '3' or '4'.
""")
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Expand Up @@ -211,7 +211,7 @@ extension HelpGenerationTests {
--degree <degree> Your degree.
--directory <directory> Directory. (default: current directory)
--manual <manual> Manual Option. (default: default-value)
--unspecial <unspecial> Unspecialized Synthesized (default: one)
--unspecial <unspecial> Unspecialized Synthesized (default: 0)
--special <special> Specialized Synthesized (default: Apple)
-h, --help Show help information.

Expand Down Expand Up @@ -633,7 +633,7 @@ extension HelpGenerationTests {

func testAllValueStrings() throws {
XCTAssertEqual(AllValues.Manual.allValueStrings, ["bar"])
XCTAssertEqual(AllValues.UnspecializedSynthesized.allValueStrings, ["one", "two"])
XCTAssertEqual(AllValues.UnspecializedSynthesized.allValueStrings, ["0", "1"])
XCTAssertEqual(AllValues.SpecializedSynthesized.allValueStrings, ["Apple", "Banana"])
}

Expand Down