refactor: Harden against occur/val ordering

This makes some naming more explicit and allows us to increment
occurrences before adding values (which is the logical mental order and
allows other improvements).
This commit is contained in:
Ed Page 2021-12-27 14:50:47 -06:00
parent 2d1dfae318
commit d7f27e8732
3 changed files with 12 additions and 5 deletions

View file

@ -176,8 +176,11 @@ impl ArgMatcher {
ma.push_index(idx);
}
pub(crate) fn arg_have_val(&mut self, arg: &Id) -> bool {
matches!(self.entry(arg), Entry::Occupied(_))
pub(crate) fn has_val_groups(&mut self, arg: &Id) -> bool {
match self.entry(arg) {
Entry::Occupied(e) => e.get().has_val_groups(),
Entry::Vacant(_) => false,
}
}
pub(crate) fn needs_more_vals(&self, o: &Arg) -> bool {

View file

@ -81,6 +81,10 @@ impl MatchedArg {
self.vals.iter().flatten().count() == 0
}
pub(crate) fn has_val_groups(&self) -> bool {
!self.vals.is_empty()
}
// Will be used later
#[allow(dead_code)]
pub(crate) fn remove_vals(&mut self, len: usize) {

View file

@ -594,7 +594,7 @@ impl<'help, 'app> Parser<'help, 'app> {
// Creating new value group rather than appending when the arg
// doesn't have any value. This behaviour is right because
// positional arguments are always present continuously.
let append = self.arg_have_val(matcher, p);
let append = self.has_val_groups(matcher, p);
self.add_val_to_arg(
p,
&arg_os,
@ -1463,8 +1463,8 @@ impl<'help, 'app> Parser<'help, 'app> {
matcher.add_index_to(&arg.id, self.cur_idx.get(), ty);
}
fn arg_have_val(&self, matcher: &mut ArgMatcher, arg: &Arg<'help>) -> bool {
matcher.arg_have_val(&arg.id)
fn has_val_groups(&self, matcher: &mut ArgMatcher, arg: &Arg<'help>) -> bool {
matcher.has_val_groups(&arg.id)
}
fn parse_flag(&self, flag: &Arg<'help>, matcher: &mut ArgMatcher) -> ParseResult {