Revert "fix(parser): Don't treat missing values as missing args"

This reverts commit 50f4018dcf.

This broke compatibility with the derive when dealing with
`Option<Option<T>>` and related cases.
This commit is contained in:
Ed Page 2022-06-02 16:32:47 -05:00
parent 552e6feb3f
commit 002d4421e5
5 changed files with 7 additions and 33 deletions

View file

@ -18,11 +18,6 @@ pub enum MatchesError {
UnknownArgument {
// Missing `id` but blocked on a public id type which will hopefully come with `unstable-v4`
},
/// Present argument must have one value
#[non_exhaustive]
ExpectedOne {
// Missing `id` but blocked on a public id type which will hopefully come with `unstable-v4`
},
}
impl MatchesError {
@ -56,9 +51,6 @@ impl std::fmt::Display for MatchesError {
Self::UnknownArgument {} => {
writeln!(f, "Unknown argument or group id. Make sure you are using the argument id and not the short or long flags")
}
Self::ExpectedOne {} => {
writeln!(f, "Present argument must have one value. Make sure you are using the correct lookup (`get_one` vs `get_many`)")
}
}
}
}

View file

@ -1063,11 +1063,8 @@ impl ArgMatches {
) -> Result<Option<&T>, MatchesError> {
let id = Id::from(name);
let arg = self.try_get_arg_t::<T>(&id)?;
let value = match arg.map(|a| a.first()) {
Some(Some(value)) => value,
Some(None) => {
return Err(MatchesError::ExpectedOne {});
}
let value = match arg.and_then(|a| a.first()) {
Some(value) => value,
None => {
return Ok(None);
}

View file

@ -38,10 +38,7 @@ fn env_bool_literal() {
let m = r.unwrap();
assert!(m.is_present("present"));
assert_eq!(m.occurrences_of("present"), 0);
assert!(matches!(
m.try_get_one::<String>("present").unwrap_err(),
clap::parser::MatchesError::ExpectedOne { .. }
));
assert_eq!(m.get_one::<String>("present").map(|v| v.as_str()), None);
assert!(!m.is_present("negated"));
assert!(!m.is_present("absent"));
}

View file

@ -111,10 +111,7 @@ fn group_single_flag() {
let m = res.unwrap();
assert!(m.is_present("grp"));
assert!(matches!(
m.try_get_one::<String>("grp").unwrap_err(),
clap::parser::MatchesError::ExpectedOne { .. }
));
assert!(m.get_one::<String>("grp").map(|v| v.as_str()).is_none());
}
#[test]

View file

@ -49,10 +49,7 @@ fn multiple_args_and_final_arg_without_value() {
Some("file")
);
assert!(m.is_present("f"));
assert!(matches!(
m.try_get_one::<String>("stuff").unwrap_err(),
clap::parser::MatchesError::ExpectedOne { .. }
));
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
}
#[test]
@ -79,10 +76,7 @@ fn multiple_args_and_intermittent_arg_without_value() {
Some("file")
);
assert!(m.is_present("f"));
assert!(matches!(
m.try_get_one::<String>("stuff").unwrap_err(),
clap::parser::MatchesError::ExpectedOne { .. }
));
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
}
#[test]
@ -124,10 +118,7 @@ fn subcommand() {
sub_m.is_present("test"),
"expected subcommand to be present due to partial parsing"
);
assert!(matches!(
sub_m.try_get_one::<String>("test").unwrap_err(),
clap::parser::MatchesError::ExpectedOne { .. }
));
assert_eq!(sub_m.get_one::<String>("test").map(|v| v.as_str()), None);
assert_eq!(
sub_m.get_one::<String>("stuff").map(|v| v.as_str()),
Some("some other val")