Skip to content

Commit

Permalink
test(complete): Add empty option
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 24, 2024
1 parent a1b866d commit 3c14c63
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 21 deletions.
17 changes: 12 additions & 5 deletions clap_complete/examples/exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ fn print_completions<G: Generator>(gen: G, cmd: &mut clap::Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}

const EMPTY: [&str; 0] = [];

#[allow(clippy::let_and_return)]
fn cli() -> clap::Command {
clap::Command::new("exhaustive")
.args([clap::Arg::new("generate")
.long("generate")
.value_name("SHELL")
.value_parser(clap::value_parser!(Shell))
.help("generate")])
.args([
clap::Arg::new("generate")
.long("generate")
.value_name("SHELL")
.value_parser(clap::value_parser!(Shell))
.help("generate"),
clap::Arg::new("empty-choice")
.long("empty-choice")
.value_parser(EMPTY),
])
.subcommands([
clap::Command::new("empty")
.disable_help_subcommand(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ _exhaustive() {

case "${cmd}" in
exhaustive)
opts="-h --generate --help empty global action quote value pacman last alias hint help"
opts="-h --generate --empty-choice --help empty global action quote value pacman last alias hint help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand All @@ -226,6 +226,10 @@ _exhaustive() {
COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}"))
return 0
;;
--empty-choice)
COMPREPLY=($(compgen -W "" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set edit:completion:arg-completer[exhaustive] = {|@words|
var completions = [
&'exhaustive'= {
cand --generate 'generate'
cand --empty-choice 'empty-choice'
cand -h 'Print help'
cand --help 'Print help'
cand empty 'empty'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.
function __fish_exhaustive_global_optspecs
string join \n generate= h/help
string join \n generate= empty-choice= h/help
end

function __fish_exhaustive_needs_command
Expand All @@ -25,6 +25,7 @@ function __fish_exhaustive_using_subcommand
end

complete -c exhaustive -n "__fish_exhaustive_needs_command" -l generate -d 'generate' -r -f -a "{bash\t'',elvish\t'',fish\t'',powershell\t'',zsh\t''}"
complete -c exhaustive -n "__fish_exhaustive_needs_command" -l empty-choice -r -f -a "{}"
complete -c exhaustive -n "__fish_exhaustive_needs_command" -s h -l help -d 'Print help'
complete -c exhaustive -n "__fish_exhaustive_needs_command" -f -a "empty"
complete -c exhaustive -n "__fish_exhaustive_needs_command" -f -a "global"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _exhaustive() {
local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" : \
'--generate=[generate]:SHELL:(bash elvish fish powershell zsh)' \
'--empty-choice=[]: :()' \
'-h[Print help]' \
'--help[Print help]' \
":: :_exhaustive_commands" \
Expand Down
8 changes: 4 additions & 4 deletions clap_complete/tests/testsuite/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ fn complete() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
%
-h --help global quote pacman alias help
--generate empty action value last hint
-h --empty-choice empty action value last hint
--generate --help global quote pacman alias help
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down Expand Up @@ -265,8 +265,8 @@ fn complete_dynamic_env_toplevel() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
%
empty action value last hint --generate
global quote pacman alias help --help
empty action value last hint --generate --help
global quote pacman alias help --empty-choice
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down
9 changes: 5 additions & 4 deletions clap_complete/tests/testsuite/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ fn complete() {

let input = "exhaustive \t";
let expected = snapbox::str![[r#"
% exhaustive --generate
% exhaustive --empty-choice
COMPLETING argument
--empty-choice empty-choice
--generate generate
--help Print help
-h Print help
Expand Down Expand Up @@ -210,10 +211,10 @@ fn complete_dynamic_env_toplevel() {

let input = "exhaustive \t";
let expected = snapbox::str![[r#"
% exhaustive --generate
% exhaustive --empty-choice
COMPLETING argument
--generate action empty help last quote
--help alias global hint pacman value
--empty-choice --help alias global hint pacman value
--generate action empty help last quote
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down
5 changes: 3 additions & 2 deletions clap_complete/tests/testsuite/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ fn complete_dynamic_env_toplevel() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
% exhaustive empty
empty action value last hint --generate (generate)
global quote pacman alias help (Print this message or the help of the given subcommand(s)) --help (Print help)
empty quote last help (Print this message or the help of the given subcommand(s)) --help (Print help)
global value alias --generate (generate)
action pacman hint --empty-choice
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down
9 changes: 5 additions & 4 deletions clap_complete/tests/testsuite/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,11 @@ fn complete_dynamic_env_toplevel() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
% exhaustive
--generate -- generate
--help -- Print help
help -- Print this message or the help of the given subcommand(s)
action alias empty global hint last pacman quote value
--generate -- generate
--help -- Print help
help -- Print this message or the help of the given subcommand(s)
--empty-choice alias global last quote
action empty hint pacman value
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down

0 comments on commit 3c14c63

Please sign in to comment.