mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 14:22:34 +00:00
Merge pull request #4269 from epage/usage
fix(error): Polish `--flag=bad-value` error
This commit is contained in:
commit
9cd1939535
2 changed files with 21 additions and 2 deletions
|
@ -183,7 +183,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
self.cmd,
|
self.cmd,
|
||||||
rest,
|
rest,
|
||||||
arg,
|
arg,
|
||||||
Usage::new(self.cmd).create_usage_no_title(&used),
|
Usage::new(self.cmd).create_usage_with_title(&used),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
ParseResult::MaybeHyphenValue => {
|
ParseResult::MaybeHyphenValue => {
|
||||||
|
@ -778,7 +778,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
"Parser::parse_long_arg({:?}): Got invalid literal `{:?}`",
|
"Parser::parse_long_arg({:?}): Got invalid literal `{:?}`",
|
||||||
long_arg, rest
|
long_arg, rest
|
||||||
);
|
);
|
||||||
let used: Vec<Id> = matcher
|
let mut used: Vec<Id> = matcher
|
||||||
.arg_ids()
|
.arg_ids()
|
||||||
.filter(|arg_id| {
|
.filter(|arg_id| {
|
||||||
matcher.check_explicit(arg_id, &crate::builder::ArgPredicate::IsPresent)
|
matcher.check_explicit(arg_id, &crate::builder::ArgPredicate::IsPresent)
|
||||||
|
@ -790,6 +790,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
})
|
})
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect();
|
.collect();
|
||||||
|
used.push(arg.get_id().clone());
|
||||||
|
|
||||||
Ok(ParseResult::UnneededAttachedValue {
|
Ok(ParseResult::UnneededAttachedValue {
|
||||||
rest: rest.to_str_lossy().into_owned(),
|
rest: rest.to_str_lossy().into_owned(),
|
||||||
|
|
|
@ -136,6 +136,24 @@ fn multiple_flags_in_single() {
|
||||||
assert!(*m.get_one::<bool>("debug").expect("defaulted by clap"));
|
assert!(*m.get_one::<bool>("debug").expect("defaulted by clap"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(feature = "error-context")]
|
||||||
|
fn unexpected_value_error() {
|
||||||
|
const USE_FLAG_AS_ARGUMENT: &str = "\
|
||||||
|
error: The value 'foo' was provided to '--a-flag' but it wasn't expecting any more values
|
||||||
|
|
||||||
|
Usage: mycat --a-flag [filename]
|
||||||
|
|
||||||
|
For more information try '--help'
|
||||||
|
";
|
||||||
|
|
||||||
|
let cmd = Command::new("mycat")
|
||||||
|
.arg(Arg::new("filename"))
|
||||||
|
.arg(Arg::new("a-flag").long("a-flag").action(ArgAction::SetTrue));
|
||||||
|
|
||||||
|
utils::assert_output(cmd, "mycat --a-flag=foo", USE_FLAG_AS_ARGUMENT, true);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "error-context")]
|
#[cfg(feature = "error-context")]
|
||||||
fn issue_1284_argument_in_flag_style() {
|
fn issue_1284_argument_in_flag_style() {
|
||||||
|
|
Loading…
Reference in a new issue