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 have multiple Vec arguments require special care. We can have two Vec args separated with a value terminator. We can also have two Vec args with no value terminators specified where the final arg uses 'raw' and thus requires '--' to be used. The 2nd of these scenarios requires special handling to avoid emitting a duplicate '*:arguments' completion entry. Currently, the zsh completions generate an error in this scenario: $ my-app <TAB> _arguments:...: doubled rest argument definition: *::second -- second set of of multi-length arguments: We already use the '-S' option when calling _arguments. This option makes it so that completion stops after '--' is encountered. This means that the handling for trailing 'raw' arguments does not need to specified. Special-case multi-valued arguments so that we can skip emitting the final multi-valued argument if a previous multi-valued argument has already been emitted. Closes clap-rs#3022 Signed-off-by: David Aguilar <davvid@gmail.com>
- Loading branch information
Showing
12 changed files
with
231 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 | ||
} |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
#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:' \ | ||
&& 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