Skip to content

feat(complete): Support require_equals in completion engine#6260

Open
AndreasBackx wants to merge 2 commits intoclap-rs:masterfrom
AndreasBackx:feat/require-equals-completion
Open

feat(complete): Support require_equals in completion engine#6260
AndreasBackx wants to merge 2 commits intoclap-rs:masterfrom
AndreasBackx:feat/require-equals-completion

Conversation

@AndreasBackx
Copy link
Contributor

This PR was generated with the assistance of an AI agent and reviewed by me.

Resolves #3923

Summary

When an argument has require_equals(true) set, the completion engine now:

  • Shows --flag= (with trailing =) instead of --flag when listing options
  • No longer suggests values after --flag (space-separated), since the parser would reject that form

Changes

clap_complete/src/engine/complete.rs:

  1. Parsing loop: skip entering ParseState::Opt for require_equals options encountered space-separated
  2. longs_and_visible_aliases() / hidden_longs_aliases(): append = to candidates for require_equals args

clap_complete/tests/testsuite/engine.rs: Added suggest_require_equals test

Test plan

  • cargo test -p clap_complete --features unstable-dynamic — all 108 tests pass
  • cargo clippy -p clap_complete --features unstable-dynamic — clean

@AndreasBackx AndreasBackx changed the title feat(complete): support require_equals in completion engine feat(complete): Support require_equals in completion engine Feb 13, 2026
@AndreasBackx AndreasBackx marked this pull request as ready for review February 13, 2026 01:17
@AndreasBackx AndreasBackx marked this pull request as draft February 13, 2026 01:19
@AndreasBackx AndreasBackx force-pushed the feat/require-equals-completion branch from 5c91848 to 535785f Compare February 13, 2026 01:38
@AndreasBackx AndreasBackx marked this pull request as ready for review February 13, 2026 01:43
Comment on lines +1447 to +1460
fn suggest_require_equals() {
let mut cmd = Command::new("exhaustive")
.arg(
clap::Arg::new("format")
.long("format")
.require_equals(true)
.value_parser(["json", "yaml", "toml"]),
)
.arg(
clap::Arg::new("name")
.long("name")
.short('n'),
);

Copy link
Member

Choose a reason for hiding this comment

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

We need a test case with num_args(0..=1) like at https://docs.rs/clap/latest/src/git/git.rs.html#25-31 which is a major use case for this feature

Comment on lines +91 to +94
if opt.get_num_args().expect("built").takes_values()
&& value.is_none()
&& !opt.is_require_equals_set()
{
Copy link
Member

Choose a reason for hiding this comment

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

This change is for

No longer suggests values after --flag (space-separated), since the parser would reject that form

The intention might not be obvious in the code. Would be good to find a way to make this clear

.map(|s| populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a))
longs.into_iter().map(|s| {
let suffix = if a.is_require_equals_set()
&& a.get_num_args().expect("built").takes_values()
Copy link
Member

Choose a reason for hiding this comment

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

takes_values is redundant

}

/// Gets all the long hidden aliases and flags of a [`clap::Command`].
fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
Copy link
Member

Choose a reason for hiding this comment

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

shorts_and_visible_aliases is not covered

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.

Support is_require_equal_set in native completions

3 participants