Treat partially-matched arguments as an error

This commit is contained in:
Dustin J. Mitchell 2021-06-05 09:15:24 -04:00
parent 21c4f95fd9
commit 2b3383842e

View file

@ -14,8 +14,12 @@ where
if let Some(arg) = input.get(0) {
return match f(arg) {
Ok(("", rv)) => Ok((&input[1..], rv)),
// single-arg parsers must consume the entire arg
Ok((unconsumed, _)) => panic!("unconsumed argument input {}", unconsumed),
// single-arg parsers must consume the entire arg, so consider unconsumed
// output to be an error.
Ok((_, _)) => Err(Err::Error(Error {
input,
code: ErrorKind::Eof,
})),
// single-arg parsers are all complete parsers
Err(Err::Incomplete(_)) => unreachable!(),
// for error and failure, rewrite to an error at this position in the arugment list
@ -48,4 +52,9 @@ mod test {
);
assert!(arg_matching(plus_tag)(argv!["foo", "bar"]).is_err());
}
#[test]
fn test_partial_arg_matching() {
assert!(arg_matching(wait_colon)(argv!["wait:UNRECOGNIZED"]).is_err());
}
}