mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
feat(complete): Make '--register' optional
This commit is contained in:
parent
fa4ccec694
commit
734cac730c
5 changed files with 38 additions and 42 deletions
|
@ -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 {
|
||||
/// Path to write completion-registration to
|
||||
#[arg(long)]
|
||||
register: Option<std::path::PathBuf>,
|
||||
|
||||
#[arg(raw = true, hide = true, conflicts_with = "register")]
|
||||
comp_words: Option<Vec<OsString>>,
|
||||
|
||||
/// Specify shell to complete for
|
||||
#[arg(long)]
|
||||
shell: Option<Shell>,
|
||||
|
||||
/// Path to write completion-registration to
|
||||
#[arg(long, required = true)]
|
||||
register: Option<std::path::PathBuf>,
|
||||
|
||||
#[arg(raw = true, hide_short_help = true, group = "complete")]
|
||||
comp_words: Vec<OsString>,
|
||||
}
|
||||
|
||||
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(())
|
||||
|
|
|
@ -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=()
|
||||
;;
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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:' \
|
||||
|
|
Loading…
Reference in a new issue