Skip to content

Commit 42b6d1f

Browse files
committed
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
1 parent 5b4170b commit 42b6d1f

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/app.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
16851685
let mut subcmd_name: Option<String> = None;
16861686
let mut needs_val_of: Option<&str> = None;
16871687
let mut pos_counter = 1;
1688+
let mut val_counter = 0;
16881689
while let Some(arg) = it.next() {
16891690
let arg_slice = arg.as_ref();
16901691
let mut skip = false;
@@ -1746,12 +1747,21 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
17461747
skip = true;
17471748
1
17481749
};
1749-
if let Some(ref mut vals) = o.values {
1750+
if let Some(ref vals) = o.values {
17501751
let len = vals.len() as u8;
17511752
if let Some(num) = opt.max_vals {
17521753
if len != num { continue }
17531754
} else if let Some(num) = opt.num_vals {
1754-
if len != num { continue }
1755+
if opt.multiple {
1756+
val_counter += 1;
1757+
if val_counter != num {
1758+
continue
1759+
} else {
1760+
val_counter = 0;
1761+
}
1762+
} else {
1763+
if len != num { continue }
1764+
}
17551765
} else if !skip {
17561766
continue
17571767
}

src/args/arg.rs

-7
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,6 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
645645
/// `.number_of_values(3)`, and this argument wouldn't be satisfied unless the user provided
646646
/// 3 and only 3 values.
647647
///
648-
/// **NOTE:** `qty` must be > 1
649-
///
650648
/// **NOTE:** Does *not* require `.multiple(true)` to be set. Setting `.multiple(true)` would
651649
/// allow `-f <file> <file> <file> -f <file> <file> <file>` where as *not* setting
652650
/// `.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> {
661659
/// .number_of_values(3)
662660
/// # ).get_matches();
663661
pub fn number_of_values(mut self, qty: u8) -> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
664-
if qty < 2 {
665-
panic!("Arguments with number_of_values(qty) qty must be > 1. Prefer \
666-
takes_value(true) for arguments with only one value, or flags for arguments \
667-
with 0 values.");
668-
}
669662
self.num_vals = Some(qty);
670663
self
671664
}

0 commit comments

Comments
 (0)