From c8335168074c37aff7dbde57c7f165fe15254b58 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 4 Jan 2022 09:04:31 -0600 Subject: [PATCH] fix(parser): Ignore Last when checking Hyphe Values This was found with #3249 --- src/parse/parser.rs | 9 +++------ tests/builder/positionals.rs | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 3deec05f4631..80dde6aaebbc 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -1194,12 +1194,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 diff --git a/tests/builder/positionals.rs b/tests/builder/positionals.rs index 6452aac2d440..e7f1a6d49db9 100644 --- a/tests/builder/positionals.rs +++ b/tests/builder/positionals.rs @@ -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")); +}