Add ability to generate shell completions

This commit is contained in:
2026-03-22 21:04:34 -05:00
parent 7000ecb3bf
commit c13d6e54ee
4 changed files with 69 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ use std::{
process::Command,
};
use clap::CommandFactory;
use colored::Colorize;
use glob::glob;
@@ -41,6 +42,28 @@ enum Subcommand {
},
/// Clean all in progress files
Clean,
/// Utility functions
Utils {
#[clap(subcommand)]
command: UtilSubcommand,
},
}
#[derive(clap::Subcommand)]
enum UtilSubcommand {
/// Generate shell completions
Completions {
/// The shell to generate completions for
shell: ShellCompletions,
},
}
#[derive(Clone, clap::ValueEnum)]
enum ShellCompletions {
Bash,
Fish,
PowerShell,
Zsh,
}
impl App {
@@ -87,6 +110,38 @@ impl App {
}
_ => {}
},
Subcommand::Utils { command } => match command {
UtilSubcommand::Completions { shell } => {
let name = env!("CARGO_PKG_NAME");
let mut command = App::command();
match shell {
ShellCompletions::Bash => clap_complete::generate(
clap_complete::shells::Bash,
&mut command,
name,
&mut std::io::stdout(),
),
ShellCompletions::Fish => clap_complete::generate(
clap_complete::shells::Fish,
&mut command,
name,
&mut std::io::stdout(),
),
ShellCompletions::PowerShell => clap_complete::generate(
clap_complete::shells::PowerShell,
&mut command,
name,
&mut std::io::stdout(),
),
ShellCompletions::Zsh => clap_complete::generate(
clap_complete::shells::Zsh,
&mut command,
name,
&mut std::io::stdout(),
),
}
}
},
}
}
}