Skip to content

fix(complete): Handle COMP_WORDBREAKS in Bash completion#6264

Open
AndreasBackx wants to merge 2 commits intoclap-rs:masterfrom
AndreasBackx:feat/comp-wordbreaks
Open

fix(complete): Handle COMP_WORDBREAKS in Bash completion#6264
AndreasBackx wants to merge 2 commits intoclap-rs:masterfrom
AndreasBackx:feat/comp-wordbreaks

Conversation

@AndreasBackx
Copy link
Copy Markdown
Contributor

@AndreasBackx AndreasBackx commented Feb 13, 2026

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

Resolves #3920

Summary

Adds COMP_WORDBREAKS prefix-stripping to the Bash registration script to prevent double-prefixing
when completing values containing = or :.

When = is in COMP_WORDBREAKS (the default), Bash splits --flag=value into separate words at =. Without this fix, the completion function returns --flag=value but Bash only replaces the part after =, producing --flag=--flag=value.

The fix uses the standard approach (also used by git, docker, aws-cli): strip the prefix up to the last word-break character from each COMPREPLY entry.

Changes

clap_complete/src/env/shells.rs: Added prefix-stripping block to Bash registration script template + unit test asserting the handling exists.

Note on testing

This fix is in the shell registration script (Bash code), not in the Rust completion engine. It cannot be unit-tested without a real Bash shell. The existing PTY-based test complete_dynamic_env_option_value in bash.rs exercises --choice= completion through a real shell and would catch regressions. The unit test verifies the registration script contains the COMP_WORDBREAKS handling logic.

Test plan

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

The Bash registration script does not currently handle COMP_WORDBREAKS,
which causes double-prefixing when completing values containing = or :.
Add prefix-stripping logic to the Bash registration script to prevent
double-prefixing when COMP_WORDBREAKS contains = or :.
@AndreasBackx AndreasBackx marked this pull request as ready for review February 13, 2026 02:03
!script.contains("COMP_WORDBREAKS"),
"registration script does not yet reference COMP_WORDBREAKS"
);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please test behavior, not implementation

#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn fish_env_completer_path_quoting_works() {
use snapbox::assert_data_eq;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this being added in this commit

@feraxhp
Copy link
Copy Markdown

feraxhp commented Feb 23, 2026

i think u must implement a method to allow personalize the behavior, because not every cli tool will need = or : or any other character.

Something similar to the proposal of #6280

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 flags with values in native completions

4 participants