mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
fix(complete): Emit value terminators on zsh
Emit the user-defined value terminator into the zsh completion pattern to avoid doubled rest-arguments definitions: $ my-app <TAB> _arguments:comparguments:325: doubled rest argument definition: *::second -- second set of of multi-length arguments: https://github.com/clap-rs/clap/issues/3266#issuecomment-1007901407 noted that including the value terminator is one step towards a robust solution for handling multiple multi-valued arguments. This change does not yet handle automatically detecting when a value terminator is needed, but it does add tests to ensure that user-specified value terminators are used on zsh. Related-to: #3022 Signed-off-by: David Aguilar <davvid@gmail.com>
This commit is contained in:
parent
0b0306cc79
commit
58b0c504d8
12 changed files with 204 additions and 1 deletions
|
@ -626,8 +626,15 @@ fn write_positionals_of(p: &Command) -> String {
|
|||
debug!("write_positionals_of:iter: arg={}", arg.get_id());
|
||||
|
||||
let num_args = arg.get_num_args().expect("built");
|
||||
let cardinality_value;
|
||||
let cardinality = if num_args.max_values() > 1 {
|
||||
"*:"
|
||||
match arg.get_value_terminator() {
|
||||
Some(terminator) => {
|
||||
cardinality_value = format!("*{}:", escape_value(terminator));
|
||||
cardinality_value.as_str()
|
||||
}
|
||||
None => "*:",
|
||||
}
|
||||
} else if !arg.is_required_set() {
|
||||
":"
|
||||
} else {
|
||||
|
|
|
@ -84,6 +84,18 @@ fn value_hint() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_terminator() {
|
||||
let name = "my-app";
|
||||
let cmd = common::value_terminator_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/value_terminator.bash",
|
||||
clap_complete::shells::Bash,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable-dynamic")]
|
||||
#[test]
|
||||
fn register_minimal() {
|
||||
|
|
|
@ -252,6 +252,15 @@ pub fn value_hint_command(name: &'static str) -> clap::Command {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn value_terminator_command(name: &'static str) -> clap::Command {
|
||||
clap::Command::new(name).arg(
|
||||
clap::Arg::new("arguments")
|
||||
.help("multi-valued argument with a value terminator")
|
||||
.num_args(1..)
|
||||
.value_terminator(";"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn assert_matches_path(
|
||||
expected_path: impl AsRef<std::path::Path>,
|
||||
gen: impl clap_complete::Generator,
|
||||
|
|
|
@ -83,3 +83,15 @@ fn value_hint() {
|
|||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_terminator() {
|
||||
let name = "my-app";
|
||||
let cmd = common::value_terminator_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/value_terminator.elvish",
|
||||
clap_complete::shells::Elvish,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -83,3 +83,15 @@ fn value_hint() {
|
|||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_terminator() {
|
||||
let name = "my-app";
|
||||
let cmd = common::value_terminator_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/value_terminator.fish",
|
||||
clap_complete::shells::Fish,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -83,3 +83,15 @@ fn value_hint() {
|
|||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_terminator() {
|
||||
let name = "my-app";
|
||||
let cmd = common::value_terminator_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/value_terminator.ps1",
|
||||
clap_complete::shells::PowerShell,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
|
38
clap_complete/tests/snapshots/value_terminator.bash
Normal file
38
clap_complete/tests/snapshots/value_terminator.bash
Normal file
|
@ -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 [arguments]..."
|
||||
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
clap_complete/tests/snapshots/value_terminator.elvish
Normal file
26
clap_complete/tests/snapshots/value_terminator.elvish
Normal file
|
@ -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]
|
||||
}
|
1
clap_complete/tests/snapshots/value_terminator.fish
Normal file
1
clap_complete/tests/snapshots/value_terminator.fish
Normal file
|
@ -0,0 +1 @@
|
|||
complete -c my-app -s h -l help -d 'Print help'
|
32
clap_complete/tests/snapshots/value_terminator.ps1
Normal file
32
clap_complete/tests/snapshots/value_terminator.ps1
Normal file
|
@ -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
clap_complete/tests/snapshots/value_terminator.zsh
Normal file
30
clap_complete/tests/snapshots/value_terminator.zsh
Normal file
|
@ -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]' \
|
||||
'*;::arguments -- multi-valued argument with a value terminator:' \
|
||||
&& ret=0
|
||||
}
|
||||
|
||||
(( $+functions[_my-app_commands] )) ||
|
||||
_my-app_commands() {
|
||||
local commands; commands=()
|
||||
_describe -t commands 'my-app commands' commands "$@"
|
||||
}
|
||||
|
||||
_my-app "$@"
|
|
@ -83,3 +83,15 @@ fn value_hint() {
|
|||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_terminator() {
|
||||
let name = "my-app";
|
||||
let cmd = common::value_terminator_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/value_terminator.zsh",
|
||||
clap_complete::shells::Zsh,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue