Description
Maintainers notes:
- Assuming this is custom completion on top of clap-driven completion, this is blocked on Stablize Rust-Native Completion Engine Tracking Issue #3166
- Open question: does the completion function accept just the arg and current value or does it have access to
ArgMatches
for completing based on other data like in Compute argument possible values based on other arguments #1910
Like with PossibleValue::help
, we should support help text
Currently clap does basic completion of arguments, however it can't be used to do things like the dynamic completion of git checkout <branch>
.
It would be really neat if this could be handled by clap itself -- AFAICT nothing like this currently exists for any language and it would just be magical since you can then do everything in Rust code.
The rough idea is that the Arg
/Subcommand
builder can take a .with_completion(|args, str| ... )
closure. We pass the
closure the partial parsed list of other args, and the partial string being completed for this arg (may be empty). The closure returns a list of completions.
When you attempt to do something like git branch -D foo<tab>
, the completion script will call git --secret-completion-param 10 -- branch -D foo
, where 10 is the index in the arguments of where <tab>
was pressed, and after the --
we pass all the arguments. Clap parses these arguments except the partial one, and passes this down to the with_completion
closure, which will return a list of completions which we print out (and feed back to bash or whatever).
A simpler version doesn't handle parsing all the other arguments, instead your closure just takes the partial string, so you just provide a closure operating on a string.
If this could be made to work neatly, it would be pretty great.
cc @killercup