diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 04dc6c96137..6d5fa9b4655 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -1075,11 +1075,38 @@ impl<'help, 'app> Parser<'help, 'app> { self.app.settings.set(AS::ValidArgFound); self.seen.push(opt.id.clone()); if opt.is_set(ArgSettings::TakesValue) { + debug!( + "Parser::parse_long_arg: Found an opt with value '{:?}'", + &val + ); return self.parse_opt(&val, opt, matcher); - } else { - self.check_for_help_and_version_str(&arg)?; - return Ok(self.parse_flag(opt, matcher)); } + debug!("Parser::parse_long_arg: Found a flag"); + if let Some(rest) = val { + debug!( + "Parser::parse_long_arg: Got invalid literal `{:?}`", + rest.to_os_string() + ); + let used: Vec = matcher + .arg_names() + .filter(|&n| { + self.app.find(n).map_or(true, |a| { + !(a.is_set(ArgSettings::Hidden) || self.required.contains(&a.id)) + }) + }) + .cloned() + .collect(); + + return Err(ClapError::too_many_values( + rest.to_string_lossy().into(), + opt, + Usage::new(self).create_usage_no_title(&used), + self.app.color(), + )); + } + self.check_for_help_and_version_str(&arg)?; + debug!("Parser::parse_long_arg: Presence validated"); + return Ok(self.parse_flag(opt, matcher)); } if let Some(sc_name) = self.possible_long_flag_subcommand(&arg) { diff --git a/tests/flags.rs b/tests/flags.rs index 8502676ba01..a3e561de9f9 100644 --- a/tests/flags.rs +++ b/tests/flags.rs @@ -87,6 +87,17 @@ fn flag_using_long() { assert!(m.is_present("color")); } +#[test] +fn flag_using_long_with_literals() { + use clap::ErrorKind; + + let m = App::new("flag") + .arg(Arg::new("rainbow").long("rainbow")) + .try_get_matches_from(vec!["", "--rainbow=false"]); + assert!(m.is_err(), "{:#?}", m.unwrap()); + assert_eq!(m.unwrap_err().kind, ErrorKind::TooManyValues); +} + #[test] fn flag_using_mixed() { let m = App::new("flag")