From 7a2bbca62bf245970866d3de76293de435dc8dbe Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 6 Sep 2022 17:11:58 -0500 Subject: [PATCH] fix(derive): Make Command::allow_hyphen_values forward to Arg --- src/builder/command.rs | 8 ++++++++ src/parser/parser.rs | 13 +------------ tests/builder/app_settings.rs | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/builder/command.rs b/src/builder/command.rs index 35c4c3c6076..d1f0e1f59f8 100644 --- a/src/builder/command.rs +++ b/src/builder/command.rs @@ -3818,6 +3818,14 @@ impl Command { } } } + #[allow(deprecated)] + if self.is_allow_hyphen_values_set() { + for arg in self.args.args_mut() { + if arg.is_takes_value_set() { + arg.settings.insert(ArgSettings::AllowHyphenValues.into()); + } + } + } #[cfg(debug_assertions)] assert_app(self); diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 5203b83d25a..98cd0a2fa33 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -634,8 +634,7 @@ impl<'cmd> Parser<'cmd> { current_positional.get_id() ); - if self.cmd.is_allow_hyphen_values_set() - || self.cmd[¤t_positional.id].is_allow_hyphen_values_set() + if self.cmd[¤t_positional.id].is_allow_hyphen_values_set() || (self.cmd.is_allow_negative_numbers_set() && next.is_number()) { // If allow hyphen, this isn't a new arg. @@ -807,9 +806,6 @@ impl<'cmd> Parser<'cmd> { } } else if let Some(sc_name) = self.possible_long_flag_subcommand(long_arg) { Ok(ParseResult::FlagSubCommand(sc_name.to_string())) - } else if self.cmd.is_allow_hyphen_values_set() { - debug!("Parser::parse_long_arg: contains non-long flag"); - Ok(ParseResult::MaybeHyphenValue) } else if self .cmd .get_keymap() @@ -850,13 +846,6 @@ impl<'cmd> Parser<'cmd> { } else if self.cmd.is_allow_negative_numbers_set() && short_arg.is_number() { debug!("Parser::parse_short_arg: negative number"); return Ok(ParseResult::MaybeHyphenValue); - } else if self.cmd.is_allow_hyphen_values_set() - && short_arg - .clone() - .any(|c| !c.map(|c| self.cmd.contains_short(c)).unwrap_or_default()) - { - debug!("Parser::parse_short_args: contains non-short flag"); - return Ok(ParseResult::MaybeHyphenValue); } else if self .cmd .get_keymap() diff --git a/tests/builder/app_settings.rs b/tests/builder/app_settings.rs index 206b0d51b99..aaae4306254 100644 --- a/tests/builder/app_settings.rs +++ b/tests/builder/app_settings.rs @@ -862,7 +862,7 @@ fn issue_1066_allow_leading_hyphen_and_unknown_args_option() { let res = Command::new("prog") .allow_hyphen_values(true) .arg(arg!(--"some-argument" )) - .try_get_matches_from(vec!["prog", "-hello"]); + .try_get_matches_from(vec!["prog", "-fish"]); assert!(res.is_err()); assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument);