From 4c64179add890f093ef2e2e73867e660a8887876 Mon Sep 17 00:00:00 2001 From: Ross Goldberg <484615+rgoldberg@users.noreply.github.com> Date: Sun, 18 May 2025 23:45:41 -0400 Subject: [PATCH] Use enum raw values instead of description. Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com> --- Sources/ArgumentParser/Parsable Properties/Flag.swift | 2 -- Sources/ArgumentParser/Parsable Types/EnumerableFlag.swift | 7 +++++++ Sources/ArgumentParser/Parsing/InputKey.swift | 2 +- Tests/ArgumentParserUnitTests/CompletionScriptTests.swift | 7 ++++++- .../ArgumentParserUnitTests/Snapshots/testBase_Bash().bash | 2 +- .../ArgumentParserUnitTests/Snapshots/testBase_Fish().fish | 4 ++-- Tests/ArgumentParserUnitTests/Snapshots/testBase_Zsh().zsh | 2 +- 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Sources/ArgumentParser/Parsable Properties/Flag.swift b/Sources/ArgumentParser/Parsable Properties/Flag.swift index 24cc0316e..b9f940334 100644 --- a/Sources/ArgumentParser/Parsable Properties/Flag.swift +++ b/Sources/ArgumentParser/Parsable Properties/Flag.swift @@ -250,7 +250,6 @@ extension Flag where Value == Bool? { exclusivity: exclusivity, help: help) } - } extension Flag where Value == Bool { @@ -556,7 +555,6 @@ extension Flag { key: parentKey, value: value, origin: origin, values: &values, exclusivity: exclusivity) })) - } return ArgumentSet(args) }) diff --git a/Sources/ArgumentParser/Parsable Types/EnumerableFlag.swift b/Sources/ArgumentParser/Parsable Types/EnumerableFlag.swift index 308e1f7c3..596f003c4 100644 --- a/Sources/ArgumentParser/Parsable Types/EnumerableFlag.swift +++ b/Sources/ArgumentParser/Parsable Types/EnumerableFlag.swift @@ -81,3 +81,10 @@ extension EnumerableFlag { nil } } + +extension EnumerableFlag +where Self: RawRepresentable, Self: CustomStringConvertible { + public var description: String { + String(describing: rawValue) + } +} diff --git a/Sources/ArgumentParser/Parsing/InputKey.swift b/Sources/ArgumentParser/Parsing/InputKey.swift index 485ebc50a..7c8a99b34 100644 --- a/Sources/ArgumentParser/Parsing/InputKey.swift +++ b/Sources/ArgumentParser/Parsing/InputKey.swift @@ -12,7 +12,7 @@ /// Represents the path to a parsed field, annotated with ``Flag``, ``Option`` /// or ``Argument``. /// -/// Fields that are directly declared on a ``ParsableComand`` have a path of +/// Fields that are directly declared on a ``ParsableCommand`` have a path of /// length 1, while fields that are declared indirectly (and included via an /// option group) have longer paths. struct InputKey: Hashable { diff --git a/Tests/ArgumentParserUnitTests/CompletionScriptTests.swift b/Tests/ArgumentParserUnitTests/CompletionScriptTests.swift index f2ac309fe..1d56a6edd 100644 --- a/Tests/ArgumentParserUnitTests/CompletionScriptTests.swift +++ b/Tests/ArgumentParserUnitTests/CompletionScriptTests.swift @@ -48,7 +48,12 @@ extension CompletionScriptTests { } } - enum Kind: String, ExpressibleByArgument, EnumerableFlag { + enum Kind: + String, + ExpressibleByArgument, + EnumerableFlag, + CustomStringConvertible + { case one, two case three = "custom-three" } diff --git a/Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash b/Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash index c07725cb3..3c57c8601 100644 --- a/Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash +++ b/Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash @@ -158,7 +158,7 @@ _base-test() { local -i positional_number local -a unparsed_words=("${COMP_WORDS[@]:1:${COMP_CWORD}}") - local -a flags=(--one --two --three --kind-counter -h --help) + local -a flags=(--one --two --custom-three --kind-counter -h --help) local -a options=(--name --kind --other-kind --path1 --path2 --path3 --rep1 -r --rep2) __base-test_offer_flags_options 2 diff --git a/Tests/ArgumentParserUnitTests/Snapshots/testBase_Fish().fish b/Tests/ArgumentParserUnitTests/Snapshots/testBase_Fish().fish index b64026a9f..4f58f7887 100644 --- a/Tests/ArgumentParserUnitTests/Snapshots/testBase_Fish().fish +++ b/Tests/ArgumentParserUnitTests/Snapshots/testBase_Fish().fish @@ -5,7 +5,7 @@ function __base-test_should_offer_completions_for -a expected_commands -a expect switch $unparsed_tokens[1] case 'base-test' - __base-test_parse_subcommand 2 'name=' 'kind=' 'other-kind=' 'path1=' 'path2=' 'path3=' 'one' 'two' 'three' 'kind-counter' 'rep1=' 'r/rep2=' 'h/help' + __base-test_parse_subcommand 2 'name=' 'kind=' 'other-kind=' 'path1=' 'path2=' 'path3=' 'one' 'two' 'custom-three' 'kind-counter' 'rep1=' 'r/rep2=' 'h/help' switch $unparsed_tokens[1] case 'sub-command' __base-test_parse_subcommand 0 'h/help' @@ -76,7 +76,7 @@ complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test" complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test"' -l 'path3' -rfka 'c1_fish c2_fish c3_fish' complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test"' -l 'one' complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test"' -l 'two' -complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test"' -l 'three' +complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test"' -l 'custom-three' complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test"' -l 'kind-counter' complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test"' -l 'rep1' -rfka '' complete -c 'base-test' -n '__base-test_should_offer_completions_for "base-test"' -s 'r' -l 'rep2' -rfka '' diff --git a/Tests/ArgumentParserUnitTests/Snapshots/testBase_Zsh().zsh b/Tests/ArgumentParserUnitTests/Snapshots/testBase_Zsh().zsh index a238886fa..f289a2cc5 100644 --- a/Tests/ArgumentParserUnitTests/Snapshots/testBase_Zsh().zsh +++ b/Tests/ArgumentParserUnitTests/Snapshots/testBase_Zsh().zsh @@ -52,7 +52,7 @@ _base-test() { '--path3:path3:{__base-test_complete "${___path3[@]}"}' '--one' '--two' - '--three' + '--custom-three' '*--kind-counter' '*--rep1:rep1:' '*'{-r,--rep2}':rep2:'