1use clap::{CommandFactory, ValueEnum};
2use clap_complete::{Generator, Shell, generate};
3
4use crate::cli::Cli;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
7pub enum CompletionShell {
8 Bash,
9 Zsh,
10}
11
12pub fn run(shell: CompletionShell) -> i32 {
13 let mut command = Cli::command();
14 let bin_name = command.get_name().to_string();
15
16 match shell {
17 CompletionShell::Bash => print_completion(Shell::Bash, &mut command, &bin_name),
18 CompletionShell::Zsh => print_completion(Shell::Zsh, &mut command, &bin_name),
19 }
20
21 0
22}
23
24fn print_completion<G: Generator>(generator: G, command: &mut clap::Command, bin_name: &str) {
25 generate(generator, command, bin_name, &mut std::io::stdout());
26}