Reduces boilerplate for adding a completion command to Clap
```rust use clap::{IntoApp, Parser, Subcommand};
struct Cli { #[clap(subcommand)] command: Commands, }
enum Commands { /// Generate shell completions Completion { /// The shell to generate the completions for #[clap(argenum)] shell: clapcomplete_command::Shell, }, }
fn main() { let cli = Cli::parse();
match cli.command {
// e.g. `$ cli completion bash`
Commands::Completion { shell } => {
shell.generate(
&mut Cli::command(),
env!("CARGO_PKG_NAME"),
&mut std::io::stdout(),
);
}
}
} ```
```rs use clap::{Arg, Command};
fn buildcli() -> Command<'static> { Command::new(env!("CARGOPKGNAME")).arg( Arg::new("completion") .help("Generate shell completions") .possiblevalues(clapcompletecommand::Shell::possible_values()), ) }
fn main() { let matches = buildcli().getmatches();
// e.g. `$ cli bash`
if let Ok(shell) = matches.value_of_t::<clap_complete_command::Shell>("completion") {
let mut command = build_cli();
shell.generate(&mut command, env!("CARGO_PKG_NAME"), &mut std::io::stdout());
}
} ```
The supported shells can be seen in clap_complete_command::Shell
.