From 8e246ef5ee83af7f121a77500900bbe27c636e2d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 23 Dec 2024 19:33:22 -0600 Subject: [PATCH] test(complete): Add empty subcommand --- clap_complete/examples/exhaustive.rs | 3 ++ .../home/static/exhaustive/bash/.bashrc | 38 ++++++++++++++++++- .../static/exhaustive/elvish/elvish/rc.elv | 6 +++ .../fish/fish/completions/exhaustive.fish | 20 +++++----- .../static/exhaustive/zsh/zsh/_exhaustive | 24 +++++++++++- clap_complete/tests/testsuite/bash.rs | 6 +-- clap_complete/tests/testsuite/elvish.rs | 5 ++- clap_complete/tests/testsuite/fish.rs | 10 ++--- clap_complete/tests/testsuite/zsh.rs | 7 ++-- 9 files changed, 93 insertions(+), 26 deletions(-) diff --git a/clap_complete/examples/exhaustive.rs b/clap_complete/examples/exhaustive.rs index ab994ecbadb..bcf107e5304 100644 --- a/clap_complete/examples/exhaustive.rs +++ b/clap_complete/examples/exhaustive.rs @@ -32,6 +32,9 @@ fn cli() -> clap::Command { .value_parser(clap::value_parser!(Shell)) .help("generate")]) .subcommands([ + clap::Command::new("empty") + .disable_help_subcommand(true) + .disable_help_flag(true), clap::Command::new("global") .version("3.0") .propagate_version(true) diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc index 0c2c5d89639..53f03c4c607 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc +++ b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc @@ -20,6 +20,9 @@ _exhaustive() { exhaustive,alias) cmd="exhaustive__alias" ;; + exhaustive,empty) + cmd="exhaustive__empty" + ;; exhaustive,global) cmd="exhaustive__global" ;; @@ -80,6 +83,9 @@ _exhaustive() { exhaustive__help,alias) cmd="exhaustive__help__alias" ;; + exhaustive__help,empty) + cmd="exhaustive__help__empty" + ;; exhaustive__help,global) cmd="exhaustive__help__global" ;; @@ -210,7 +216,7 @@ _exhaustive() { case "${cmd}" in exhaustive) - opts="-h --generate --help global action quote value pacman last alias hint help" + opts="-h --generate --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 @@ -279,6 +285,20 @@ _exhaustive() { COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 ;; + exhaustive__empty) + opts="" + if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + case "${prev}" in + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; exhaustive__global) opts="-h -V --global --help --version one two help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then @@ -448,7 +468,7 @@ _exhaustive() { return 0 ;; exhaustive__help) - opts="global action quote value pacman last alias hint help" + opts="empty global action quote value pacman last alias hint help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -489,6 +509,20 @@ _exhaustive() { COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 ;; + exhaustive__help__empty) + opts="" + if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + case "${prev}" in + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; exhaustive__help__global) opts="one two" if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then 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 7da38bf037f..a04ca1c4442 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 @@ -23,6 +23,7 @@ set edit:completion:arg-completer[exhaustive] = {|@words| cand --generate 'generate' cand -h 'Print help' cand --help 'Print help' + cand empty 'empty' cand global 'global' cand action 'action' cand quote 'quote' @@ -33,6 +34,8 @@ set edit:completion:arg-completer[exhaustive] = {|@words| cand hint 'hint' cand help 'Print this message or the help of the given subcommand(s)' } + &'exhaustive;empty'= { + } &'exhaustive;global'= { cand --global 'everywhere' cand -h 'Print help' @@ -243,6 +246,7 @@ set edit:completion:arg-completer[exhaustive] = {|@words| cand --help 'Print help' } &'exhaustive;help'= { + cand empty 'empty' cand global 'global' cand action 'action' cand quote 'quote' @@ -253,6 +257,8 @@ set edit:completion:arg-completer[exhaustive] = {|@words| cand hint 'hint' cand help 'Print this message or the help of the given subcommand(s)' } + &'exhaustive;help;empty'= { + } &'exhaustive;help;global'= { cand one 'one' cand two 'two' 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 ff1e5f39447..bf5895ff92f 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 @@ -26,6 +26,7 @@ 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" -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" complete -c exhaustive -n "__fish_exhaustive_needs_command" -f -a "action" complete -c exhaustive -n "__fish_exhaustive_needs_command" -f -a "quote" @@ -119,15 +120,16 @@ complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -s H -l host complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -l url -r -f complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -l email -r -f complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -s h -l help -d 'Print help' -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "global" -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "action" -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "quote" -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "value" -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "pacman" -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "last" -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "alias" -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "hint" -complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from global action quote value pacman last alias hint help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "empty" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "global" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "action" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "quote" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "value" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "pacman" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "last" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "alias" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "hint" +complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from empty global action quote value pacman last alias hint help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and __fish_seen_subcommand_from global" -f -a "one" complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and __fish_seen_subcommand_from global" -f -a "two" complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and __fish_seen_subcommand_from quote" -f -a "cmd-single-quotes" -d 'Can be \'always\', \'auto\', or \'never\'' 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 d5b42eca257..957a41c737e 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive +++ b/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive @@ -27,7 +27,11 @@ _exhaustive() { (( CURRENT += 1 )) curcontext="${curcontext%:*:*}:exhaustive-command-$line[1]:" case $line[1] in - (global) + (empty) +_arguments "${_arguments_options[@]}" : \ +&& ret=0 +;; +(global) _arguments "${_arguments_options[@]}" : \ '--global[everywhere]' \ '-h[Print help]' \ @@ -413,7 +417,11 @@ _arguments "${_arguments_options[@]}" : \ (( CURRENT += 1 )) curcontext="${curcontext%:*:*}:exhaustive-help-command-$line[1]:" case $line[1] in - (global) + (empty) +_arguments "${_arguments_options[@]}" : \ +&& ret=0 +;; +(global) _arguments "${_arguments_options[@]}" : \ ":: :_exhaustive__help__global_commands" \ "*::: :->global" \ @@ -557,6 +565,7 @@ esac (( $+functions[_exhaustive_commands] )) || _exhaustive_commands() { local commands; commands=( +'empty:' \ 'global:' \ 'action:' \ 'quote:' \ @@ -579,6 +588,11 @@ _exhaustive__alias_commands() { local commands; commands=() _describe -t commands 'exhaustive alias commands' commands "$@" } +(( $+functions[_exhaustive__empty_commands] )) || +_exhaustive__empty_commands() { + local commands; commands=() + _describe -t commands 'exhaustive empty commands' commands "$@" +} (( $+functions[_exhaustive__global_commands] )) || _exhaustive__global_commands() { local commands; commands=( @@ -658,6 +672,7 @@ _exhaustive__global__two_commands() { (( $+functions[_exhaustive__help_commands] )) || _exhaustive__help_commands() { local commands; commands=( +'empty:' \ 'global:' \ 'action:' \ 'quote:' \ @@ -680,6 +695,11 @@ _exhaustive__help__alias_commands() { local commands; commands=() _describe -t commands 'exhaustive help alias commands' commands "$@" } +(( $+functions[_exhaustive__help__empty_commands] )) || +_exhaustive__help__empty_commands() { + local commands; commands=() + _describe -t commands 'exhaustive help empty commands' commands "$@" +} (( $+functions[_exhaustive__help__global_commands] )) || _exhaustive__help__global_commands() { local commands; commands=( diff --git a/clap_complete/tests/testsuite/bash.rs b/clap_complete/tests/testsuite/bash.rs index 79297c810f5..635a189f2d6 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 action value last hint ---generate global quote pacman alias help +-h --help global quote pacman alias help +--generate empty action value last hint "#]]; let actual = runtime.complete(input, &term).unwrap(); assert_data_eq!(actual, expected); @@ -260,8 +260,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 -action value last hint --generate "#]]; 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 6f485d6fb60..d04124b1d78 100644 --- a/clap_complete/tests/testsuite/elvish.rs +++ b/clap_complete/tests/testsuite/elvish.rs @@ -169,6 +169,7 @@ fn complete_static_toplevel() { -h Print help action action alias alias +empty empty global global help Print this message or the help of the given subcommand(s) hint hint @@ -203,8 +204,8 @@ fn complete_dynamic_env_toplevel() { let expected = snapbox::str![[r#" % exhaustive --generate COMPLETING argument ---generate action global hint pacman value ---help alias help last quote +--generate action empty help last quote +--help alias global hint pacman value "#]]; 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 06c4725f4d9..4fed1faca2d 100644 --- a/clap_complete/tests/testsuite/fish.rs +++ b/clap_complete/tests/testsuite/fish.rs @@ -163,8 +163,8 @@ fn complete() { let input = "exhaustive \t"; let expected = snapbox::str![[r#" % exhaustive -action global hint pacman value -alias help (Print this message or the help of the given subcommand(s)) last quote +action empty help (Print this message or the help of the given subcommand(s)) last quote +alias global hint pacman value "#]]; let actual = runtime.complete(input, &term).unwrap(); assert_data_eq!(actual, expected); @@ -198,9 +198,9 @@ fn complete_dynamic_env_toplevel() { let input = "exhaustive \t\t"; let expected = snapbox::str![[r#" -% exhaustive global -global quote pacman alias help (Print this message or the help of the given subcommand(s)) --help (Print help) -action value last hint --generate (generate) +% 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) "#]]; 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 c5d6d31272c..0c5392849e4 100644 --- a/clap_complete/tests/testsuite/zsh.rs +++ b/clap_complete/tests/testsuite/zsh.rs @@ -164,8 +164,9 @@ fn complete() { let input = "exhaustive \t"; let expected = snapbox::str![[r#" % exhaustive -help -- Print this message or the help of the given subcommand(s) -pacman action global alias value quote hint last -- +help -- Print this message or the help of the given subcommand(s) +hint +pacman action global alias value quote empty last -- "#]]; let actual = runtime.complete(input, &term).unwrap(); assert_data_eq!(actual, expected); @@ -195,7 +196,7 @@ fn complete_dynamic_env_toplevel() { --generate -- generate --help -- Print help help -- Print this message or the help of the given subcommand(s) -action alias global hint last pacman quote value +action alias empty global hint last pacman quote value "#]]; let actual = runtime.complete(input, &term).unwrap(); assert_data_eq!(actual, expected);