Skip to content

Commit

Permalink
fix: fixes using require_equals(true) and min_values(0) together
Browse files Browse the repository at this point in the history
Closes #1044
  • Loading branch information
kbknapp committed Sep 13, 2017
1 parent 0136e99 commit 10ae208
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,13 +1530,17 @@ impl<'a, 'b> Parser<'a, 'b>
debugln!("Parser::parse_opt; opt={}, val={:?}", opt.b.name, val);
debugln!("Parser::parse_opt; opt.settings={:?}", opt.b.settings);
let mut has_eq = false;
let no_val = val.is_none();
let empty_vals = opt.is_set(ArgSettings::EmptyValues);
let min_vals_zero = opt.v.min_vals.unwrap_or(1) == 0;
let needs_eq = opt.is_set(ArgSettings::RequireEquals);

debug!("Parser::parse_opt; Checking for val...");
if let Some(fv) = val {
has_eq = fv.starts_with(&[b'=']) || had_eq;
let v = fv.trim_left_matches(b'=');
if !opt.is_set(ArgSettings::EmptyValues) &&
(v.len_() == 0 || (opt.is_set(ArgSettings::RequireEquals) && !has_eq)) {
if !empty_vals &&
(v.len_() == 0 || (needs_eq && !has_eq)) {
sdebugln!("Found Empty - Error");
return Err(Error::empty_value(opt,
&*usage::create_error_usage(self, matcher, None),
Expand All @@ -1547,7 +1551,7 @@ impl<'a, 'b> Parser<'a, 'b>
fv,
fv.starts_with(&[b'=']));
self.add_val_to_arg(opt, v, matcher)?;
} else if opt.is_set(ArgSettings::RequireEquals) && !opt.is_set(ArgSettings::EmptyValues) {
} else if needs_eq && !(empty_vals || min_vals_zero) {
sdebugln!("None, but requires equals...Error");
return Err(Error::empty_value(opt,
&*usage::create_error_usage(self, matcher, None),
Expand All @@ -1562,10 +1566,12 @@ impl<'a, 'b> Parser<'a, 'b>
self.groups_for_arg(opt.b.name)
.and_then(|vec| Some(matcher.inc_occurrences_of(&*vec)));

if val.is_none() ||
!has_eq &&
(opt.is_set(ArgSettings::Multiple) && !opt.is_set(ArgSettings::RequireDelimiter) &&
matcher.needs_more_vals(opt)) {
let needs_delim = opt.is_set(ArgSettings::RequireDelimiter);
let mult = opt.is_set(ArgSettings::Multiple);
if no_val && min_vals_zero && !has_eq && needs_eq {
debugln!("Parser::parse_opt: More arg vals not required...");
return Ok(ParseResult::ValuesDone);
} else if (mult && !needs_delim) && matcher.needs_more_vals(opt) {
debugln!("Parser::parse_opt: More arg vals required...");
return Ok(ParseResult::Opt(opt.b.name));
}
Expand Down
9 changes: 5 additions & 4 deletions src/app/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ impl<'a, 'b, 'z> Validator<'a, 'b, 'z> {
self.0.color()));
}
}
if let Some(num) = a.min_vals() {
let min_vals_zero = if let Some(num) = a.min_vals() {
debugln!("Validator::validate_arg_num_vals: min_vals set: {}", num);
if (ma.vals.len() as u64) < num {
if (ma.vals.len() as u64) < num && num != 0 {
debugln!("Validator::validate_arg_num_vals: Sending error TooFewValues");
return Err(Error::too_few_values(a,
num,
Expand All @@ -306,9 +306,10 @@ impl<'a, 'b, 'z> Validator<'a, 'b, 'z> {
None),
self.0.color()));
}
}
num == 0
} else { false };
// Issue 665 (https://github.com/kbknapp/clap-rs/issues/665)
if a.takes_value() && !a.is_set(ArgSettings::EmptyValues) && ma.vals.is_empty() {
if a.takes_value() && !(a.is_set(ArgSettings::EmptyValues) || min_vals_zero) && ma.vals.is_empty() {
return Err(Error::empty_value(a,
&*usage::create_error_usage(self.0, matcher, None),
self.0.color()));
Expand Down

0 comments on commit 10ae208

Please sign in to comment.