From 42b6d1fc3c519c92dfb3af15276e7d3b635e6cfe Mon Sep 17 00:00:00 2001 From: Kevin K Date: Thu, 16 Jul 2015 21:33:34 -0400 Subject: [PATCH] fix: fixes a logic bug and allows setting Arg::number_of_values() < 2 Allows setting `Arg::number_of_values(qty)` where `qty` < 2. This allows things such as `Arg::number_of_values(1)` in conjuction with `Arg::multiple(true)` which would make invoking the program like this `myprog --opt val --opt val` legal, but `myprog --opt val1 val2` illegal. Closes #161 --- src/app.rs | 14 ++++++++++++-- src/args/arg.rs | 7 ------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/app.rs b/src/app.rs index e2fe0676..ec88ee1a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1685,6 +1685,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ let mut subcmd_name: Option = None; let mut needs_val_of: Option<&str> = None; let mut pos_counter = 1; + let mut val_counter = 0; while let Some(arg) = it.next() { let arg_slice = arg.as_ref(); let mut skip = false; @@ -1746,12 +1747,21 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ skip = true; 1 }; - if let Some(ref mut vals) = o.values { + if let Some(ref vals) = o.values { let len = vals.len() as u8; if let Some(num) = opt.max_vals { if len != num { continue } } else if let Some(num) = opt.num_vals { - if len != num { continue } + if opt.multiple { + val_counter += 1; + if val_counter != num { + continue + } else { + val_counter = 0; + } + } else { + if len != num { continue } + } } else if !skip { continue } diff --git a/src/args/arg.rs b/src/args/arg.rs index cb29585c..9bfdc86f 100644 --- a/src/args/arg.rs +++ b/src/args/arg.rs @@ -645,8 +645,6 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// `.number_of_values(3)`, and this argument wouldn't be satisfied unless the user provided /// 3 and only 3 values. /// - /// **NOTE:** `qty` must be > 1 - /// /// **NOTE:** Does *not* require `.multiple(true)` to be set. Setting `.multiple(true)` would /// allow `-f -f ` where as *not* setting /// `.multiple(true)` would only allow one occurrence of this argument. @@ -661,11 +659,6 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> { /// .number_of_values(3) /// # ).get_matches(); pub fn number_of_values(mut self, qty: u8) -> Arg<'n, 'l, 'h, 'g, 'p, 'r> { - if qty < 2 { - panic!("Arguments with number_of_values(qty) qty must be > 1. Prefer \ - takes_value(true) for arguments with only one value, or flags for arguments \ - with 0 values."); - } self.num_vals = Some(qty); self }