use crate::commands;
use clap::Subcommand;
use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh};
#[derive(Subcommand)]
pub enum CompletionCommands {
Completion {
#[arg(value_name = "SHELL")]
shell: String,
#[arg(short, long)]
install: bool,
},
}
impl CompletionCommands {
pub fn execute(self) -> Result<(), crate::error::CliError> {
match self {
CompletionCommands::Completion { shell, install } => {
if install {
println!("{}", commands::print_installation_instructions(&shell));
Ok(())
} else {
generate_completion(&shell)
}
}
}
}
}
fn generate_completion(shell: &str) -> Result<(), crate::error::CliError> {
use crate::create_cli_command;
use crate::error::CliError;
let mut cmd = create_cli_command();
match shell.to_lowercase().as_str() {
"bash" => commands::generate_completion_for_command(Bash, &mut cmd),
"zsh" => commands::generate_completion_for_command(Zsh, &mut cmd),
"fish" => commands::generate_completion_for_command(Fish, &mut cmd),
"powershell" | "pwsh" => commands::generate_completion_for_command(PowerShell, &mut cmd),
"elvish" => commands::generate_completion_for_command(Elvish, &mut cmd),
_ => Err(CliError::invalid_input(format!(
"Unsupported shell: '{shell}'. Supported shells: bash, zsh, fish, powershell, elvish"
))),
}
}