From 64bf287e6895b677ed754d98e18f200681ae1868 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 10 Aug 2024 19:38:59 -0500 Subject: [PATCH] fix(complete)!: Switch `--shell` to a positional This matches how I've seen it done elsewhere and removes extra boilerplate --- clap_complete/src/dynamic/shells/command.rs | 38 +++++++++---------- .../home/dynamic/exhaustive/bash/.bashrc | 2 +- .../dynamic/exhaustive/elvish/elvish/rc.elv | 2 +- .../fish/fish/completions/exhaustive.fish | 2 +- .../dynamic/exhaustive/zsh/zsh/_exhaustive | 2 +- .../home/static/exhaustive/bash/.bashrc | 6 +-- .../static/exhaustive/elvish/elvish/rc.elv | 1 - .../fish/fish/completions/exhaustive.fish | 1 - .../static/exhaustive/zsh/zsh/_exhaustive | 2 +- .../tests/snapshots/register_minimal.bash | 2 +- clap_complete/tests/testsuite/common.rs | 2 +- 11 files changed, 27 insertions(+), 33 deletions(-) diff --git a/clap_complete/src/dynamic/shells/command.rs b/clap_complete/src/dynamic/shells/command.rs index 248c7b5d..36db2fb9 100644 --- a/clap_complete/src/dynamic/shells/command.rs +++ b/clap_complete/src/dynamic/shells/command.rs @@ -50,27 +50,27 @@ use super::Shell; /// /// Bash /// ```bash -/// echo "source <(your_program complete --shell bash --register -)" >> ~/.bashrc +/// echo "source <(your_program complete bash --register -)" >> ~/.bashrc /// ``` /// /// Elvish /// ```elvish -/// echo "eval (your_program complete --shell elvish --register -)" >> ~/.elvish/rc.elv +/// echo "eval (your_program complete elvish --register -)" >> ~/.elvish/rc.elv /// ``` /// /// Fish /// ```fish -/// echo "source (your_program complete --shell fish --register - | psub)" >> ~/.config/fish/config.fish +/// echo "source (your_program complete fish --register - | psub)" >> ~/.config/fish/config.fish /// ``` /// /// Powershell /// ```powershell -/// echo "your_program complete --shell powershell --register - | Invoke-Expression" >> $PROFILE +/// echo "your_program complete powershell --register - | Invoke-Expression" >> $PROFILE /// ``` /// /// Zsh /// ```zsh -/// echo "source <(your_program complete --shell zsh --register -)" >> ~/.zshrc +/// echo "source <(your_program complete zsh --register -)" >> ~/.zshrc /// ``` #[derive(clap::Subcommand)] #[allow(missing_docs)] @@ -149,37 +149,37 @@ impl CompleteCommand { /// /// Bash /// ```bash -/// echo "source <(your_program complete --shell bash)" >> ~/.bashrc +/// echo "source <(your_program complete bash)" >> ~/.bashrc /// ``` /// /// Elvish /// ```elvish -/// echo "eval (your_program complete --shell elvish)" >> ~/.elvish/rc.elv +/// echo "eval (your_program complete elvish)" >> ~/.elvish/rc.elv /// ``` /// /// Fish /// ```fish -/// echo "source (your_program complete --shell fish | psub)" >> ~/.config/fish/config.fish +/// echo "source (your_program complete fish | psub)" >> ~/.config/fish/config.fish /// ``` /// /// Powershell /// ```powershell -/// echo "your_program complete --shell powershell | Invoke-Expression" >> $PROFILE +/// echo "your_program complete powershell | Invoke-Expression" >> $PROFILE /// ``` /// /// Zsh /// ```zsh -/// echo "source <(your_program complete --shell zsh)" >> ~/.zshrc +/// echo "source <(your_program complete zsh)" >> ~/.zshrc /// ``` #[derive(clap::Args, Clone, Debug)] #[command(about = None, long_about = None)] pub struct CompleteArgs { + /// Specify shell to complete for + #[arg(value_name = "NAME")] + shell: Option, + #[arg(raw = true, value_name = "ARG", hide = true)] comp_words: Option>, - - /// Specify shell to complete for - #[arg(long, value_name = "NAME")] - shell: Option, } impl CompleteArgs { @@ -290,7 +290,7 @@ _clap_complete_NAME() { else export _CLAP_COMPLETE_SPACE=true fi - COMPREPLY=( $("COMPLETER" complete --shell bash -- "${COMP_WORDS[@]}") ) + COMPREPLY=( $("COMPLETER" complete bash -- "${COMP_WORDS[@]}") ) if [[ $? != 0 ]]; then unset COMPREPLY elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then @@ -401,7 +401,7 @@ set edit:completion:arg-completer[BIN] = { |@words| set index = (- $index 1) set E:_CLAP_COMPLETE_INDEX = (to-string $index) - put (COMPLETER complete --shell elvish -- $@words) | to-lines + put (COMPLETER complete elvish -- $@words) | to-lines } "# .replace("COMPLETER", &completer) @@ -451,7 +451,7 @@ impl ShellCompleter for super::Fish { writeln!( buf, - r#"complete -x -c {bin} -a "("'{completer}'" complete --shell fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""# + r#"complete -x -c {bin} -a "("'{completer}'" complete fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""# ) } fn write_complete( @@ -501,7 +501,7 @@ impl ShellCompleter for super::Powershell { Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{ param($wordToComplete, $commandAst, $cursorPosition) - $results = Invoke-Expression "&{completer} complete --shell powershell -- $($commandAst.ToString())"; + $results = Invoke-Expression "&{completer} complete powershell -- $($commandAst.ToString())"; $results | ForEach-Object {{ $split = $_.Split("`t"); $cmd = $split[0]; @@ -565,7 +565,7 @@ function _clap_dynamic_completer() { export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1) export _CLAP_IFS=$'\n' - local completions=("${(@f)$(COMPLETER complete --shell zsh -- ${words} 2>/dev/null)}") + local completions=("${(@f)$(COMPLETER complete zsh -- ${words} 2>/dev/null)}") if [[ -n $completions ]]; then compadd -a completions diff --git a/clap_complete/tests/snapshots/home/dynamic/exhaustive/bash/.bashrc b/clap_complete/tests/snapshots/home/dynamic/exhaustive/bash/.bashrc index f1bd2f1f..b10dc26e 100644 --- a/clap_complete/tests/snapshots/home/dynamic/exhaustive/bash/.bashrc +++ b/clap_complete/tests/snapshots/home/dynamic/exhaustive/bash/.bashrc @@ -10,7 +10,7 @@ _clap_complete_exhaustive() { else export _CLAP_COMPLETE_SPACE=true fi - COMPREPLY=( $("exhaustive" complete --shell bash -- "${COMP_WORDS[@]}") ) + COMPREPLY=( $("exhaustive" complete bash -- "${COMP_WORDS[@]}") ) if [[ $? != 0 ]]; then unset COMPREPLY elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then diff --git a/clap_complete/tests/snapshots/home/dynamic/exhaustive/elvish/elvish/rc.elv b/clap_complete/tests/snapshots/home/dynamic/exhaustive/elvish/elvish/rc.elv index 2e54dd4f..614e4a29 100644 --- a/clap_complete/tests/snapshots/home/dynamic/exhaustive/elvish/elvish/rc.elv +++ b/clap_complete/tests/snapshots/home/dynamic/exhaustive/elvish/elvish/rc.elv @@ -8,7 +8,7 @@ set edit:completion:arg-completer[exhaustive] = { |@words| set index = (- $index 1) set E:_CLAP_COMPLETE_INDEX = (to-string $index) - put (exhaustive complete --shell elvish -- $@words) | to-lines + put (exhaustive complete elvish -- $@words) | to-lines } diff --git a/clap_complete/tests/snapshots/home/dynamic/exhaustive/fish/fish/completions/exhaustive.fish b/clap_complete/tests/snapshots/home/dynamic/exhaustive/fish/fish/completions/exhaustive.fish index cfb35d74..34301ce9 100644 --- a/clap_complete/tests/snapshots/home/dynamic/exhaustive/fish/fish/completions/exhaustive.fish +++ b/clap_complete/tests/snapshots/home/dynamic/exhaustive/fish/fish/completions/exhaustive.fish @@ -1 +1 @@ -complete -x -c exhaustive -a "("'exhaustive'" complete --shell fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))" +complete -x -c exhaustive -a "("'exhaustive'" complete fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))" diff --git a/clap_complete/tests/snapshots/home/dynamic/exhaustive/zsh/zsh/_exhaustive b/clap_complete/tests/snapshots/home/dynamic/exhaustive/zsh/zsh/_exhaustive index 97d2d1ab..a7082462 100644 --- a/clap_complete/tests/snapshots/home/dynamic/exhaustive/zsh/zsh/_exhaustive +++ b/clap_complete/tests/snapshots/home/dynamic/exhaustive/zsh/zsh/_exhaustive @@ -3,7 +3,7 @@ function _clap_dynamic_completer() { export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1) export _CLAP_IFS=$'\n' - local completions=("${(@f)$(exhaustive complete --shell zsh -- ${words} 2>/dev/null)}") + local completions=("${(@f)$(exhaustive complete zsh -- ${words} 2>/dev/null)}") if [[ -n $completions ]]; then compadd -a completions diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc index 751c6459..68a5a1a1 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc +++ b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc @@ -238,16 +238,12 @@ _exhaustive() { return 0 ;; exhaustive__complete) - opts="-h -V --shell --global --help --version [ARG]..." + opts="-h -V --global --help --version bash elvish fish powershell zsh [ARG]..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 fi case "${prev}" in - --shell) - COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${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 e58c03ed..2fc89206 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 @@ -239,7 +239,6 @@ set edit:completion:arg-completer[exhaustive] = {|@words| cand --version 'Print version' } &'exhaustive;complete'= { - cand --shell 'Specify shell to complete for' cand --global 'everywhere' cand -h 'Print help' cand --help 'Print help' 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 9b4840b0..92d6d42e 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 @@ -136,7 +136,6 @@ complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -l email -r complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -l global -d 'everywhere' complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -s h -l help -d 'Print help' complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -s V -l version -d 'Print version' -complete -c exhaustive -n "__fish_exhaustive_using_subcommand complete" -l shell -d 'Specify shell to complete for' -r -f -a "{bash\t'',elvish\t'',fish\t'',powershell\t'',zsh\t''}" complete -c exhaustive -n "__fish_exhaustive_using_subcommand complete" -l global -d 'everywhere' complete -c exhaustive -n "__fish_exhaustive_using_subcommand complete" -s h -l help -d 'Print help' complete -c exhaustive -n "__fish_exhaustive_using_subcommand complete" -s V -l version -d 'Print version' 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 1f95ed07..73f57fdf 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive +++ b/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive @@ -325,12 +325,12 @@ _arguments "${_arguments_options[@]}" : \ ;; (complete) _arguments "${_arguments_options[@]}" : \ -'--shell=[Specify shell to complete for]:NAME:(bash elvish fish powershell zsh)' \ '--global[everywhere]' \ '-h[Print help]' \ '--help[Print help]' \ '-V[Print version]' \ '--version[Print version]' \ +'::shell -- Specify shell to complete for:(bash elvish fish powershell zsh)' \ '*::comp_words:' \ && ret=0 ;; diff --git a/clap_complete/tests/snapshots/register_minimal.bash b/clap_complete/tests/snapshots/register_minimal.bash index 7198bfe1..a111d3d9 100644 --- a/clap_complete/tests/snapshots/register_minimal.bash +++ b/clap_complete/tests/snapshots/register_minimal.bash @@ -8,7 +8,7 @@ _clap_complete_my_app() { else export _CLAP_COMPLETE_SPACE=true fi - COMPREPLY=( $("my-app" complete --shell bash -- "${COMP_WORDS[@]}") ) + COMPREPLY=( $("my-app" complete bash -- "${COMP_WORDS[@]}") ) if [[ $? != 0 ]]; then unset COMPREPLY elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then diff --git a/clap_complete/tests/testsuite/common.rs b/clap_complete/tests/testsuite/common.rs index c174731a..29d0f6df 100644 --- a/clap_complete/tests/testsuite/common.rs +++ b/clap_complete/tests/testsuite/common.rs @@ -330,7 +330,7 @@ pub(crate) fn register_example(context: &str, nam let mut registration = std::process::Command::new(&bin_path); match context { "static" => registration.args([format!("--generate={shell_name}")]), - "dynamic" => registration.args(["complete".to_owned(), format!("--shell={shell_name}")]), + "dynamic" => registration.args(["complete", shell_name]), _ => unreachable!("unsupported context {}", context), }; let registration = registration.output().unwrap();