clap_complete: Allow access to previous args in completion function #5784
Labels
A-completion
Area: completion generator
C-enhancement
Category: Raise on the bar on expectations
S-waiting-on-design
Status: Waiting on user-facing design to be resolved before implementing
Please complete the following tasks
Clap Version
clap 4.4.2, clap_complete 4.5.29
Describe your use case
In a ArgValueCompleter function, it would be nice to have access to what has been parsed so far when generating completions. My use case is that I'm loading the arguments from a YAML file, but the file to load from can be overridden by a prior
-f
argument. Another identical example is docker-compose run: You can rundocker-compose run <service>
in which case it will load services fromdocker-compose.yml
, or you can dodocker-compose -f different/compose/file.yml run <service>
, in which case it should load fromdifferent/compose/file.yml
.Describe the solution you'd like
Access to what has already been parsed as an argument to the
ArgValueCompleter
function. This would most likely be a breaking change because it requires adding an argument to ValueCompleter::complete. The user impact could be mitigated though by keeping the blanket impl onfn(&OsStr) -> Vec<CompletionCandidate>
. I imagine most users are not implementingValueCompleter
themselves.The best I can think of is the additional argument is just a
&[&OsStr]
containing what's already been parsed. The completer would then be responsible for iterating over that and figuring out the semantic meaning. It'd be great if we could get the prior arguments in a more structured form, but doing that without the full command present is probably not possible. So an example completion function for something likedocker-compose run
would look like:(this is pseudocode, I'm sure it doesn't actually compile but it should get the idea across)
Alternatives, if applicable
std::env::args
and look for-f
or--file
. This requires you to manually skip over the first few items to get to where the arguments start, which is fragileCommand
struct to parsestd::env::args
, but this requires everything already present to constitute a valid command, i.e. it can't be used to complete a required argument (unless at least one character has already been typed)Additional Context
The text was updated successfully, but these errors were encountered: