use std::io::Write;
use anyhow::Result;
use clap::Command;
use clap_complete::Shell;
#[derive(clap::ValueEnum, Clone, Copy, Debug)]
pub enum CompletionShell {
Bash,
Elvish,
Powershell,
Zsh,
}
impl From<CompletionShell> for Shell {
fn from(shell: CompletionShell) -> Self {
match shell {
CompletionShell::Bash => Shell::Bash,
CompletionShell::Elvish => Shell::Elvish,
CompletionShell::Powershell => Shell::PowerShell,
CompletionShell::Zsh => Shell::Zsh,
}
}
}
#[derive(clap::Args, Debug, Clone)]
pub struct CompletionsArgs {
#[arg(value_enum)]
pub shell: CompletionShell,
}
pub fn handle_completions_cmd(mut root: Command, args: CompletionsArgs) -> Result<()> {
let bin_name = root.get_name().to_owned();
let mut script = Vec::new();
clap_complete::generate(Shell::from(args.shell), &mut root, bin_name, &mut script);
std::io::stdout().lock().write_all(&script)?;
Ok(())
}