Skip to content

Commit

Permalink
fix(parser): Restore interleaved positional behavior
Browse files Browse the repository at this point in the history
If we felt this was important long-term, we should fix this outside of
the Action.  Since we might be changing up occurrences (clap-rs#3772), we can
probably get away with a hack.
  • Loading branch information
epage committed May 31, 2022
1 parent f2a219e commit cb6f7b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 15 additions & 8 deletions src/parser/parser.rs
Expand Up @@ -1139,15 +1139,22 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
);
match arg.get_action() {
Action::StoreValue => {
if source == ValueSource::CommandLine {
if matches!(ident, Some(Identifier::Short) | Some(Identifier::Long)) {
// Record flag's index
self.cur_idx.set(self.cur_idx.get() + 1);
debug!("Parser::react: cur_idx:={}", self.cur_idx.get());
}
self.start_occurrence_of_arg(matcher, arg);
if ident == Some(Identifier::Index)
&& arg.is_multiple_values_set()
&& matcher.contains(&arg.id)
{
// HACK: Reuse existing occurrence
} else {
self.start_custom_arg(matcher, arg, source);
if source == ValueSource::CommandLine {
if matches!(ident, Some(Identifier::Short) | Some(Identifier::Long)) {
// Record flag's index
self.cur_idx.set(self.cur_idx.get() + 1);
debug!("Parser::react: cur_idx:={}", self.cur_idx.get());
}
self.start_occurrence_of_arg(matcher, arg);
} else {
self.start_custom_arg(matcher, arg, source);
}
}
self.store_arg_values(arg, raw_vals, matcher)?;
if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/grouped_values.rs
Expand Up @@ -190,8 +190,8 @@ fn grouped_interleaved_positional_values() {
.try_get_matches_from(["foo", "1", "2", "-f", "a", "3", "-f", "b", "4"])
.unwrap();
let pos: Vec<_> = m.grouped_values_of("pos").unwrap().collect();
assert_eq!(pos, vec![vec!["1", "2"], vec!["3"], vec!["4"]]);
assert_eq!(m.occurrences_of("pos"), 3);
assert_eq!(pos, vec![vec!["1", "2", "3", "4"]]);
assert_eq!(m.occurrences_of("pos"), 1);
let flag: Vec<_> = m.grouped_values_of("flag").unwrap().collect();
assert_eq!(flag, vec![vec!["a"], vec!["b"]]);
assert_eq!(m.occurrences_of("flag"), 2);
Expand Down

0 comments on commit cb6f7b7

Please sign in to comment.