Skip to content

Commit

Permalink
Merge pull request #5037 from epage/term
Browse files Browse the repository at this point in the history
fix(parser): Value terminator has higher precedence than later multiple values
  • Loading branch information
epage authored Jul 21, 2023
2 parents 727ca29 + 8bee728 commit 5540d20
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,21 @@ impl<'cmd> Parser<'cmd> {
.map(|p_name| !p_name.is_last_set())
.unwrap_or_default();

let is_terminated = self
.cmd
.get_keymap()
.get(&pos_counter)
.map(|a| a.get_value_terminator().is_some())
.unwrap_or_default();

let missing_pos = self.cmd.is_allow_missing_positional_set()
&& is_second_to_last
&& !trailing_values;

debug!("Parser::get_matches_with: Positional counter...{pos_counter}");
debug!("Parser::get_matches_with: Low index multiples...{low_index_mults:?}");

if low_index_mults || missing_pos {
if (low_index_mults || missing_pos) && !is_terminated {
let skip_current = if let Some(n) = raw_args.peek(&args_cursor) {
if let Some(arg) = self
.cmd
Expand Down
26 changes: 26 additions & 0 deletions tests/builder/multiple_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,32 @@ fn multiple_value_terminator_option_other_arg() {
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
}

#[test]
fn all_multiple_value_terminator() {
let m = Command::new("lip")
.arg(
Arg::new("files")
.value_terminator(";")
.action(ArgAction::Set)
.num_args(0..),
)
.arg(Arg::new("other").num_args(0..))
.try_get_matches_from(vec!["test", "value", ";"]);

assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
let m = m.unwrap();

assert!(m.contains_id("files"));
assert!(!m.contains_id("other"));
assert_eq!(
m.get_many::<String>("files")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
["value".to_owned()],
);
}

#[test]
fn multiple_vals_with_hyphen() {
let res = Command::new("do")
Expand Down

0 comments on commit 5540d20

Please sign in to comment.