Skip to content

Commit

Permalink
feat(clap_complete): Support multiple values after flags in native co…
Browse files Browse the repository at this point in the history
…mpletions
  • Loading branch information
shannmu committed Jul 30, 2024
1 parent 5d8c84b commit 75a45e5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
35 changes: 28 additions & 7 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn complete(
if value.is_some() {
ParseState::ValueDone
} else {
ParseState::Opt(opt.unwrap())
ParseState::Opt((opt.unwrap(), 1))
}
}
Some(clap::ArgAction::SetTrue) | Some(clap::ArgAction::SetFalse) => {
Expand All @@ -115,7 +115,7 @@ pub fn complete(
Some(opt) => {
state = match short.next_value_os() {
Some(_) => ParseState::ValueDone,
None => ParseState::Opt(opt),
None => ParseState::Opt((opt, 1)),
};
}
None => {
Expand All @@ -128,9 +128,19 @@ pub fn complete(
pos_index += 1;
state = ParseState::ValueDone;
}
ParseState::Opt(_) => {
state = ParseState::ValueDone;
}
ParseState::Opt((ref opt, count)) => match opt.get_num_args() {
Some(range) => {
let max = range.max_values();
if count < max {
state = ParseState::Opt((opt.clone(), count + 1));
} else {
state = ParseState::ValueDone;
}
}
None => {
state = ParseState::ValueDone;
}
},
}
}
}
Expand All @@ -150,7 +160,7 @@ enum ParseState<'a> {
Pos(usize),

/// Parsing a optional flag argument
Opt(&'a clap::Arg),
Opt((&'a clap::Arg, usize)),
}

fn complete_arg(
Expand Down Expand Up @@ -298,8 +308,19 @@ fn complete_arg(
completions.extend(complete_arg_value(arg.to_value(), positional, current_dir));
}
}
ParseState::Opt(opt) => {
ParseState::Opt((opt, count)) => {
completions.extend(complete_arg_value(arg.to_value(), opt, current_dir));
let min = opt.get_num_args().map(|r| r.min_values()).unwrap_or(0);
if count > min {
// Also complete this raw_arg as a positional argument, flags, options and subcommand.
completions.extend(complete_arg(
arg,
cmd,
current_dir,
pos_index,
ParseState::ValueDone,
)?);
}
}
}
if completions.iter().any(|a| a.is_visible()) {
Expand Down
28 changes: 14 additions & 14 deletions clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,9 @@ val3"
assert_data_eq!(
complete!(cmd, "--certain-num val1 [TAB]"),
snapbox::str![
"--certain-num
--uncertain-num
--help\tPrint help
-Y
-N
-h\tPrint help"
"val1
val2
val3"
]
);

Expand Down Expand Up @@ -502,7 +499,10 @@ val3"
assert_data_eq!(
complete!(cmd, "--uncertain-num val1 [TAB]"),
snapbox::str![
"--certain-num
"val1
val2
val3
--certain-num
--uncertain-num
--help\tPrint help
-Y
Expand Down Expand Up @@ -535,12 +535,9 @@ val3"
assert_data_eq!(
complete!(cmd, "-Y val1 [TAB]"),
snapbox::str![
"--certain-num
--uncertain-num
--help\tPrint help
-Y
-N
-h\tPrint help"
"val1
val2
val3"
]
);

Expand Down Expand Up @@ -568,7 +565,10 @@ val3"
assert_data_eq!(
complete!(cmd, "-N val1 [TAB]"),
snapbox::str![
"--certain-num
"val1
val2
val3
--certain-num
--uncertain-num
--help\tPrint help
-Y
Expand Down

0 comments on commit 75a45e5

Please sign in to comment.