Skip to content

Commit 3a2fb25

Browse files
authored
Merge pull request #5547 from tesuji/fish-list
clap_complete: Pass a list to `__fish_seen_subcommand_from`
2 parents 5cc44bb + 1e3681b commit 3a2fb25

File tree

9 files changed

+140
-139
lines changed

9 files changed

+140
-139
lines changed

clap_complete/src/shells/fish.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,23 @@ fn gen_fish_inner(
6767
basic_template.push_str(" -n \"__fish_use_subcommand\"");
6868
}
6969
} else {
70-
basic_template.push_str(
71-
format!(
72-
" -n \"{}\"",
73-
parent_commands
74-
.iter()
75-
.map(|command| format!("__fish_seen_subcommand_from {command}"))
76-
.chain(
77-
cmd.get_subcommands()
78-
.flat_map(Command::get_name_and_visible_aliases)
79-
.map(|name| format!("not __fish_seen_subcommand_from {name}"))
80-
)
81-
.collect::<Vec<_>>()
82-
.join("; and ")
83-
)
84-
.as_str(),
85-
);
70+
let mut out = String::from("__fish_seen_subcommand_from");
71+
for &command in parent_commands {
72+
out.push(' ');
73+
out.push_str(command);
74+
}
75+
let subcommands: Vec<&str> = cmd
76+
.get_subcommands()
77+
.flat_map(Command::get_name_and_visible_aliases)
78+
.collect();
79+
if !subcommands.is_empty() {
80+
out.push_str("; and not __fish_seen_subcommand_from");
81+
}
82+
for name in subcommands {
83+
out.push(' ');
84+
out.push_str(name);
85+
}
86+
basic_template.push_str(format!(" -n \"{out}\"").as_str());
8687
}
8788

8889
debug!("gen_fish_inner: parent_commands={parent_commands:?}");

clap_complete/tests/snapshots/basic.fish

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this messag
66
complete -c my-app -n "__fish_seen_subcommand_from test" -s d
77
complete -c my-app -n "__fish_seen_subcommand_from test" -s c
88
complete -c my-app -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help'
9-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand with a second line'
10-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
9+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test help" -f -a "test" -d 'Subcommand with a second line'
10+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'

clap_complete/tests/snapshots/custom_bin_name.fish

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ complete -c bin-name -n "__fish_use_subcommand" -f -a "help" -d 'Print this mess
66
complete -c bin-name -n "__fish_seen_subcommand_from test" -s d
77
complete -c bin-name -n "__fish_seen_subcommand_from test" -s c
88
complete -c bin-name -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help'
9-
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand with a second line'
10-
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
9+
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test help" -f -a "test" -d 'Subcommand with a second line'
10+
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'

clap_complete/tests/snapshots/feature_sample.fish

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this messag
66
complete -c my-app -n "__fish_seen_subcommand_from test" -l case -d 'the case to test' -r
77
complete -c my-app -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help'
88
complete -c my-app -n "__fish_seen_subcommand_from test" -s V -l version -d 'Print version'
9-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'tests things'
10-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
9+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test help" -f -a "test" -d 'tests things'
10+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'

clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish

+80-80
Large diffs are not rendered by default.

clap_complete/tests/snapshots/quoting.fish

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ complete -c my-app -n "__fish_seen_subcommand_from cmd-backticks" -s h -l help -
1919
complete -c my-app -n "__fish_seen_subcommand_from cmd-backslash" -s h -l help -d 'Print help'
2020
complete -c my-app -n "__fish_seen_subcommand_from cmd-brackets" -s h -l help -d 'Print help'
2121
complete -c my-app -n "__fish_seen_subcommand_from cmd-expansions" -s h -l help -d 'Print help'
22-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-single-quotes" -d 'Can be \'always\', \'auto\', or \'never\''
23-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-double-quotes" -d 'Can be "always", "auto", or "never"'
24-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-backticks" -d 'For more information see `echo test`'
25-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-backslash" -d 'Avoid \'\\n\''
26-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-brackets" -d 'List packages [filter]'
27-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-expansions" -d 'Execute the shell command with $SHELL'
28-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
22+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions help" -f -a "cmd-single-quotes" -d 'Can be \'always\', \'auto\', or \'never\''
23+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions help" -f -a "cmd-double-quotes" -d 'Can be "always", "auto", or "never"'
24+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions help" -f -a "cmd-backticks" -d 'For more information see `echo test`'
25+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions help" -f -a "cmd-backslash" -d 'Avoid \'\\n\''
26+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions help" -f -a "cmd-brackets" -d 'List packages [filter]'
27+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions help" -f -a "cmd-expansions" -d 'Execute the shell command with $SHELL'
28+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'

clap_complete/tests/snapshots/special_commands.fish

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ complete -c my-app -n "__fish_seen_subcommand_from some-cmd-with-hyphens" -s h -
1616
complete -c my-app -n "__fish_seen_subcommand_from some-cmd-with-hyphens" -s V -l version -d 'Print version'
1717
complete -c my-app -n "__fish_seen_subcommand_from some-hidden-cmd" -s h -l help -d 'Print help'
1818
complete -c my-app -n "__fish_seen_subcommand_from some-hidden-cmd" -s V -l version -d 'Print version'
19-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from some_cmd; and not __fish_seen_subcommand_from some-cmd-with-hyphens; and not __fish_seen_subcommand_from some-hidden-cmd; and not __fish_seen_subcommand_from help" -f -a "test" -d 'tests things'
20-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from some_cmd; and not __fish_seen_subcommand_from some-cmd-with-hyphens; and not __fish_seen_subcommand_from some-hidden-cmd; and not __fish_seen_subcommand_from help" -f -a "some_cmd" -d 'tests other things'
21-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from some_cmd; and not __fish_seen_subcommand_from some-cmd-with-hyphens; and not __fish_seen_subcommand_from some-hidden-cmd; and not __fish_seen_subcommand_from help" -f -a "some-cmd-with-hyphens"
22-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from some_cmd; and not __fish_seen_subcommand_from some-cmd-with-hyphens; and not __fish_seen_subcommand_from some-hidden-cmd; and not __fish_seen_subcommand_from help" -f -a "some-hidden-cmd"
23-
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from some_cmd; and not __fish_seen_subcommand_from some-cmd-with-hyphens; and not __fish_seen_subcommand_from some-hidden-cmd; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
19+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test some_cmd some-cmd-with-hyphens some-hidden-cmd help" -f -a "test" -d 'tests things'
20+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test some_cmd some-cmd-with-hyphens some-hidden-cmd help" -f -a "some_cmd" -d 'tests other things'
21+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test some_cmd some-cmd-with-hyphens some-hidden-cmd help" -f -a "some-cmd-with-hyphens"
22+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test some_cmd some-cmd-with-hyphens some-hidden-cmd help" -f -a "some-hidden-cmd"
23+
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test some_cmd some-cmd-with-hyphens some-hidden-cmd help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'

0 commit comments

Comments
 (0)