forked from clap-rs/clap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(complete): Handle multi-valued arguments
zsh completions for commands that end with two Vec arguments require special care to handle the transition from the penultimate multi-valued argument and the final multi-valued argument. We can have two Vec args separated with a value terminator. We can also have two Vec args with no value terminators specified. Both of these scenarios require a terminator in the zsh completions. Special-case the penultimate multi-valued argument and force it to emit a value terminator when the final argument also takes multiple values or has the `last` option set. Closes clap-rs#3022 Signed-off-by: David Aguilar <davvid@gmail.com>
- Loading branch information
Showing
12 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
clap_complete/tests/snapshots/two_multi_valued_arguments.bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
_my-app() { | ||
local i cur prev opts cmds | ||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
prev="${COMP_WORDS[COMP_CWORD-1]}" | ||
cmd="" | ||
opts="" | ||
|
||
for i in ${COMP_WORDS[@]} | ||
do | ||
case "${cmd},${i}" in | ||
",$1") | ||
cmd="my__app" | ||
;; | ||
*) | ||
;; | ||
esac | ||
done | ||
|
||
case "${cmd}" in | ||
my__app) | ||
opts="-h --help [first]... [second]..." | ||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then | ||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) | ||
return 0 | ||
fi | ||
case "${prev}" in | ||
*) | ||
COMPREPLY=() | ||
;; | ||
esac | ||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) | ||
return 0 | ||
;; | ||
esac | ||
} | ||
|
||
complete -F _my-app -o bashdefault -o default my-app |
26 changes: 26 additions & 0 deletions
26
clap_complete/tests/snapshots/two_multi_valued_arguments.elvish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
use builtin; | ||
use str; | ||
|
||
set edit:completion:arg-completer[my-app] = {|@words| | ||
fn spaces {|n| | ||
builtin:repeat $n ' ' | str:join '' | ||
} | ||
fn cand {|text desc| | ||
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc | ||
} | ||
var command = 'my-app' | ||
for word $words[1..-1] { | ||
if (str:has-prefix $word '-') { | ||
break | ||
} | ||
set command = $command';'$word | ||
} | ||
var completions = [ | ||
&'my-app'= { | ||
cand -h 'Print help' | ||
cand --help 'Print help' | ||
} | ||
] | ||
$completions[$command] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
complete -c my-app -s h -l help -d 'Print help' |
32 changes: 32 additions & 0 deletions
32
clap_complete/tests/snapshots/two_multi_valued_arguments.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
using namespace System.Management.Automation | ||
using namespace System.Management.Automation.Language | ||
|
||
Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { | ||
param($wordToComplete, $commandAst, $cursorPosition) | ||
|
||
$commandElements = $commandAst.CommandElements | ||
$command = @( | ||
'my-app' | ||
for ($i = 1; $i -lt $commandElements.Count; $i++) { | ||
$element = $commandElements[$i] | ||
if ($element -isnot [StringConstantExpressionAst] -or | ||
$element.StringConstantType -ne [StringConstantType]::BareWord -or | ||
$element.Value.StartsWith('-') -or | ||
$element.Value -eq $wordToComplete) { | ||
break | ||
} | ||
$element.Value | ||
}) -join ';' | ||
|
||
$completions = @(switch ($command) { | ||
'my-app' { | ||
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') | ||
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') | ||
break | ||
} | ||
}) | ||
|
||
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } | | ||
Sort-Object -Property ListItemText | ||
} |
31 changes: 31 additions & 0 deletions
31
clap_complete/tests/snapshots/two_multi_valued_arguments.zsh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#compdef my-app | ||
|
||
autoload -U is-at-least | ||
|
||
_my-app() { | ||
typeset -A opt_args | ||
typeset -a _arguments_options | ||
local ret=1 | ||
|
||
if is-at-least 5.2; then | ||
_arguments_options=(-s -S -C) | ||
else | ||
_arguments_options=(-s -C) | ||
fi | ||
|
||
local context curcontext="$curcontext" state line | ||
_arguments "${_arguments_options[@]}" \ | ||
'-h[Print help]' \ | ||
'--help[Print help]' \ | ||
'*--::first -- first multi-valued argument:' \ | ||
'*::second -- second multi-valued argument:' \ | ||
&& ret=0 | ||
} | ||
|
||
(( $+functions[_my-app_commands] )) || | ||
_my-app_commands() { | ||
local commands; commands=() | ||
_describe -t commands 'my-app commands' commands "$@" | ||
} | ||
|
||
_my-app "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters