Expand description

Examples

Derive

use clap::{IntoApp, Parser, Subcommand};

#[derive(Parser)]
struct Cli {
    #[clap(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Generate shell completions
    Completion {
        /// The shell to generate the completions for
        #[clap(arg_enum)]
        shell: clap_complete_command::Shell,
    },
}

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(),
        );
    }
}

Builder

use clap::{Arg, Command};

fn build_cli() -> Command<'static> {
    Command::new(env!("CARGO_PKG_NAME")).arg(
        Arg::new("completion")
            .help("Generate shell completions")
            .possible_values(clap_complete_command::Shell::possible_values()),
    )
}

let matches = build_cli().get_matches();

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());
}

Enums

A clap::ArgEnum for available shell completions.