Add ability to generate shell completions
This commit is contained in:
55
src/app.rs
55
src/app.rs
@@ -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(),
|
||||
),
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user