DocsIntegratingIntegrationsClap

Fig Autocomplete & clap

This tutorial will walk you through setting up Fig's integration with the Rust CLI framework, clap.

Add clap_complete_fig to your project

Ensure the following dependencies are in your Cargo.toml.

[dependencies]
clap = { version = "3.1", features = ["derive"] }
clap_complete = "3.1"
clap_complete_fig = "3.1"

Generate the spec

There are two ways to generate the spec using clap_complete_fig, you can create the spec at runtime or at build.

Generate spec at runtime (Easy)

Using the

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

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

#[derive(Subcommand)]
enum Commands {
    GenerateFigSpec,
}

fn main() {
    let cli = Cli::parse();

    match &cli.command {
        Commands::GenerateFigSpec => clap_complete::generate(
            clap_complete_fig::Fig,
            &mut Cli::command(),
            "my-cli",
            &mut std::io::stdout(),
        ),
    }
}

Now if you run my-cli generate-fig-spec you'll see it output a Fig completion spec!

Generate spec at build (Advanced)

In your build.rs file, which should be a top level file in the same directory as you Cargo.toml you can generate the spec using the following format. You must ensure that your

use your_app::App;
use clap::IntoApp;
use ;

fn main() {
    clap_complete::generate_to(
        clap_complete_fig::Fig,
        &mut App::into_app(),
        "my-cli",
        "fig_spec",
    )
    .expect("Unable to generate Fig spec");
}

Next Steps: now that you have successfully configured the integration you can either push the spec to the autocomplete repo or push it to Fig's cloud.