Skip to content

Commit

Permalink
fix: fixes a bug that allowed options to pass parsing when no value w…
Browse files Browse the repository at this point in the history
…as provided

Since options have `empty_values(true)` by default, so long as another valid flag
or option came after the option in question, clap would parse it as an empty value
incorrectly. This commit forces the user to explicitly add an empty value.

`--option "" --flag` is allowed
`--option --flag` is no longer allowed unless the user has *also* set `min_values(0)`

This commit also fixes an issue where `-o=` would be parsed as a value of `Some("=")`
instead of `Some("")`.

Closes #1105
  • Loading branch information
kbknapp committed Nov 13, 2017
1 parent 46c1317 commit 2fb7582
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/app/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'a, 'b, 'z> Validator<'a, 'b, 'z> {
let mut c_with = find_from!($p, &$name, blacklist, &$matcher);
c_with = c_with.or(
$p.find_any_arg(&$name).map_or(None, |aa| aa.blacklist())
.map_or(None,
.map_or(None,
|bl| bl.iter().find(|arg| $matcher.contains(arg)))
.map_or(None, |an| $p.find_any_arg(an))
.map_or(None, |aa| Some(format!("{}", aa)))
Expand Down Expand Up @@ -312,7 +312,8 @@ impl<'a, 'b, 'z> Validator<'a, 'b, 'z> {
num == 0
} else { false };
// Issue 665 (https://github.com/kbknapp/clap-rs/issues/665)
if a.takes_value() && !(a.is_set(ArgSettings::EmptyValues) || min_vals_zero) && ma.vals.is_empty() {
// Issue 1105 (https://github.com/kbknapp/clap-rs/issues/1105)
if a.takes_value() && !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 Expand Up @@ -410,7 +411,7 @@ impl<'a, 'b, 'z> Validator<'a, 'b, 'z> {
}
})
})
}};
}};
}
if a.is_set(ArgSettings::RequiredUnlessAll) {
check!(all, self.0, a, matcher)
Expand Down
6 changes: 6 additions & 0 deletions src/osstringext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ impl OsStrExt2 for OsStr {
}

fn trim_left_matches(&self, byte: u8) -> &OsStr {
let mut found = false;
for (i, b) in self.as_bytes().iter().enumerate() {
if b != &byte {
return OsStr::from_bytes(&self.as_bytes()[i..]);
} else {
found = true;
}
}
if found {
return OsStr::from_bytes(&self.as_bytes()[self.len_()..]);
}
&*self
}

Expand Down

0 comments on commit 2fb7582

Please sign in to comment.