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 the defaultValueDescription for string enums whose values differ from the case names #476

Merged
merged 2 commits into from Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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