Skip to content

Commit b256739

Browse files
authored
Merge pull request #6131 from mernen/do-not-suggest-opts-after-escape
fix(complete): do not suggest options after escape (`--`)
2 parents 281f8ae + 8aaf704 commit b256739

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

clap_complete/src/engine/complete.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ pub fn complete(
4747
arg.to_value_os(),
4848
);
4949
if cursor == target_cursor {
50-
return complete_arg(&arg, current_cmd, current_dir, pos_index, current_state);
50+
return complete_arg(
51+
&arg,
52+
current_cmd,
53+
current_dir,
54+
pos_index,
55+
is_escaped,
56+
current_state,
57+
);
5158
}
5259

5360
if let Ok(value) = arg.to_value() {
@@ -133,6 +140,7 @@ fn complete_arg(
133140
cmd: &clap::Command,
134141
current_dir: Option<&std::path::Path>,
135142
pos_index: usize,
143+
is_escaped: bool,
136144
state: ParseState<'_>,
137145
) -> Result<Vec<CompletionCandidate>, std::io::Error> {
138146
debug!(
@@ -157,7 +165,9 @@ fn complete_arg(
157165
{
158166
completions.extend(complete_arg_value(arg.to_value(), positional, current_dir));
159167
}
160-
completions.extend(complete_option(arg, cmd, current_dir));
168+
if !is_escaped {
169+
completions.extend(complete_option(arg, cmd, current_dir));
170+
}
161171
}
162172
ParseState::Pos((_, num_arg)) => {
163173
if let Some(positional) = cmd
@@ -183,6 +193,7 @@ fn complete_arg(
183193
cmd,
184194
current_dir,
185195
pos_index,
196+
is_escaped,
186197
ParseState::ValueDone,
187198
)?);
188199
}

clap_complete/tests/testsuite/engine.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,26 @@ goodbye-world
263263
);
264264
}
265265

266+
#[test]
267+
fn suggest_subcommand_positional_after_escape() {
268+
let mut cmd = Command::new("exhaustive").subcommand(Command::new("hello-world").arg(
269+
clap::Arg::new("hello-world").value_parser([
270+
PossibleValue::new("hello-world").help("Say hello to the world"),
271+
"hello-moon".into(),
272+
"goodbye-world".into(),
273+
]),
274+
));
275+
276+
assert_data_eq!(
277+
complete!(cmd, "hello-world -- [TAB]"),
278+
snapbox::str![[r#"
279+
hello-world Say hello to the world
280+
hello-moon
281+
goodbye-world
282+
"#]],
283+
);
284+
}
285+
266286
#[test]
267287
fn suggest_argument_value() {
268288
let mut cmd = Command::new("dynamic")
@@ -1034,8 +1054,6 @@ comma,tab
10341054
a_pos
10351055
b_pos
10361056
c_pos
1037-
--delimiter
1038-
--help Print help
10391057
"#]]
10401058
);
10411059

0 commit comments

Comments
 (0)