From f65e2e4bf5969bd4025cccebe8d4c094ee384467 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sat, 3 Mar 2018 21:04:28 -0500 Subject: [PATCH] fix(Indices): fixes Indices to work on v3 --- src/app/parser.rs | 13 +++++++------ src/args/arg_matches.rs | 2 +- tests/indices.rs | 26 +++++++++++++------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/app/parser.rs b/src/app/parser.rs index d38df22b..95ed3fd2 100644 --- a/src/app/parser.rs +++ b/src/app/parser.rs @@ -108,6 +108,7 @@ where num_flags: 0, positionals: VecMap::new(), seen: Vec::new(), + cur_idx: Cell::new(0), } } @@ -489,7 +490,7 @@ where let low_index_mults = self.is_set(AS::LowIndexMultiplePositional) && pos_counter == (self.positionals.len() - 1); let missing_pos = self.is_set(AS::AllowMissingPositional) - && (pos_counter == (self.positionals.len() - 1) + && (pos_counter == (self.positionals.len() - 1) && !self.is_set(AS::TrailingValues)); debugln!( "Parser::get_matches_with: Positional counter...{}", @@ -1193,20 +1194,20 @@ where ) -> ClapResult> { debugln!("Parser::add_single_val_to_arg;"); debugln!("Parser::add_single_val_to_arg: adding val...{:?}", v); - + // update the current index because each value is a distinct index to clap self.cur_idx.set(self.cur_idx.get() + 1); - + // @TODO @docs @p4 docs should probably note that terminator doesn't get an index if let Some(t) = arg.terminator { if t == v { return Ok(ParseResult::ValuesDone); } } - + matcher.add_val_to(arg.name, v); matcher.add_index_to(arg.name, self.cur_idx.get()); - + // Increment or create the group "args" if let Some(grps) = self.groups_for_arg(arg.name) { for grp in grps { @@ -1229,7 +1230,7 @@ where matcher.inc_occurrence_of(flag.name); matcher.add_index_to(flag.name, self.cur_idx.get()); - + // Increment or create the group "args" self.groups_for_arg(flag.name) .and_then(|vec| Some(matcher.inc_occurrences_of(&*vec))); diff --git a/src/args/arg_matches.rs b/src/args/arg_matches.rs index 4cbff410..29912267 100644 --- a/src/args/arg_matches.rs +++ b/src/args/arg_matches.rs @@ -549,7 +549,7 @@ impl<'a> ArgMatches<'a> { /// .multiple(true)) /// .arg(Arg::with_name("flag") /// .short("f") - /// .multiple(true)) + /// .multiple_occurrences(true)) /// .get_matches_from(vec!["myapp", "-o", "val1", "-f", "-o", "val2", "-f"]); /// // ARGV idices: ^0 ^1 ^2 ^3 ^4 ^5 ^6 /// // clap idices: ^2 ^3 ^5 ^6 diff --git a/tests/indices.rs b/tests/indices.rs index ebc7a529..60c29182 100644 --- a/tests/indices.rs +++ b/tests/indices.rs @@ -57,10 +57,10 @@ fn index_flags() { let m = App::new("ind") .arg(Arg::with_name("exclude") .short("e") - .multiple(true)) + .multiple_occurrences(true)) .arg(Arg::with_name("include") .short("i") - .multiple(true)) + .multiple_occurrences(true)) .get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"]); assert_eq!(m.index_of("exclude"), Some(1)); @@ -72,10 +72,10 @@ fn indices_mult_flags() { let m = App::new("ind") .arg(Arg::with_name("exclude") .short("e") - .multiple(true)) + .multiple_occurrences(true)) .arg(Arg::with_name("include") .short("i") - .multiple(true)) + .multiple_occurrences(true)) .get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"]); assert_eq!(m.indices_of("exclude").unwrap().collect::>(), &[1, 3, 4]); @@ -87,10 +87,10 @@ fn indices_mult_flags_combined() { let m = App::new("ind") .arg(Arg::with_name("exclude") .short("e") - .multiple(true)) + .multiple_occurrences(true)) .arg(Arg::with_name("include") .short("i") - .multiple(true)) + .multiple_occurrences(true)) .get_matches_from(vec!["ind", "-eieei"]); assert_eq!(m.indices_of("exclude").unwrap().collect::>(), &[1, 3, 4]); @@ -102,10 +102,10 @@ fn indices_mult_flags_opt_combined() { let m = App::new("ind") .arg(Arg::with_name("exclude") .short("e") - .multiple(true)) + .multiple_occurrences(true)) .arg(Arg::with_name("include") .short("i") - .multiple(true)) + .multiple_occurrences(true)) .arg(Arg::with_name("option") .short("o") .takes_value(true)) @@ -121,10 +121,10 @@ fn indices_mult_flags_opt_combined_eq() { let m = App::new("ind") .arg(Arg::with_name("exclude") .short("e") - .multiple(true)) + .multiple_occurrences(true)) .arg(Arg::with_name("include") .short("i") - .multiple(true)) + .multiple_occurrences(true)) .arg(Arg::with_name("option") .short("o") .takes_value(true)) @@ -164,12 +164,12 @@ fn indices_mult_opt_mult_flag() { .arg(Arg::with_name("option") .short("o") .takes_value(true) - .multiple(true)) + .multiple_occurrences(true)) .arg(Arg::with_name("flag") .short("f") - .multiple(true)) + .multiple_occurrences(true)) .get_matches_from(vec!["myapp", "-o", "val1", "-f", "-o", "val2", "-f"]); assert_eq!(m.indices_of("option").unwrap().collect::>(), &[2, 5]); assert_eq!(m.indices_of("flag").unwrap().collect::>(), &[3, 6]); -} \ No newline at end of file +}