Skip to content

feat(complete): Add ValueCompleter::complete_at for indexed multi-value completion#6353

Merged
epage merged 2 commits intoclap-rs:masterfrom
truffle-dev:feat-completer-arg-index
Apr 27, 2026
Merged

feat(complete): Add ValueCompleter::complete_at for indexed multi-value completion#6353
epage merged 2 commits intoclap-rs:masterfrom
truffle-dev:feat-completer-arg-index

Conversation

@truffle-dev
Copy link
Copy Markdown
Contributor

Closes #6284.

Adds ValueCompleter::complete_at(arg_index, current) so a custom completer
can return different candidates for each value of a multi-value argument.
The motivating case from the issue: --set-upstream <REMOTE> <BRANCH> should
complete remotes at index 0 and branches at index 1.

The new method has a default implementation that delegates to complete, so
existing implementations of ValueCompleter (including all closure-based
completers) keep working unchanged. arg_index is the zero-based position
within the argument's num_args range and is unaffected by value_delimiter.

Engine plumbing computes the index from the existing ParseState:

  • ParseState::Opt((opt, count)): arg_index = count - 1
  • ParseState::Pos((_, num_arg)): arg_index = num_arg - 1
  • All other call sites pass 0 (single-value paths).

The new test suggest_custom_arg_completer_at_index covers both indices for
a num_args(2) flag, including prefix-filtered completions, and confirms
the default complete_at still defers to complete for the unindexed case.

Comment thread clap_complete/src/engine/custom.rs Outdated
Comment thread clap_complete/tests/testsuite/engine.rs
@truffle-dev truffle-dev force-pushed the feat-completer-arg-index branch from 6ca55b8 to 7b23bd2 Compare April 25, 2026 23:12
Snapshot existing behavior for a custom `ValueCompleter` on a
multi-value option. Without an index-aware API, the completer
cannot differentiate by argument position, so both the first and
second values surface the same merged candidate set.

This pins down the baseline so a follow-up commit can show the
behavior change in the test diff.
Add `ValueCompleter::complete_at(arg_index, current)` and a
matching `ArgValueCompleter::complete_at` shim. The default
trait impl delegates to `complete`, so existing implementors are
unaffected. The dynamic engine now threads each value's position
within an argument's `num_args` range into the completer, so a
multi-value argument can return different candidates per slot
(for example, a remote name first and a branch name second for
`--set-upstream <REMOTE> <BRANCH>`).

`arg_index` counts shell arguments and is unaffected by
`value_delimiter`.

Closes clap-rs#6284
@truffle-dev truffle-dev force-pushed the feat-completer-arg-index branch from 7b23bd2 to ac0d148 Compare April 25, 2026 23:15
@truffle-dev
Copy link
Copy Markdown
Contributor Author

  1. Dropped the doc prose from custom.rs per "leave this off" — ac0d148.
  2. Reordered into a baseline-test commit followed by the impl commit so the diff in the impl commit shows the behavior change — 1565a3c, ac0d148.

Copy link
Copy Markdown
Member

@epage epage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge when I have a chance to release

@epage epage merged commit 9706951 into clap-rs:master Apr 27, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(clap_complete): Aditional context for Arguments with multiple args ["unstable-dynamic"]

2 participants