From d0a7083c1480fe69edf95190314a36c360f4414e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 23 Dec 2024 19:52:03 -0600 Subject: [PATCH] test(complete): Add empty option --- clap_complete/examples/exhaustive.rs | 17 ++++++++++++----- .../home/static/exhaustive/bash/.bashrc | 6 +++++- .../home/static/exhaustive/elvish/elvish/rc.elv | 1 + .../fish/fish/completions/exhaustive.fish | 3 ++- .../home/static/exhaustive/zsh/zsh/_exhaustive | 1 + clap_complete/tests/testsuite/bash.rs | 8 ++++---- clap_complete/tests/testsuite/elvish.rs | 9 +++++---- clap_complete/tests/testsuite/fish.rs | 5 +++-- clap_complete/tests/testsuite/zsh.rs | 9 +++++---- 9 files changed, 38 insertions(+), 21 deletions(-) diff --git a/clap_complete/examples/exhaustive.rs b/clap_complete/examples/exhaustive.rs index bcf107e5304..1cc3be281f0 100644 --- a/clap_complete/examples/exhaustive.rs +++ b/clap_complete/examples/exhaustive.rs @@ -23,14 +23,21 @@ fn print_completions(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) diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc index 53f03c4c607..6fba6ddc7ff 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc +++ b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc @@ -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 @@ -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=() ;; diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/elvish/elvish/rc.elv b/clap_complete/tests/snapshots/home/static/exhaustive/elvish/elvish/rc.elv index a04ca1c4442..c164d23056c 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/elvish/elvish/rc.elv +++ b/clap_complete/tests/snapshots/home/static/exhaustive/elvish/elvish/rc.elv @@ -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' diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish b/clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish index bf5895ff92f..926edab8ac8 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish +++ b/clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish @@ -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 @@ -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" diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive b/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive index 957a41c737e..24733787a30 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive +++ b/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive @@ -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" \ diff --git a/clap_complete/tests/testsuite/bash.rs b/clap_complete/tests/testsuite/bash.rs index 257cbfd9c55..6658b0660bb 100644 --- a/clap_complete/tests/testsuite/bash.rs +++ b/clap_complete/tests/testsuite/bash.rs @@ -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); @@ -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); diff --git a/clap_complete/tests/testsuite/elvish.rs b/clap_complete/tests/testsuite/elvish.rs index e9843c4d31b..2c1e9bdf13a 100644 --- a/clap_complete/tests/testsuite/elvish.rs +++ b/clap_complete/tests/testsuite/elvish.rs @@ -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 @@ -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); diff --git a/clap_complete/tests/testsuite/fish.rs b/clap_complete/tests/testsuite/fish.rs index 518ae0737c3..03e498b9cf4 100644 --- a/clap_complete/tests/testsuite/fish.rs +++ b/clap_complete/tests/testsuite/fish.rs @@ -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); diff --git a/clap_complete/tests/testsuite/zsh.rs b/clap_complete/tests/testsuite/zsh.rs index 2afbb97ce14..e8973aa9940 100644 --- a/clap_complete/tests/testsuite/zsh.rs +++ b/clap_complete/tests/testsuite/zsh.rs @@ -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) +action empty global last quote +alias --empty-choice hint pacman value "#]]; let actual = runtime.complete(input, &term).unwrap(); assert_data_eq!(actual, expected);