Module aot

Source
Expand description

Prebuilt completions

§Quick Start

Shell is a convenience enum for an argument value type that implements Generator for each natively-supported shell type.

To customize completions, see

§Example

use clap::{Command, Arg, ValueHint, value_parser, ArgAction};
use clap_complete::{generate, Generator, Shell};
use std::io;

fn build_cli() -> Command {
    Command::new("example")
         .arg(Arg::new("file")
             .help("some input file")
                .value_hint(ValueHint::AnyPath),
        )
       .arg(
           Arg::new("generator")
               .long("generate")
               .action(ArgAction::Set)
               .value_parser(value_parser!(Shell)),
       )
}

fn print_completions<G: Generator>(generator: G, cmd: &mut Command) {
    generate(generator, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn main() {
    let matches = build_cli().get_matches();

    if let Some(generator) = matches.get_one::<Shell>("generator").copied() {
        let mut cmd = build_cli();
        eprintln!("Generating completion file for {generator}...");
        print_completions(generator, &mut cmd);
    }
}

Modules§

utils
Helpers for writing generators

Structs§

Bash
Generate bash completion file
Elvish
Generate elvish completion file
Fish
Generate fish completion file
PowerShell
Generate powershell completion file
Zsh
Generate zsh completion file

Enums§

Shell
Shell with auto-generated completion script available.
ValueHint
Provide shell with hint on how to complete an argument.

Traits§

Generator
Generator trait which can be used to write generators

Functions§

generate
Generate a completions file for a specified shell at runtime.
generate_to
Generate a completions file for a specified shell at compile-time.