Skip to content

Commit 0cf923d

Browse files
committed
Improve custom completion args for fish 4+.
Resolve #737 Signed-off-by: Ross Goldberg <[email protected]>
1 parent a97f2d8 commit 0cf923d

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

Sources/ArgumentParser/Completions/FishCompletionsGenerator.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@ extension [ParsableCommand.Type] {
3939
end
4040
end
4141
42+
function \(tokensFunctionName)
43+
if test (string split -m 1 -f 1 . $FISH_VERSION) -gt 3
44+
commandline --tokens-raw $argv
45+
else
46+
commandline -o $argv
47+
end
48+
end
49+
4250
function \(usingCommandFunctionName) -a expected_commands
4351
set COMMANDS
44-
set POSITIONALS (commandline -opc)
52+
set POSITIONALS (\(tokensFunctionName) -pc)
4553
\(commandsAndPositionalsFunctionName)
4654
test "$COMMANDS" = $expected_commands
4755
end
4856
4957
function \(positionalIndexFunctionName)
50-
set POSITIONALS (commandline -opc)
58+
set POSITIONALS (\(tokensFunctionName) -pc)
5159
\(commandsAndPositionalsFunctionName)
5260
math (count $POSITIONALS) + 1
5361
end
@@ -63,9 +71,9 @@ extension [ParsableCommand.Type] {
6371
set -x \(CompletionShell.shellEnvironmentVariableName) fish
6472
set -x \(CompletionShell.shellVersionEnvironmentVariableName) $FISH_VERSION
6573
66-
set tokens (commandline -op)
67-
if test -z (commandline -ot)
68-
set index (count (commandline -opc))
74+
set tokens (\(tokensFunctionName) -p)
75+
if test -z (\(tokensFunctionName) -t)
76+
set index (count (\(tokensFunctionName) -pc))
6977
set tokens $tokens[..$index] \\'\\' $tokens[$(math $index + 1)..]
7078
end
7179
command $tokens[1] $argv $tokens
@@ -234,6 +242,12 @@ extension [ParsableCommand.Type] {
234242
"_swift_\(first!._commandName)_commands_and_positionals"
235243
}
236244

245+
private var tokensFunctionName: String {
246+
// swift-format-ignore: NeverForceUnwrap
247+
// Precondition: first is guaranteed to be non-empty
248+
"_swift_\(first!._commandName)_tokens"
249+
}
250+
237251
private var usingCommandFunctionName: String {
238252
// swift-format-ignore: NeverForceUnwrap
239253
// Precondition: first is guaranteed to be non-empty

Tests/ArgumentParserExampleTests/Snapshots/testMathFishCompletionScript().fish

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,23 @@ function _swift_math_commands_and_positionals_helper -S -a argparse_options -a o
3737
end
3838
end
3939

40+
function _swift_math_tokens
41+
if test (string split -m 1 -f 1 . $FISH_VERSION) -gt 3
42+
commandline --tokens-raw $argv
43+
else
44+
commandline -o $argv
45+
end
46+
end
47+
4048
function _swift_math_using_command -a expected_commands
4149
set COMMANDS
42-
set POSITIONALS (commandline -opc)
50+
set POSITIONALS (_swift_math_tokens -pc)
4351
_swift_math_commands_and_positionals
4452
test "$COMMANDS" = $expected_commands
4553
end
4654

4755
function _swift_math_positional_index
48-
set POSITIONALS (commandline -opc)
56+
set POSITIONALS (_swift_math_tokens -pc)
4957
_swift_math_commands_and_positionals
5058
math (count $POSITIONALS) + 1
5159
end
@@ -61,9 +69,9 @@ function _swift_math_custom_completion
6169
set -x SAP_SHELL fish
6270
set -x SAP_SHELL_VERSION $FISH_VERSION
6371

64-
set tokens (commandline -op)
65-
if test -z (commandline -ot)
66-
set index (count (commandline -opc))
72+
set tokens (_swift_math_tokens -p)
73+
if test -z (_swift_math_tokens -t)
74+
set index (count (_swift_math_tokens -pc))
6775
set tokens $tokens[..$index] \'\' $tokens[$(math $index + 1)..]
6876
end
6977
command $tokens[1] $argv $tokens

Tests/ArgumentParserUnitTests/Snapshots/testBase_Fish().fish

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,23 @@ function _swift_base-test_commands_and_positionals_helper -S -a argparse_options
2121
end
2222
end
2323

24+
function _swift_base-test_tokens
25+
if test (string split -m 1 -f 1 . $FISH_VERSION) -gt 3
26+
commandline --tokens-raw $argv
27+
else
28+
commandline -o $argv
29+
end
30+
end
31+
2432
function _swift_base-test_using_command -a expected_commands
2533
set COMMANDS
26-
set POSITIONALS (commandline -opc)
34+
set POSITIONALS (_swift_base-test_tokens -pc)
2735
_swift_base-test_commands_and_positionals
2836
test "$COMMANDS" = $expected_commands
2937
end
3038

3139
function _swift_base-test_positional_index
32-
set POSITIONALS (commandline -opc)
40+
set POSITIONALS (_swift_base-test_tokens -pc)
3341
_swift_base-test_commands_and_positionals
3442
math (count $POSITIONALS) + 1
3543
end
@@ -45,9 +53,9 @@ function _swift_base-test_custom_completion
4553
set -x SAP_SHELL fish
4654
set -x SAP_SHELL_VERSION $FISH_VERSION
4755

48-
set tokens (commandline -op)
49-
if test -z (commandline -ot)
50-
set index (count (commandline -opc))
56+
set tokens (_swift_base-test_tokens -p)
57+
if test -z (_swift_base-test_tokens -t)
58+
set index (count (_swift_base-test_tokens -pc))
5159
set tokens $tokens[..$index] \'\' $tokens[$(math $index + 1)..]
5260
end
5361
command $tokens[1] $argv $tokens

0 commit comments

Comments
 (0)