Skip to content

Commit 535785f

Browse files
committed
feat(complete): support require_equals in completion engine
1 parent 88c519e commit 535785f

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

clap_complete/src/engine/complete.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ pub fn complete(
8888
});
8989

9090
if let Some(opt) = opt {
91-
if opt.get_num_args().expect("built").takes_values() && value.is_none() {
91+
if opt.get_num_args().expect("built").takes_values()
92+
&& value.is_none()
93+
&& !opt.is_require_equals_set()
94+
{
9295
next_state = ParseState::Opt((opt, 1));
9396
};
9497
} else if pos_allows_hyphen(current_cmd, pos_index) {
@@ -478,9 +481,16 @@ fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
478481
p.get_arguments()
479482
.filter_map(|a| {
480483
a.get_long_and_visible_aliases().map(|longs| {
481-
longs
482-
.into_iter()
483-
.map(|s| populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a))
484+
longs.into_iter().map(|s| {
485+
let suffix = if a.is_require_equals_set()
486+
&& a.get_num_args().expect("built").takes_values()
487+
{
488+
"="
489+
} else {
490+
""
491+
};
492+
populate_arg_candidate(CompletionCandidate::new(format!("--{s}{suffix}")), a)
493+
})
484494
})
485495
})
486496
.flatten()
@@ -495,7 +505,15 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
495505
.filter_map(|a| {
496506
a.get_aliases().map(|longs| {
497507
longs.into_iter().map(|s| {
498-
populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a).hide(true)
508+
let suffix = if a.is_require_equals_set()
509+
&& a.get_num_args().expect("built").takes_values()
510+
{
511+
"="
512+
} else {
513+
""
514+
};
515+
populate_arg_candidate(CompletionCandidate::new(format!("--{s}{suffix}")), a)
516+
.hide(true)
499517
})
500518
})
501519
})

clap_complete/tests/testsuite/engine.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ fn suggest_require_equals() {
14621462
assert_data_eq!(
14631463
complete!(cmd, " [TAB]"),
14641464
snapbox::str![[r#"
1465-
--format
1465+
--format=
14661466
--name
14671467
--help Print help
14681468
"#]]
@@ -1485,13 +1485,12 @@ fn suggest_require_equals() {
14851485
);
14861486

14871487
// When typing --format (space), should NOT suggest values since require_equals forbids it
1488-
// Current behavior: suggests values (this will change with the fix)
14891488
assert_data_eq!(
14901489
complete!(cmd, "--format [TAB]"),
14911490
snapbox::str![[r#"
1492-
json
1493-
yaml
1494-
toml
1491+
--format=
1492+
--name
1493+
--help Print help
14951494
"#]]
14961495
);
14971496
}

0 commit comments

Comments
 (0)