Skip to content

Commit

Permalink
fix(dynamic): add more completion support for the ci test
Browse files Browse the repository at this point in the history
  • Loading branch information
shannmu committed Jun 14, 2024
1 parent a9f4b5a commit 097482c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
24 changes: 21 additions & 3 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn complete(
} else if arg.is_short() {
if let Some(short) = arg.to_short() {
let mut short = short.clone();
// HACK: Not consider `-fhg` now. During parsing, we assume that ShortFlags are in the format of `-fbar` or `-f=bar`.
// HACK: During completion parsing, we assume that ShortFlags are in the format of `-fbar` or `-f=bar`.
let opt = short.next_flag();
state = if let Some(opt) = opt {
if let Ok(opt) = opt {
Expand Down Expand Up @@ -243,7 +243,7 @@ fn complete_arg(
);
} else if arg.is_negative_number() {
} else if arg.is_short() {
// HACK: Assuming knowledge of -f<TAB>` and `-f=<TAB>` to complete the value of `-f`
// HACK: Assuming knowledge of -f<TAB>` and `-f=<TAB>` to complete the value of `-f`, and `-f<TAB>` to complete another short flag of cmd.
if let Some(short) = arg.to_short() {
let mut short = short.clone();
let opt = short.next_flag();
Expand Down Expand Up @@ -274,6 +274,13 @@ fn complete_arg(
}
}
}

completions.extend(
shorts_and_visible_aliases(cmd)
.into_iter()
.map(|(f, help)| (format!("{}{}", arg.to_value_os().to_string_lossy(), f).into(), help)),
);

}
} else if arg.is_stdio() {
// HACK: Assuming knowledge of is_stdio
Expand All @@ -289,7 +296,18 @@ fn complete_arg(
.map(|(f, help)| (format!("-{}", f).into(), help)),
);
} else if arg.is_empty() {
// NOTE: Do nothing for empty arg.
// Complete all the long and short flag of current command.
completions.extend(
longs_and_visible_aliases(cmd)
.into_iter()
.map(|(f, help)| (format!("--{f}").into(), help)),
);

completions.extend(
shorts_and_visible_aliases(cmd)
.into_iter()
.map(|(f, help)| (format!("-{}", f).into(), help)),
);
}
}

Expand Down
3 changes: 2 additions & 1 deletion clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn suggest_additional_short_flags() {
.short('c')
.action(clap::ArgAction::Count),
);

println!("{}", complete!(cmd, "-"));
snapbox::assert_eq(
snapbox::str![
"-aa
Expand All @@ -119,6 +119,7 @@ fn suggest_subcommand_positional() {
]),
));

println!("{}", complete!(cmd, "hello-world [TAB]"));
snapbox::assert_eq(
snapbox::str![
"--help\tPrint help (see more with '--help')
Expand Down

0 comments on commit 097482c

Please sign in to comment.