Skip to content

Commit

Permalink
fix(parser): Ensure terminated positionals are assigned correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed May 19, 2023
1 parent 1ee2e95 commit e1db168
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
39 changes: 20 additions & 19 deletions clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,26 +372,27 @@ impl<'cmd> Parser<'cmd> {
if matcher.pending_arg_id() != Some(arg.get_id()) || !arg.is_multiple_values_set() {
ok!(self.resolve_pending(matcher));
}
if let Some(_parse_result) = self.check_terminator(arg, arg_os.to_value_os()) {
debug!(
"Parser::get_matches_with: ignoring terminator result {_parse_result:?}"
);
} else {
let arg_values = matcher.pending_values_mut(
arg.get_id(),
Some(Identifier::Index),
trailing_values,
);
arg_values.push(arg_os.to_value_os().to_owned());
}
parse_state =
if let Some(parse_result) = self.check_terminator(arg, arg_os.to_value_os()) {
debug_assert_eq!(parse_result, ParseResult::ValuesDone);
pos_counter += 1;
ParseState::ValuesDone
} else {
let arg_values = matcher.pending_values_mut(
arg.get_id(),
Some(Identifier::Index),
trailing_values,
);
arg_values.push(arg_os.to_value_os().to_owned());

// Only increment the positional counter if it doesn't allow multiples
if !arg.is_multiple() {
pos_counter += 1;
parse_state = ParseState::ValuesDone;
} else {
parse_state = ParseState::Pos(arg.get_id().clone());
}
// Only increment the positional counter if it doesn't allow multiples
if !arg.is_multiple() {
pos_counter += 1;
ParseState::ValuesDone
} else {
ParseState::Pos(arg.get_id().clone())
}
};
valid_arg_found = true;
} else if let Some(external_parser) =
self.cmd.get_external_subcommand_value_parser().cloned()
Expand Down
7 changes: 2 additions & 5 deletions tests/builder/multiple_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,16 +1447,13 @@ fn multiple_positional_multiple_values() {
.unwrap()
.map(|v| v.as_str())
.collect();
assert_eq!(
&cmd1,
&["find", "-type", "f", "-name", "special", "/home/clap"]
);
assert_eq!(&cmd1, &["find", "-type", "f", "-name", "special"]);
let cmd2: Vec<_> = m
.get_many::<String>("cmd2")
.unwrap()
.map(|v| v.as_str())
.collect();
assert_eq!(&cmd2, &["foo"]);
assert_eq!(&cmd2, &["/home/clap", "foo"]);
}

#[test]
Expand Down

0 comments on commit e1db168

Please sign in to comment.