Skip to content

Commit

Permalink
fix(parser): Ignore Last when checking Hyphe Values
Browse files Browse the repository at this point in the history
This was found with clap-rs#3249
  • Loading branch information
epage committed Jan 4, 2022
1 parent e6ee585 commit 15704dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,9 @@ impl<'help, 'app> Parser<'help, 'app> {
{
debug!("Parser::parse_short_args: prior arg accepts hyphenated values",);
return ParseResult::MaybeHyphenValue;
} else if self
.app
.args
.get(&pos_counter)
.map_or(false, |arg| arg.is_set(ArgSettings::AllowHyphenValues))
{
} else if self.app.args.get(&pos_counter).map_or(false, |arg| {
arg.is_set(ArgSettings::AllowHyphenValues) && !arg.is_set(ArgSettings::Last)
}) {
debug!(
"Parser::parse_short_args: positional at {} allows hyphens",
pos_counter
Expand Down
21 changes: 21 additions & 0 deletions tests/builder/positionals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,24 @@ fn positional_arg_with_short() {
.arg(Arg::new("arg").index(1).short('a'))
.try_get_matches();
}

#[test]
fn ignore_hyphen_values_on_last() {
let app = clap::App::new("foo")
.arg(
clap::Arg::new("cmd")
.multiple_values(true)
.last(true)
.allow_hyphen_values(true),
)
.arg(
clap::Arg::new("name")
.long("name")
.short('n')
.takes_value(true)
.required(false),
);

let matches = app.try_get_matches_from(["test", "-n", "foo"]).unwrap();
assert_eq!(matches.value_of("name"), Some("foo"));
}

0 comments on commit 15704dc

Please sign in to comment.