Skip to content

Commit

Permalink
Fix the defaultValueDescription for string enums (#476)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ian-twilightcoder committed Aug 26, 2022
1 parent 0672ff8 commit 67fa3c0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
Expand Up @@ -60,6 +60,12 @@ extension ExpressibleByArgument where Self: CaseIterable, Self: RawRepresentable
}
}

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

extension String: ExpressibleByArgument {
public init?(argument: String) {
self = argument
Expand Down
34 changes: 30 additions & 4 deletions Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
Expand Up @@ -22,12 +22,15 @@ final class DumpHelpGenerationTests: XCTestCase {
extension DumpHelpGenerationTests {
struct A: ParsableCommand {
enum TestEnum: String, CaseIterable, ExpressibleByArgument {
case a, b, c
case a = "one", b = "two", c = "three"
}

@Option
var enumeratedOption: TestEnum

@Option
var enumeratedOptionWithDefaultValue: TestEnum = .b

@Option
var noHelpOption: Int

Expand Down Expand Up @@ -80,9 +83,9 @@ extension DumpHelpGenerationTests {
"arguments" : [
{
"allValues" : [
"a",
"b",
"c"
"one",
"two",
"three"
],
"isOptional" : false,
"isRepeating" : false,
Expand All @@ -100,6 +103,29 @@ extension DumpHelpGenerationTests {
"shouldDisplay" : true,
"valueName" : "enumerated-option"
},
{
"allValues" : [
"one",
"two",
"three"
],
"defaultValue" : "two",
"isOptional" : true,
"isRepeating" : false,
"kind" : "option",
"names" : [
{
"kind" : "long",
"name" : "enumerated-option-with-default-value"
}
],
"preferredName" : {
"kind" : "long",
"name" : "enumerated-option-with-default-value"
},
"shouldDisplay" : true,
"valueName" : "enumerated-option-with-default-value"
},
{
"isOptional" : false,
"isRepeating" : false,
Expand Down
24 changes: 23 additions & 1 deletion Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Expand Up @@ -174,11 +174,30 @@ extension HelpGenerationTests {

@Option(help: "Directory.")
var directory: URL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)

enum Manual: Int, ExpressibleByArgument {
case foo
var defaultValueDescription: String { "default-value" }
}
@Option(help: "Manual Option.")
var manual: Manual = .foo

enum UnspecializedSynthesized: Int, CaseIterable, ExpressibleByArgument {
case one, two
}
@Option(help: "Unspecialized Synthesized")
var unspecial: UnspecializedSynthesized = .one

enum SpecializedSynthesized: String, CaseIterable, ExpressibleByArgument {
case apple = "Apple", banana = "Banana"
}
@Option(help: "Specialized Synthesized")
var special: SpecializedSynthesized = .apple
}

func testHelpWithDefaultValues() {
AssertHelp(.default, for: D.self, equals: """
USAGE: d [<occupation>] [--name <name>] [--age <age>] [--logging <logging>] [--lucky <numbers> ...] [--optional] [--required] [--degree <degree>] [--directory <directory>]
USAGE: d [<occupation>] [--name <name>] [--age <age>] [--logging <logging>] [--lucky <numbers> ...] [--optional] [--required] [--degree <degree>] [--directory <directory>] [--manual <manual>] [--unspecial <unspecial>] [--special <special>]
ARGUMENTS:
<occupation> Your occupation. (default: --)
Expand All @@ -191,6 +210,9 @@ extension HelpGenerationTests {
--optional/--required Vegan diet. (default: optional)
--degree <degree> Your degree. (default: bachelor)
--directory <directory> Directory. (default: current directory)
--manual <manual> Manual Option. (default: default-value)
--unspecial <unspecial> Unspecialized Synthesized (default: one)
--special <special> Specialized Synthesized (default: Apple)
-h, --help Show help information.
""")
Expand Down

0 comments on commit 67fa3c0

Please sign in to comment.