Skip to content

Lazy loading of dynamic completions #5668

@alerque

Description

@alerque

I've been looking forward to dynamic completion support for a long time. I see ZSH support has landed as an unstable feature so I started playing around with a POC on a small project to get an idea how it all goes together.

So far I have...

  1. Enabled features = [ "unstable-dynamic" ] for clap_complete in both my dependencies and build-dependencies.

  2. Added a new subcommand to my CLI using the derive macros similar to the example in the docs:

    /// Used internally by Clap to generate dynamic completions
    #[command(subcommand)]
    pub _complete: Option<CompleteCommand>,
  3. Edited my main function to intercept ARG[1] being 'complete' and handle dynamic completions:

    let cli = Cli::parse();
    if let Some(completions) = cli._complete {
        completions.complete(&mut Cli::command());
    } else {
        // my original app logic
    }
  4. Swapped out the completer on one of my Args:

    /// Optional list of paths to operate on instead of default which is all files tracked by Git
    -#[arg(value_hint = clap::ValueHint::FilePath)]
    +#[arg(add = ArgValueCompleter::new(|| { vec![
    +        CompletionCandidate::new("foo"),
    +        CompletionCandidate::new("bar"),
    +        CompletionCandidate::new("baz")] }))]
    pub paths: Option<Vec<String>>,

This got me as far as being abe to use myapp complete --shell zsh to generate a shell completion function that I could dump to a file manually that later enable dynamic completions.

What I can't figure out is how to use generate_to() (or similar) to output the same completion file with dynamic support at build time:

pub fn generate_to<G, S, T>(

Am I missing some updated way to do that, or has it just not been implemented yet?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-completionArea: completion generator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions