From 89cae3a2b3a56f7e02fcfef3b05625a30c235a08 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Wed, 28 Sep 2022 22:44:27 +0300 Subject: [PATCH] fix(complete): Fix handling of multiple arguments --- clap_complete/src/shells/zsh.rs | 27 +++++++----- clap_complete/tests/snapshots/aliases.zsh | 24 +++++----- clap_complete/tests/snapshots/basic.zsh | 14 +++--- .../tests/snapshots/feature_sample.zsh | 18 ++++---- clap_complete/tests/snapshots/quoting.zsh | 44 +++++++++---------- .../tests/snapshots/special_commands.zsh | 44 +++++++++---------- .../tests/snapshots/sub_subcommands.zsh | 36 +++++++-------- clap_complete/tests/snapshots/value_hint.zsh | 44 +++++++++---------- 8 files changed, 129 insertions(+), 122 deletions(-) diff --git a/clap_complete/src/shells/zsh.rs b/clap_complete/src/shells/zsh.rs index 894b5f1487f..d4fc63ba711 100644 --- a/clap_complete/src/shells/zsh.rs +++ b/clap_complete/src/shells/zsh.rs @@ -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(), @@ -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!( @@ -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}' \\", diff --git a/clap_complete/tests/snapshots/aliases.zsh b/clap_complete/tests/snapshots/aliases.zsh index dcb879cbdaf..b97cde4156a 100644 --- a/clap_complete/tests/snapshots/aliases.zsh +++ b/clap_complete/tests/snapshots/aliases.zsh @@ -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 } diff --git a/clap_complete/tests/snapshots/basic.zsh b/clap_complete/tests/snapshots/basic.zsh index 6da2ae451e0..56959696862 100644 --- a/clap_complete/tests/snapshots/basic.zsh +++ b/clap_complete/tests/snapshots/basic.zsh @@ -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 @@ -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) diff --git a/clap_complete/tests/snapshots/feature_sample.zsh b/clap_complete/tests/snapshots/feature_sample.zsh index 88e8960034e..4bc3c3aa7ff 100644 --- a/clap_complete/tests/snapshots/feature_sample.zsh +++ b/clap_complete/tests/snapshots/feature_sample.zsh @@ -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" \ @@ -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) diff --git a/clap_complete/tests/snapshots/quoting.zsh b/clap_complete/tests/snapshots/quoting.zsh index 3ff1a25627a..d8d9c21b8be 100644 --- a/clap_complete/tests/snapshots/quoting.zsh +++ b/clap_complete/tests/snapshots/quoting.zsh @@ -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 @@ -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) diff --git a/clap_complete/tests/snapshots/special_commands.zsh b/clap_complete/tests/snapshots/special_commands.zsh index 37a004711e2..0d0ced687e0 100644 --- a/clap_complete/tests/snapshots/special_commands.zsh +++ b/clap_complete/tests/snapshots/special_commands.zsh @@ -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" \ @@ -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) diff --git a/clap_complete/tests/snapshots/sub_subcommands.zsh b/clap_complete/tests/snapshots/sub_subcommands.zsh index be4378d7290..922613ba209 100644 --- a/clap_complete/tests/snapshots/sub_subcommands.zsh +++ b/clap_complete/tests/snapshots/sub_subcommands.zsh @@ -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" \ @@ -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 @@ -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) diff --git a/clap_complete/tests/snapshots/value_hint.zsh b/clap_complete/tests/snapshots/value_hint.zsh index 3db2f312ec0..d865e4caeea 100644 --- a/clap_complete/tests/snapshots/value_hint.zsh +++ b/clap_complete/tests/snapshots/value_hint.zsh @@ -15,28 +15,28 @@ _my-app() { local context curcontext="$curcontext" state line _arguments "${_arguments_options[@]}" \ -'*--choice=[]: :(bash fish zsh)' \ -'*--unknown=[]: : ' \ -'*--other=[]: :( )' \ -'*-p+[]: :_files' \ -'*--path=[]: :_files' \ -'*-f+[]: :_files' \ -'*--file=[]: :_files' \ -'*-d+[]: :_files -/' \ -'*--dir=[]: :_files -/' \ -'*-e+[]: :_absolute_command_paths' \ -'*--exe=[]: :_absolute_command_paths' \ -'*--cmd-name=[]: :_command_names -e' \ -'*-c+[]: :_cmdstring' \ -'*--cmd=[]: :_cmdstring' \ -'*-u+[]: :_users' \ -'*--user=[]: :_users' \ -'*-H+[]: :_hosts' \ -'*--host=[]: :_hosts' \ -'*--url=[]: :_urls' \ -'*--email=[]: :_email_addresses' \ -'*-h[Print help information]' \ -'*--help[Print help information]' \ +'--choice=[]: :(bash fish zsh)' \ +'--unknown=[]: : ' \ +'--other=[]: :( )' \ +'-p+[]: :_files' \ +'--path=[]: :_files' \ +'-f+[]: :_files' \ +'--file=[]: :_files' \ +'-d+[]: :_files -/' \ +'--dir=[]: :_files -/' \ +'-e+[]: :_absolute_command_paths' \ +'--exe=[]: :_absolute_command_paths' \ +'--cmd-name=[]: :_command_names -e' \ +'-c+[]: :_cmdstring' \ +'--cmd=[]: :_cmdstring' \ +'-u+[]: :_users' \ +'--user=[]: :_users' \ +'-H+[]: :_hosts' \ +'--host=[]: :_hosts' \ +'--url=[]: :_urls' \ +'--email=[]: :_email_addresses' \ +'-h[Print help information]' \ +'--help[Print help information]' \ '*::command_with_args:_cmdambivalent' \ && ret=0 }