Skip to content

Commit

Permalink
Merge pull request #4278 from intgr/zsh-completion-fix-multiple-args-…
Browse files Browse the repository at this point in the history
…handling

fix(complete): Fix zsh handling of multiple arguments
  • Loading branch information
epage authored Sep 28, 2022
2 parents f6602c5 + 89cae3a commit 7b886d2
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 122 deletions.
27 changes: 17 additions & 10 deletions clap_complete/src/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,11 @@ fn write_opts_of(p: &Command, p_global: Option<&Command>) -> String {
let help = escape_help(&o.get_help().unwrap_or_default().to_string());
let conflicts = arg_conflicts(p, o, p_global);

let multiple = "*";
let multiple = if let ArgAction::Count | ArgAction::Append = o.get_action() {
"*"
} else {
""
};

let vn = match o.get_value_names() {
None => " ".to_string(),
Expand Down Expand Up @@ -545,7 +549,11 @@ fn write_flags_of(p: &Command, p_global: Option<&Command>) -> String {
let help = escape_help(&f.get_help().unwrap_or_default().to_string());
let conflicts = arg_conflicts(p, &f, p_global);

let multiple = "*";
let multiple = if let ArgAction::Count | ArgAction::Append = f.get_action() {
"*"
} else {
""
};

if let Some(short) = f.get_short() {
let s = format!(
Expand Down Expand Up @@ -620,14 +628,13 @@ 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 =
if num_args != builder::ValueRange::EMPTY && num_args != builder::ValueRange::SINGLE {
"*:"
} else if !arg.is_required_set() {
":"
} else {
""
};
let cardinality = if num_args.max_values() > 1 {
"*:"
} else if !arg.is_required_set() {
":"
} else {
""
};

let a = format!(
"'{cardinality}:{name}{help}:{value_completion}' \\",
Expand Down
24 changes: 12 additions & 12 deletions clap_complete/tests/snapshots/aliases.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ _my-app() {

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'*-o+[cmd option]: : ' \
'*-O+[cmd option]: : ' \
'*--option=[cmd option]: : ' \
'*--opt=[cmd option]: : ' \
'*-f[cmd flag]' \
'*-F[cmd flag]' \
'*--flag[cmd flag]' \
'*--flg[cmd flag]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'-o+[cmd option]: : ' \
'-O+[cmd option]: : ' \
'--option=[cmd option]: : ' \
'--opt=[cmd option]: : ' \
'-f[cmd flag]' \
'-F[cmd flag]' \
'--flag[cmd flag]' \
'--flg[cmd flag]' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
'::positional:' \
&& ret=0
}
Expand Down
14 changes: 7 additions & 7 deletions clap_complete/tests/snapshots/basic.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ _my-app() {

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'*-c[]' \
'(-c)*-v[]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'-c[]' \
'(-c)-v[]' \
'-h[Print help information]' \
'--help[Print help information]' \
":: :_my-app_commands" \
"*::: :->my-app" \
&& ret=0
Expand All @@ -31,9 +31,9 @@ _my-app() {
(test)
_arguments "${_arguments_options[@]}" \
'*-d[]' \
'*-c[]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'-c[]' \
'-h[Print help information]' \
'--help[Print help information]' \
&& ret=0
;;
(help)
Expand Down
18 changes: 9 additions & 9 deletions clap_complete/tests/snapshots/feature_sample.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ _my-app() {
'*-C[some config file]' \
'*--config[some config file]' \
'*--conf[some config file]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
'::file -- some input file:_files' \
'::choice:(first second)' \
":: :_my-app_commands" \
Expand All @@ -36,11 +36,11 @@ _my-app() {
case $line[3] in
(test)
_arguments "${_arguments_options[@]}" \
'*--case=[the case to test]: : ' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'--case=[the case to test]: : ' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
&& ret=0
;;
(help)
Expand Down
44 changes: 22 additions & 22 deletions clap_complete/tests/snapshots/quoting.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ _my-app() {

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'*--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
'*--double-quotes[Can be "always", "auto", or "never"]' \
'*--backticks[For more information see `echo test`]' \
'*--backslash[Avoid '\''\\n'\'']' \
'*--brackets[List packages \[filter\]]' \
'*--expansions[Execute the shell command with $SHELL]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
'--double-quotes[Can be "always", "auto", or "never"]' \
'--backticks[For more information see `echo test`]' \
'--backslash[Avoid '\''\\n'\'']' \
'--brackets[List packages \[filter\]]' \
'--expansions[Execute the shell command with $SHELL]' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
":: :_my-app_commands" \
"*::: :->my-app" \
&& ret=0
Expand All @@ -36,38 +36,38 @@ _my-app() {
case $line[1] in
(cmd-single-quotes)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'-h[Print help information]' \
'--help[Print help information]' \
&& ret=0
;;
(cmd-double-quotes)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'-h[Print help information]' \
'--help[Print help information]' \
&& ret=0
;;
(cmd-backticks)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'-h[Print help information]' \
'--help[Print help information]' \
&& ret=0
;;
(cmd-backslash)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'-h[Print help information]' \
'--help[Print help information]' \
&& ret=0
;;
(cmd-brackets)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'-h[Print help information]' \
'--help[Print help information]' \
&& ret=0
;;
(cmd-expansions)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'-h[Print help information]' \
'--help[Print help information]' \
&& ret=0
;;
(help)
Expand Down
44 changes: 22 additions & 22 deletions clap_complete/tests/snapshots/special_commands.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ _my-app() {
'*-C[some config file]' \
'*--config[some config file]' \
'*--conf[some config file]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
'::file -- some input file:_files' \
'::choice:(first second)' \
":: :_my-app_commands" \
Expand All @@ -36,37 +36,37 @@ _my-app() {
case $line[3] in
(test)
_arguments "${_arguments_options[@]}" \
'*--case=[the case to test]: : ' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'--case=[the case to test]: : ' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
&& ret=0
;;
(some_cmd)
_arguments "${_arguments_options[@]}" \
'*--config=[the other case to test]: : ' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'--config=[the other case to test]: : ' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
'*::path:' \
&& ret=0
;;
(some-cmd-with-hyphens)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
&& ret=0
;;
(some-hidden-cmd)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
&& ret=0
;;
(help)
Expand Down
36 changes: 18 additions & 18 deletions clap_complete/tests/snapshots/sub_subcommands.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ _my-app() {
'*-C[some config file]' \
'*--config[some config file]' \
'*--conf[some config file]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
'::file -- some input file:_files' \
'::choice:(first second)' \
":: :_my-app_commands" \
Expand All @@ -36,19 +36,19 @@ _my-app() {
case $line[3] in
(test)
_arguments "${_arguments_options[@]}" \
'*--case=[the case to test]: : ' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'--case=[the case to test]: : ' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
&& ret=0
;;
(some_cmd)
_arguments "${_arguments_options[@]}" \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
":: :_my-app__some_cmd_commands" \
"*::: :->some_cmd" \
&& ret=0
Expand All @@ -61,12 +61,12 @@ _arguments "${_arguments_options[@]}" \
case $line[1] in
(sub_cmd)
_arguments "${_arguments_options[@]}" \
'*--config=[the other case to test]: :((Lest\ quotes,\ aren'\''t\ escaped.\:"help,with,comma"
'--config=[the other case to test]: :((Lest\ quotes,\ aren'\''t\ escaped.\:"help,with,comma"
Second\ to\ trigger\ display\ of\ options\:""))' \
'*-h[Print help information (use `--help` for more detail)]' \
'*--help[Print help information (use `--help` for more detail)]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'-h[Print help information (use `--help` for more detail)]' \
'--help[Print help information (use `--help` for more detail)]' \
'-V[Print version information]' \
'--version[Print version information]' \
&& ret=0
;;
(help)
Expand Down
Loading

0 comments on commit 7b886d2

Please sign in to comment.