Skip to content

Commit

Permalink
fix(derive): Make Command::allow_hyphen_values forward to Arg
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 7, 2022
1 parent fd9a5a1 commit 7a2bbca
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 1 addition & 12 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,7 @@ impl<'cmd> Parser<'cmd> {
current_positional.get_id()
);

if self.cmd.is_allow_hyphen_values_set()
|| self.cmd[&current_positional.id].is_allow_hyphen_values_set()
if self.cmd[&current_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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/builder/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" <val>))
.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);
Expand Down

0 comments on commit 7a2bbca

Please sign in to comment.