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...
-
Enabled features = [ "unstable-dynamic" ] for clap_complete in both my dependencies and build-dependencies.
-
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>,
-
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
}
-
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?
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...
Enabled
features = [ "unstable-dynamic" ]for clap_complete in both my dependencies and build-dependencies.Added a new subcommand to my CLI using the derive macros similar to the example in the docs:
Edited my
mainfunction to intercept ARG[1] being 'complete' and handle dynamic completions:Swapped out the completer on one of my
Args:This got me as far as being abe to use
myapp complete --shell zshto 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:clap/clap_complete/src/generator/mod.rs
Line 191 in 2d8138a
Am I missing some updated way to do that, or has it just not been implemented yet?