Correctly handle -f==a (value is =a)

This commit is contained in:
CreepySkeleton 2020-04-20 13:28:50 +03:00
parent aae96236b2
commit 32ffdfe855
2 changed files with 21 additions and 5 deletions

View file

@ -1215,7 +1215,7 @@ where
p[1].as_bytes() p[1].as_bytes()
); );
let i = p[0].as_bytes().len() + 1; let i = p[0].as_bytes().len() + 1;
let val = if !p[1].as_bytes().is_empty() { let val = if !p[1].is_empty() {
debugln!( debugln!(
"Parser::parse_short_arg:iter:{}: val={:?} (bytes), val={:?} (ascii)", "Parser::parse_short_arg:iter:{}: val={:?} (bytes), val={:?} (ascii)",
c, c,
@ -1228,11 +1228,10 @@ where
}; };
// Default to "we're expecting a value later" // Default to "we're expecting a value later"
let ret = self.parse_opt(val, opt, false, matcher)?; return self.parse_opt(val, opt, false, matcher);
return Ok(ret);
} else { } else {
let arg = format!("-{}", c); let arg = format!("-{}", c);
return Err(ClapError::unknown_argument( return Err(ClapError::unknown_argument(
&*arg, &*arg,
None, None,
@ -1262,7 +1261,7 @@ where
debug!("Parser::parse_opt; Checking for val..."); debug!("Parser::parse_opt; Checking for val...");
if let Some(fv) = val { if let Some(fv) = val {
has_eq = fv.starts_with(&[b'=']) || had_eq; has_eq = fv.starts_with(&[b'=']) || had_eq;
let v = fv.trim_start_matches(b'='); let v = fv.trim_start_n_matches(1, b'=');
if !empty_vals && (v.is_empty() || (needs_eq && !has_eq)) { if !empty_vals && (v.is_empty() || (needs_eq && !has_eq)) {
sdebugln!("Found Empty - Error"); sdebugln!("Found Empty - Error");
return Err(ClapError::empty_value( return Err(ClapError::empty_value(

View file

@ -15,6 +15,7 @@ pub(crate) trait OsStrExt2 {
fn split_at_byte(&self, b: u8) -> (&OsStr, &OsStr); fn split_at_byte(&self, b: u8) -> (&OsStr, &OsStr);
fn split_at(&self, i: usize) -> (&OsStr, &OsStr); fn split_at(&self, i: usize) -> (&OsStr, &OsStr);
fn trim_start_matches(&self, b: u8) -> &OsStr; fn trim_start_matches(&self, b: u8) -> &OsStr;
fn trim_start_n_matches(&self, n: usize, ch: u8) -> &OsStr;
fn contains_byte(&self, b: u8) -> bool; fn contains_byte(&self, b: u8) -> bool;
fn split(&self, b: u8) -> OsSplit; fn split(&self, b: u8) -> OsSplit;
} }
@ -75,6 +76,22 @@ impl OsStrExt2 for OsStr {
&*self &*self
} }
// Like `trim_start_matches`, but trims no more than `n` matches
#[inline]
fn trim_start_n_matches(&self, n: usize, ch: u8) -> &OsStr {
let i = self
.as_bytes()
.into_iter()
.take(n)
.take_while(|c| **c == ch)
.enumerate()
.last()
.map(|(i, _)| i + 1)
.unwrap_or(0);
self.split_at(i).1
}
fn split_at(&self, i: usize) -> (&OsStr, &OsStr) { fn split_at(&self, i: usize) -> (&OsStr, &OsStr) {
( (
OsStr::from_bytes(&self.as_bytes()[..i]), OsStr::from_bytes(&self.as_bytes()[..i]),