mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
fix(builder): UnknownValueParser shouldn't error on flag absense
Fixes #5079
This commit is contained in:
parent
6720240577
commit
56135f3ff3
2 changed files with 43 additions and 26 deletions
|
@ -2228,8 +2228,23 @@ impl TypedValueParser for UnknownArgumentValueParser {
|
|||
&self,
|
||||
cmd: &crate::Command,
|
||||
arg: Option<&crate::Arg>,
|
||||
_value: &std::ffi::OsStr,
|
||||
value: &std::ffi::OsStr,
|
||||
) -> Result<Self::Value, crate::Error> {
|
||||
TypedValueParser::parse_ref_(self, cmd, arg, value, ValueSource::CommandLine)
|
||||
}
|
||||
|
||||
fn parse_ref_(
|
||||
&self,
|
||||
cmd: &crate::Command,
|
||||
arg: Option<&crate::Arg>,
|
||||
_value: &std::ffi::OsStr,
|
||||
source: ValueSource,
|
||||
) -> Result<Self::Value, crate::Error> {
|
||||
match source {
|
||||
ValueSource::DefaultValue => {
|
||||
TypedValueParser::parse_ref_(&StringValueParser::new(), cmd, arg, _value, source)
|
||||
}
|
||||
ValueSource::EnvVariable | ValueSource::CommandLine => {
|
||||
let arg = match arg {
|
||||
Some(arg) => arg.to_string(),
|
||||
None => "..".to_owned(),
|
||||
|
@ -2255,6 +2270,8 @@ impl TypedValueParser for UnknownArgumentValueParser {
|
|||
};
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a type with [value_parser!][crate::value_parser!]
|
||||
|
|
|
@ -263,7 +263,7 @@ fn unknown_argument_flag() {
|
|||
]);
|
||||
|
||||
let res = cmd.clone().try_get_matches_from(["test"]);
|
||||
assert!(res.is_err());
|
||||
assert!(res.is_ok());
|
||||
|
||||
let res = cmd.try_get_matches_from(["test", "--ignored"]);
|
||||
assert!(res.is_err());
|
||||
|
|
Loading…
Reference in a new issue