mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
Merge pull request #162 from kbknapp/issue-161
fix: fixes a logic bug and allows setting Arg::number_of_values() < 2
This commit is contained in:
commit
65ba33d895
2 changed files with 12 additions and 9 deletions
14
src/app.rs
14
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<String> = 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
|
||||
}
|
||||
|
|
|
@ -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 <file> <file> <file> -f <file> <file> <file>` 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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue