Repository: apple/swift-argument-parser
Pull request #476 (2022-08-26 00:35:58)

Fix the defaultValueDescription for string enums whose values differ from the case names
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.

### Checklist
- [x] I've added at least one test that validates that my change is working, if appropriate
- [x] I've followed the code style of the rest of the project
- [x] I've read the [Contribution Guidelines](https://github.com/apple/swift-argument-parser/blob/main/CONTRIBUTING.md)
- [ ] I've updated the documentation if necessary


ian-twilightcoder: @swift-ci test
ian-twilightcoder: @swift-ci test
ian-twilightcoder: @swift-ci test
rauhul: @swift-ci please test
natecook1000: @swift-ci Please test
rauhul: @swift-ci please test

Patch:
diff --git a/Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift b/Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift
index eb45908..240f082 100644
--- a/Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift
+++ b/Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift
@@ -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


Test patch:
diff --git a/Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift b/Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
index f207ac9..f6c119e 100644
--- a/Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
+++ b/Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
@@ -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
     
@@ -80,9 +83,9 @@ extension DumpHelpGenerationTests {
     "arguments" : [
       {
         "allValues" : [
-          "a",
-          "b",
-          "c"
+          "one",
+          "two",
+          "three"
         ],
         "isOptional" : false,
         "isRepeating" : false,
@@ -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,
diff --git a/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift b/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
index cc2e3a8..f963d89 100644
--- a/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
+++ b/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
@@ -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: --)
@@ -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.
 
             """)