From 734cac730ccaaafc92115d6a03f326a2eb90b707 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 9 Aug 2024 16:16:15 -0500 Subject: [PATCH] feat(complete): Make '--register' optional --- clap_complete/src/dynamic/shells/mod.rs | 54 +++++++++---------- .../home/static/exhaustive/bash/.bashrc | 10 ++-- .../static/exhaustive/elvish/elvish/rc.elv | 6 +-- .../fish/fish/completions/exhaustive.fish | 4 +- .../static/exhaustive/zsh/zsh/_exhaustive | 6 +-- 5 files changed, 38 insertions(+), 42 deletions(-) diff --git a/clap_complete/src/dynamic/shells/mod.rs b/clap_complete/src/dynamic/shells/mod.rs index ba415b22bd1..ff06a6a0923 100644 --- a/clap_complete/src/dynamic/shells/mod.rs +++ b/clap_complete/src/dynamic/shells/mod.rs @@ -161,45 +161,41 @@ impl CompleteCommand { /// /// Bash /// ```bash -/// echo "source <(your_program complete --shell bash --register -)" >> ~/.bashrc +/// echo "source <(your_program complete --shell bash)" >> ~/.bashrc /// ``` /// /// Elvish /// ```elvish -/// echo "eval (your_program complete --shell elvish --register -)" >> ~/.elvish/rc.elv +/// echo "eval (your_program complete --shell elvish)" >> ~/.elvish/rc.elv /// ``` /// /// Fish /// ```fish -/// echo "source (your_program complete --shell fish --register - | psub)" >> ~/.config/fish/config.fish +/// echo "source (your_program complete --shell fish | psub)" >> ~/.config/fish/config.fish /// ``` /// /// Powershell /// ```powershell -/// echo "your_program complete --shell powershell --register - | Invoke-Expression" >> $PROFILE +/// echo "your_program complete --shell powershell | Invoke-Expression" >> $PROFILE /// ``` /// /// Zsh /// ```zsh -/// echo "source <(your_program complete --shell zsh --register -)" >> ~/.zshrc +/// echo "source <(your_program complete --shell zsh)" >> ~/.zshrc /// ``` -#[derive(clap::Args)] -#[command(arg_required_else_help = true)] -#[command(group = clap::ArgGroup::new("complete").multiple(true).conflicts_with("register"))] -#[allow(missing_docs)] -#[derive(Clone, Debug)] +#[derive(clap::Args, Clone, Debug)] #[command(about = None, long_about = None)] pub struct CompleteArgs { - /// Specify shell to complete for - #[arg(long)] - shell: Option, - /// Path to write completion-registration to - #[arg(long, required = true)] + #[arg(long)] register: Option, - #[arg(raw = true, hide_short_help = true, group = "complete")] - comp_words: Vec, + #[arg(raw = true, hide = true, conflicts_with = "register")] + comp_words: Option>, + + /// Specify shell to complete for + #[arg(long)] + shell: Option, } impl CompleteArgs { @@ -223,10 +219,21 @@ impl CompleteArgs { .or_else(|| Shell::from_env()) .unwrap_or(Shell::Bash); - if let Some(out_path) = self.register.as_deref() { + if let Some(comp_words) = self.comp_words.as_ref() { + let current_dir = std::env::current_dir().ok(); + let mut buf = Vec::new(); + shell.write_complete(cmd, comp_words.clone(), current_dir.as_deref(), &mut buf)?; + std::io::stdout().write_all(&buf)?; + } else { + let out_path = self + .register + .as_deref() + .unwrap_or(std::path::Path::new("-")); let name = cmd.get_name(); let bin = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name()); + + let mut buf = Vec::new(); shell.write_registration(name, bin, bin, &mut buf)?; if out_path == std::path::Path::new("-") { std::io::stdout().write_all(&buf)?; @@ -236,17 +243,6 @@ impl CompleteArgs { } else { std::fs::write(out_path, buf)?; } - } else { - let current_dir = std::env::current_dir().ok(); - - let mut buf = Vec::new(); - shell.write_complete( - cmd, - self.comp_words.clone(), - current_dir.as_deref(), - &mut buf, - )?; - std::io::stdout().write_all(&buf)?; } Ok(()) diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc index 49c9960ba11..2c7aaa02782 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc +++ b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc @@ -238,20 +238,20 @@ _exhaustive() { return 0 ;; exhaustive__complete) - opts="-h -V --shell --register --global --help --version [COMP_WORDS]..." + opts="-h -V --register --shell --global --help --version [COMP_WORDS]..." 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 - ;; --register) COMPREPLY=($(compgen -f "${cur}")) return 0 ;; + --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 c8253260263..1b63da9fe5f 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,11 +239,11 @@ set edit:completion:arg-completer[exhaustive] = {|@words| cand --version 'Print version' } &'exhaustive;complete'= { - cand --shell 'Specify shell to complete for' cand --register 'Path to write completion-registration to' + cand --shell 'Specify shell to complete for' cand --global 'everywhere' - cand -h 'Print help (see more with ''--help'')' - cand --help 'Print help (see more with ''--help'')' + cand -h 'Print help' + cand --help 'Print help' cand -V 'Print version' cand --version 'Print version' } 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 7b28a7cd611..b66c67ef164 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,10 +136,10 @@ 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 register -d 'Path to write completion-registration to' -r -F +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 (see more with \'--help\')' +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' complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from action quote value pacman last alias hint complete help" -f -a "action" complete -c exhaustive -n "__fish_exhaustive_using_subcommand help; and not __fish_seen_subcommand_from action quote value pacman last alias hint complete help" -f -a "quote" 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 d596f71e25a..e9dd916293a 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive +++ b/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive @@ -325,11 +325,11 @@ _arguments "${_arguments_options[@]}" : \ ;; (complete) _arguments "${_arguments_options[@]}" : \ -'--shell=[Specify shell to complete for]:SHELL:(bash elvish fish powershell zsh)' \ '--register=[Path to write completion-registration to]:REGISTER:_files' \ +'--shell=[Specify shell to complete for]:SHELL:(bash elvish fish powershell zsh)' \ '--global[everywhere]' \ -'-h[Print help (see more with '\''--help'\'')]' \ -'--help[Print help (see more with '\''--help'\'')]' \ +'-h[Print help]' \ +'--help[Print help]' \ '-V[Print version]' \ '--version[Print version]' \ '*::comp_words:' \