Repository: clap-rs/clap
Pull request #4269 (2022-09-27 14:26:43)

fix(error): Polish `--flag=bad-value` error
This error was untested and didn't fit the style of our other errors
```
error: The value 'value' was provided to '--flag' but it wasn't expecting any more values

example --other <path>...

For more information try '--help'
```
- It should show a usage type
- It should show the flag in the usage so people can scan more quickly for what is wrong

This supersedes #4268


Patch:
diff --git a/src/builder/command.rs b/src/builder/command.rs
index eace0415..4be5b55a 100644
--- a/src/builder/command.rs
+++ b/src/builder/command.rs
@@ -1662,7 +1662,7 @@ impl Command {
     /// correctly by the default help formatter:
     ///
     /// - Do not indent the first usage line.
-    /// - Indent all subsequent usage lines with seven spaces.
+    /// - Indent all subsequent usage lines with four spaces.
     /// - The last line must not end with a newline.
     ///
     /// # Examples
@@ -1680,8 +1680,8 @@ impl Command {
     /// # use clap::{Command, Arg};
     /// Command::new("myprog")
     ///     .override_usage(
-    ///         "myapp -X [-a] [-b] <file>\n       \
-    ///          myapp -Y [-c] <file1> <file2>\n       \
+    ///         "myapp -X [-a] [-b] <file>\n    \
+    ///          myapp -Y [-c] <file1> <file2>\n    \
     ///          myapp -Z [-d|-e]"
     ///     )
     /// # ;
diff --git a/src/parser/parser.rs b/src/parser/parser.rs
index cd96d597..fb43923f 100644
--- a/src/parser/parser.rs
+++ b/src/parser/parser.rs
@@ -183,7 +183,7 @@ impl<'cmd> Parser<'cmd> {
                                 self.cmd,
                                 rest,
                                 arg,
-                                Usage::new(self.cmd).create_usage_no_title(&used),
+                                Usage::new(self.cmd).create_usage_with_title(&used),
                             ));
                         }
                         ParseResult::MaybeHyphenValue => {
@@ -778,7 +778,7 @@ impl<'cmd> Parser<'cmd> {
                     "Parser::parse_long_arg({:?}): Got invalid literal `{:?}`",
                     long_arg, rest
                 );
-                let used: Vec<Id> = matcher
+                let mut used: Vec<Id> = matcher
                     .arg_ids()
                     .filter(|arg_id| {
                         matcher.check_explicit(arg_id, &crate::builder::ArgPredicate::IsPresent)
@@ -790,6 +790,7 @@ impl<'cmd> Parser<'cmd> {
                     })
                     .cloned()
                     .collect();
+                used.push(arg.get_id().clone());
 
                 Ok(ParseResult::UnneededAttachedValue {
                     rest: rest.to_str_lossy().into_owned(),


Test patch:
diff --git a/tests/builder/flags.rs b/tests/builder/flags.rs
index e86adcdf..50ba789a 100644
--- a/tests/builder/flags.rs
+++ b/tests/builder/flags.rs
@@ -136,6 +136,24 @@ fn multiple_flags_in_single() {
     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]
 #[cfg(feature = "error-context")]
 fn issue_1284_argument_in_flag_style() {