Skip to content

Commit 89cae3a

Browse files
committed
fix(complete): Fix handling of multiple arguments
1 parent cad5cde commit 89cae3a

File tree

8 files changed

+129
-122
lines changed

8 files changed

+129
-122
lines changed

clap_complete/src/shells/zsh.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,11 @@ fn write_opts_of(p: &Command, p_global: Option<&Command>) -> String {
449449
let help = escape_help(&o.get_help().unwrap_or_default().to_string());
450450
let conflicts = arg_conflicts(p, o, p_global);
451451

452-
let multiple = "*";
452+
let multiple = if let ArgAction::Count | ArgAction::Append = o.get_action() {
453+
"*"
454+
} else {
455+
""
456+
};
453457

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

548-
let multiple = "*";
552+
let multiple = if let ArgAction::Count | ArgAction::Append = f.get_action() {
553+
"*"
554+
} else {
555+
""
556+
};
549557

550558
if let Some(short) = f.get_short() {
551559
let s = format!(
@@ -620,14 +628,13 @@ fn write_positionals_of(p: &Command) -> String {
620628
debug!("write_positionals_of:iter: arg={}", arg.get_id());
621629

622630
let num_args = arg.get_num_args().expect("built");
623-
let cardinality =
624-
if num_args != builder::ValueRange::EMPTY && num_args != builder::ValueRange::SINGLE {
625-
"*:"
626-
} else if !arg.is_required_set() {
627-
":"
628-
} else {
629-
""
630-
};
631+
let cardinality = if num_args.max_values() > 1 {
632+
"*:"
633+
} else if !arg.is_required_set() {
634+
":"
635+
} else {
636+
""
637+
};
631638

632639
let a = format!(
633640
"'{cardinality}:{name}{help}:{value_completion}' \\",

clap_complete/tests/snapshots/aliases.zsh

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ _my-app() {
1515

1616
local context curcontext="$curcontext" state line
1717
_arguments "${_arguments_options[@]}" \
18-
'*-o+[cmd option]: : ' \
19-
'*-O+[cmd option]: : ' \
20-
'*--option=[cmd option]: : ' \
21-
'*--opt=[cmd option]: : ' \
22-
'*-f[cmd flag]' \
23-
'*-F[cmd flag]' \
24-
'*--flag[cmd flag]' \
25-
'*--flg[cmd flag]' \
26-
'*-h[Print help information]' \
27-
'*--help[Print help information]' \
28-
'*-V[Print version information]' \
29-
'*--version[Print version information]' \
18+
'-o+[cmd option]: : ' \
19+
'-O+[cmd option]: : ' \
20+
'--option=[cmd option]: : ' \
21+
'--opt=[cmd option]: : ' \
22+
'-f[cmd flag]' \
23+
'-F[cmd flag]' \
24+
'--flag[cmd flag]' \
25+
'--flg[cmd flag]' \
26+
'-h[Print help information]' \
27+
'--help[Print help information]' \
28+
'-V[Print version information]' \
29+
'--version[Print version information]' \
3030
'::positional:' \
3131
&& ret=0
3232
}

clap_complete/tests/snapshots/basic.zsh

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ _my-app() {
1515

1616
local context curcontext="$curcontext" state line
1717
_arguments "${_arguments_options[@]}" \
18-
'*-c[]' \
19-
'(-c)*-v[]' \
20-
'*-h[Print help information]' \
21-
'*--help[Print help information]' \
18+
'-c[]' \
19+
'(-c)-v[]' \
20+
'-h[Print help information]' \
21+
'--help[Print help information]' \
2222
":: :_my-app_commands" \
2323
"*::: :->my-app" \
2424
&& ret=0
@@ -31,9 +31,9 @@ _my-app() {
3131
(test)
3232
_arguments "${_arguments_options[@]}" \
3333
'*-d[]' \
34-
'*-c[]' \
35-
'*-h[Print help information]' \
36-
'*--help[Print help information]' \
34+
'-c[]' \
35+
'-h[Print help information]' \
36+
'--help[Print help information]' \
3737
&& ret=0
3838
;;
3939
(help)

clap_complete/tests/snapshots/feature_sample.zsh

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ _my-app() {
1919
'*-C[some config file]' \
2020
'*--config[some config file]' \
2121
'*--conf[some config file]' \
22-
'*-h[Print help information]' \
23-
'*--help[Print help information]' \
24-
'*-V[Print version information]' \
25-
'*--version[Print version information]' \
22+
'-h[Print help information]' \
23+
'--help[Print help information]' \
24+
'-V[Print version information]' \
25+
'--version[Print version information]' \
2626
'::file -- some input file:_files' \
2727
'::choice:(first second)' \
2828
":: :_my-app_commands" \
@@ -36,11 +36,11 @@ _my-app() {
3636
case $line[3] in
3737
(test)
3838
_arguments "${_arguments_options[@]}" \
39-
'*--case=[the case to test]: : ' \
40-
'*-h[Print help information]' \
41-
'*--help[Print help information]' \
42-
'*-V[Print version information]' \
43-
'*--version[Print version information]' \
39+
'--case=[the case to test]: : ' \
40+
'-h[Print help information]' \
41+
'--help[Print help information]' \
42+
'-V[Print version information]' \
43+
'--version[Print version information]' \
4444
&& ret=0
4545
;;
4646
(help)

clap_complete/tests/snapshots/quoting.zsh

+22-22
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ _my-app() {
1515

1616
local context curcontext="$curcontext" state line
1717
_arguments "${_arguments_options[@]}" \
18-
'*--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
19-
'*--double-quotes[Can be "always", "auto", or "never"]' \
20-
'*--backticks[For more information see `echo test`]' \
21-
'*--backslash[Avoid '\''\\n'\'']' \
22-
'*--brackets[List packages \[filter\]]' \
23-
'*--expansions[Execute the shell command with $SHELL]' \
24-
'*-h[Print help information]' \
25-
'*--help[Print help information]' \
26-
'*-V[Print version information]' \
27-
'*--version[Print version information]' \
18+
'--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
19+
'--double-quotes[Can be "always", "auto", or "never"]' \
20+
'--backticks[For more information see `echo test`]' \
21+
'--backslash[Avoid '\''\\n'\'']' \
22+
'--brackets[List packages \[filter\]]' \
23+
'--expansions[Execute the shell command with $SHELL]' \
24+
'-h[Print help information]' \
25+
'--help[Print help information]' \
26+
'-V[Print version information]' \
27+
'--version[Print version information]' \
2828
":: :_my-app_commands" \
2929
"*::: :->my-app" \
3030
&& ret=0
@@ -36,38 +36,38 @@ _my-app() {
3636
case $line[1] in
3737
(cmd-single-quotes)
3838
_arguments "${_arguments_options[@]}" \
39-
'*-h[Print help information]' \
40-
'*--help[Print help information]' \
39+
'-h[Print help information]' \
40+
'--help[Print help information]' \
4141
&& ret=0
4242
;;
4343
(cmd-double-quotes)
4444
_arguments "${_arguments_options[@]}" \
45-
'*-h[Print help information]' \
46-
'*--help[Print help information]' \
45+
'-h[Print help information]' \
46+
'--help[Print help information]' \
4747
&& ret=0
4848
;;
4949
(cmd-backticks)
5050
_arguments "${_arguments_options[@]}" \
51-
'*-h[Print help information]' \
52-
'*--help[Print help information]' \
51+
'-h[Print help information]' \
52+
'--help[Print help information]' \
5353
&& ret=0
5454
;;
5555
(cmd-backslash)
5656
_arguments "${_arguments_options[@]}" \
57-
'*-h[Print help information]' \
58-
'*--help[Print help information]' \
57+
'-h[Print help information]' \
58+
'--help[Print help information]' \
5959
&& ret=0
6060
;;
6161
(cmd-brackets)
6262
_arguments "${_arguments_options[@]}" \
63-
'*-h[Print help information]' \
64-
'*--help[Print help information]' \
63+
'-h[Print help information]' \
64+
'--help[Print help information]' \
6565
&& ret=0
6666
;;
6767
(cmd-expansions)
6868
_arguments "${_arguments_options[@]}" \
69-
'*-h[Print help information]' \
70-
'*--help[Print help information]' \
69+
'-h[Print help information]' \
70+
'--help[Print help information]' \
7171
&& ret=0
7272
;;
7373
(help)

clap_complete/tests/snapshots/special_commands.zsh

+22-22
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ _my-app() {
1919
'*-C[some config file]' \
2020
'*--config[some config file]' \
2121
'*--conf[some config file]' \
22-
'*-h[Print help information]' \
23-
'*--help[Print help information]' \
24-
'*-V[Print version information]' \
25-
'*--version[Print version information]' \
22+
'-h[Print help information]' \
23+
'--help[Print help information]' \
24+
'-V[Print version information]' \
25+
'--version[Print version information]' \
2626
'::file -- some input file:_files' \
2727
'::choice:(first second)' \
2828
":: :_my-app_commands" \
@@ -36,37 +36,37 @@ _my-app() {
3636
case $line[3] in
3737
(test)
3838
_arguments "${_arguments_options[@]}" \
39-
'*--case=[the case to test]: : ' \
40-
'*-h[Print help information]' \
41-
'*--help[Print help information]' \
42-
'*-V[Print version information]' \
43-
'*--version[Print version information]' \
39+
'--case=[the case to test]: : ' \
40+
'-h[Print help information]' \
41+
'--help[Print help information]' \
42+
'-V[Print version information]' \
43+
'--version[Print version information]' \
4444
&& ret=0
4545
;;
4646
(some_cmd)
4747
_arguments "${_arguments_options[@]}" \
48-
'*--config=[the other case to test]: : ' \
49-
'*-h[Print help information]' \
50-
'*--help[Print help information]' \
51-
'*-V[Print version information]' \
52-
'*--version[Print version information]' \
48+
'--config=[the other case to test]: : ' \
49+
'-h[Print help information]' \
50+
'--help[Print help information]' \
51+
'-V[Print version information]' \
52+
'--version[Print version information]' \
5353
'*::path:' \
5454
&& ret=0
5555
;;
5656
(some-cmd-with-hyphens)
5757
_arguments "${_arguments_options[@]}" \
58-
'*-h[Print help information]' \
59-
'*--help[Print help information]' \
60-
'*-V[Print version information]' \
61-
'*--version[Print version information]' \
58+
'-h[Print help information]' \
59+
'--help[Print help information]' \
60+
'-V[Print version information]' \
61+
'--version[Print version information]' \
6262
&& ret=0
6363
;;
6464
(some-hidden-cmd)
6565
_arguments "${_arguments_options[@]}" \
66-
'*-h[Print help information]' \
67-
'*--help[Print help information]' \
68-
'*-V[Print version information]' \
69-
'*--version[Print version information]' \
66+
'-h[Print help information]' \
67+
'--help[Print help information]' \
68+
'-V[Print version information]' \
69+
'--version[Print version information]' \
7070
&& ret=0
7171
;;
7272
(help)

clap_complete/tests/snapshots/sub_subcommands.zsh

+18-18
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ _my-app() {
1919
'*-C[some config file]' \
2020
'*--config[some config file]' \
2121
'*--conf[some config file]' \
22-
'*-h[Print help information]' \
23-
'*--help[Print help information]' \
24-
'*-V[Print version information]' \
25-
'*--version[Print version information]' \
22+
'-h[Print help information]' \
23+
'--help[Print help information]' \
24+
'-V[Print version information]' \
25+
'--version[Print version information]' \
2626
'::file -- some input file:_files' \
2727
'::choice:(first second)' \
2828
":: :_my-app_commands" \
@@ -36,19 +36,19 @@ _my-app() {
3636
case $line[3] in
3737
(test)
3838
_arguments "${_arguments_options[@]}" \
39-
'*--case=[the case to test]: : ' \
40-
'*-h[Print help information]' \
41-
'*--help[Print help information]' \
42-
'*-V[Print version information]' \
43-
'*--version[Print version information]' \
39+
'--case=[the case to test]: : ' \
40+
'-h[Print help information]' \
41+
'--help[Print help information]' \
42+
'-V[Print version information]' \
43+
'--version[Print version information]' \
4444
&& ret=0
4545
;;
4646
(some_cmd)
4747
_arguments "${_arguments_options[@]}" \
48-
'*-h[Print help information]' \
49-
'*--help[Print help information]' \
50-
'*-V[Print version information]' \
51-
'*--version[Print version information]' \
48+
'-h[Print help information]' \
49+
'--help[Print help information]' \
50+
'-V[Print version information]' \
51+
'--version[Print version information]' \
5252
":: :_my-app__some_cmd_commands" \
5353
"*::: :->some_cmd" \
5454
&& ret=0
@@ -61,12 +61,12 @@ _arguments "${_arguments_options[@]}" \
6161
case $line[1] in
6262
(sub_cmd)
6363
_arguments "${_arguments_options[@]}" \
64-
'*--config=[the other case to test]: :((Lest\ quotes,\ aren'\''t\ escaped.\:"help,with,comma"
64+
'--config=[the other case to test]: :((Lest\ quotes,\ aren'\''t\ escaped.\:"help,with,comma"
6565
Second\ to\ trigger\ display\ of\ options\:""))' \
66-
'*-h[Print help information (use `--help` for more detail)]' \
67-
'*--help[Print help information (use `--help` for more detail)]' \
68-
'*-V[Print version information]' \
69-
'*--version[Print version information]' \
66+
'-h[Print help information (use `--help` for more detail)]' \
67+
'--help[Print help information (use `--help` for more detail)]' \
68+
'-V[Print version information]' \
69+
'--version[Print version information]' \
7070
&& ret=0
7171
;;
7272
(help)

0 commit comments

Comments
 (0)