Skip to content

Commit 1ed398f

Browse files
committed
Pass each fish option spec to _swift_*_commands_and_positionals functions as a separate argument.
Signed-off-by: Ross Goldberg <[email protected]>
1 parent 18e7437 commit 1ed398f

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

Sources/ArgumentParser/Completions/FishCompletionsGenerator.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ extension [ParsableCommand.Type] {
2626
end
2727
end
2828
29-
function \(commandsAndPositionalsFunctionName)_helper -S -a argparse_options -a option_specs
29+
function \(commandsAndPositionalsFunctionName)_helper -S -a argparse_options
30+
set -l option_specs $argv[2..]
3031
set -a commands $positionals[1]
3132
set -e positionals[1]
3233
if test -z $argparse_options
33-
argparse -n "$commands" (string split -- '\(separator)' $option_specs) -- $positionals 2> /dev/null
34+
argparse -n "$commands" $option_specs -- $positionals 2> /dev/null
3435
set positionals $argv
3536
else
36-
argparse (string split -- '\(separator)' $argparse_options) -n "$commands" (string split -- '\(separator)' $option_specs) -- $positionals 2> /dev/null
37+
argparse (string split -- '\(separator)' $argparse_options) -n "$commands" $option_specs -- $positionals 2> /dev/null
3738
set positionals $argv
3839
end
3940
end
@@ -91,7 +92,12 @@ extension [ParsableCommand.Type] {
9192
case '\(last!._commandName)'
9293
\(commandsAndPositionalsFunctionName)_helper '\(
9394
subcommands.isEmpty ? "" : "-s"
94-
)' '\(completableArguments.compactMap(\.optionSpec).map { "\($0)" }.joined(separator: separator))'\(
95+
)' \(
96+
completableArguments
97+
.compactMap(\.optionSpec)
98+
.map { "'\($0.fishEscapeForSingleQuotedString())'" }
99+
.joined(separator: separator)
100+
)\(
95101
subcommands.isEmpty
96102
? ""
97103
: """

Tests/ArgumentParserExampleTests/Snapshots/testMathFishCompletionScript().fish

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
function _swift_math_commands_and_positionals -S
22
switch $positionals[1]
33
case 'math'
4-
_swift_math_commands_and_positionals_helper '-s' 'version h/help'
4+
_swift_math_commands_and_positionals_helper '-s' 'version' 'h/help'
55
switch $positionals[1]
66
case 'add'
7-
_swift_math_commands_and_positionals_helper '' 'x/hex-output version h/help'
7+
_swift_math_commands_and_positionals_helper '' 'x/hex-output' 'version' 'h/help'
88
case 'multiply'
9-
_swift_math_commands_and_positionals_helper '' 'x/hex-output version h/help'
9+
_swift_math_commands_and_positionals_helper '' 'x/hex-output' 'version' 'h/help'
1010
case 'stats'
11-
_swift_math_commands_and_positionals_helper '-s' 'version h/help'
11+
_swift_math_commands_and_positionals_helper '-s' 'version' 'h/help'
1212
switch $positionals[1]
1313
case 'average'
14-
_swift_math_commands_and_positionals_helper '' 'kind= version h/help'
14+
_swift_math_commands_and_positionals_helper '' 'kind=' 'version' 'h/help'
1515
case 'stdev'
16-
_swift_math_commands_and_positionals_helper '' 'version h/help'
16+
_swift_math_commands_and_positionals_helper '' 'version' 'h/help'
1717
case 'quantiles'
18-
_swift_math_commands_and_positionals_helper '' 'file= directory= shell= custom= version h/help'
18+
_swift_math_commands_and_positionals_helper '' 'file=' 'directory=' 'shell=' 'custom=' 'version' 'h/help'
1919
end
2020
case 'help'
2121
_swift_math_commands_and_positionals_helper '' 'version'
@@ -26,14 +26,15 @@ function _swift_math_commands_and_positionals -S
2626
end
2727
end
2828

29-
function _swift_math_commands_and_positionals_helper -S -a argparse_options -a option_specs
29+
function _swift_math_commands_and_positionals_helper -S -a argparse_options
30+
set -l option_specs $argv[2..]
3031
set -a commands $positionals[1]
3132
set -e positionals[1]
3233
if test -z $argparse_options
33-
argparse -n "$commands" (string split -- ' ' $option_specs) -- $positionals 2> /dev/null
34+
argparse -n "$commands" $option_specs -- $positionals 2> /dev/null
3435
set positionals $argv
3536
else
36-
argparse (string split -- ' ' $argparse_options) -n "$commands" (string split -- ' ' $option_specs) -- $positionals 2> /dev/null
37+
argparse (string split -- ' ' $argparse_options) -n "$commands" $option_specs -- $positionals 2> /dev/null
3738
set positionals $argv
3839
end
3940
end

Tests/ArgumentParserUnitTests/Snapshots/testBase_Fish().fish

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
function _swift_base-test_commands_and_positionals -S
22
switch $positionals[1]
33
case 'base-test'
4-
_swift_base-test_commands_and_positionals_helper '' 'name= kind= other-kind= path1= path2= path3= one two three kind-counter rep1= r/rep2= h/help'
4+
_swift_base-test_commands_and_positionals_helper '' 'name=' 'kind=' 'other-kind=' 'path1=' 'path2=' 'path3=' 'one' 'two' 'three' 'kind-counter' 'rep1=' 'r/rep2=' 'h/help'
55
case '*'
66
set commands $positionals[1]
77
set -e positionals[1]
88
end
99
end
1010

11-
function _swift_base-test_commands_and_positionals_helper -S -a argparse_options -a option_specs
11+
function _swift_base-test_commands_and_positionals_helper -S -a argparse_options
12+
set -l option_specs $argv[2..]
1213
set -a commands $positionals[1]
1314
set -e positionals[1]
1415
if test -z $argparse_options
15-
argparse -n "$commands" (string split -- ' ' $option_specs) -- $positionals 2> /dev/null
16+
argparse -n "$commands" $option_specs -- $positionals 2> /dev/null
1617
set positionals $argv
1718
else
18-
argparse (string split -- ' ' $argparse_options) -n "$commands" (string split -- ' ' $option_specs) -- $positionals 2> /dev/null
19+
argparse (string split -- ' ' $argparse_options) -n "$commands" $option_specs -- $positionals 2> /dev/null
1920
set positionals $argv
2021
end
2122
end

0 commit comments

Comments
 (0)