mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 06:12:40 +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,32 +2228,49 @@ impl TypedValueParser for UnknownArgumentValueParser {
|
||||||
&self,
|
&self,
|
||||||
cmd: &crate::Command,
|
cmd: &crate::Command,
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
_value: &std::ffi::OsStr,
|
value: &std::ffi::OsStr,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let arg = match arg {
|
TypedValueParser::parse_ref_(self, cmd, arg, value, ValueSource::CommandLine)
|
||||||
Some(arg) => arg.to_string(),
|
}
|
||||||
None => "..".to_owned(),
|
|
||||||
};
|
fn parse_ref_(
|
||||||
let err = crate::Error::unknown_argument(
|
&self,
|
||||||
cmd,
|
cmd: &crate::Command,
|
||||||
arg,
|
arg: Option<&crate::Arg>,
|
||||||
self.arg.as_ref().map(|s| (s.as_str().to_owned(), None)),
|
_value: &std::ffi::OsStr,
|
||||||
false,
|
source: ValueSource,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
) -> Result<Self::Value, crate::Error> {
|
||||||
);
|
match source {
|
||||||
#[cfg(feature = "error-context")]
|
ValueSource::DefaultValue => {
|
||||||
let err = {
|
TypedValueParser::parse_ref_(&StringValueParser::new(), cmd, arg, _value, source)
|
||||||
debug_assert_eq!(
|
}
|
||||||
err.get(crate::error::ContextKind::Suggested),
|
ValueSource::EnvVariable | ValueSource::CommandLine => {
|
||||||
None,
|
let arg = match arg {
|
||||||
"Assuming `Error::unknown_argument` doesn't apply any `Suggested` so we can without caution"
|
Some(arg) => arg.to_string(),
|
||||||
);
|
None => "..".to_owned(),
|
||||||
err.insert_context_unchecked(
|
};
|
||||||
crate::error::ContextKind::Suggested,
|
let err = crate::Error::unknown_argument(
|
||||||
crate::error::ContextValue::StyledStrs(self.suggestions.clone()),
|
cmd,
|
||||||
)
|
arg,
|
||||||
};
|
self.arg.as_ref().map(|s| (s.as_str().to_owned(), None)),
|
||||||
Err(err)
|
false,
|
||||||
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
|
);
|
||||||
|
#[cfg(feature = "error-context")]
|
||||||
|
let err = {
|
||||||
|
debug_assert_eq!(
|
||||||
|
err.get(crate::error::ContextKind::Suggested),
|
||||||
|
None,
|
||||||
|
"Assuming `Error::unknown_argument` doesn't apply any `Suggested` so we can without caution"
|
||||||
|
);
|
||||||
|
err.insert_context_unchecked(
|
||||||
|
crate::error::ContextKind::Suggested,
|
||||||
|
crate::error::ContextValue::StyledStrs(self.suggestions.clone()),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -263,7 +263,7 @@ fn unknown_argument_flag() {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let res = cmd.clone().try_get_matches_from(["test"]);
|
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"]);
|
let res = cmd.try_get_matches_from(["test", "--ignored"]);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
|
Loading…
Reference in a new issue