From d4e3f52389f2b5aeebd7940ccebfeed1fa65df64 Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 6 Aug 2021 00:44:20 +0200 Subject: [PATCH] test: add validation test for #1794 --- src/parse/parser.rs | 10 ++++++++++ tests/positionals.rs | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 6d5fa9b46553..abfea9340604 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -475,6 +475,10 @@ impl<'help, 'app> Parser<'help, 'app> { "Parser::get_matches_with: Positional counter...{}", pos_counter ); + debug!( + "Parser::get_matches_with: Missing positional...{:?}", + missing_pos + ); debug!( "Parser::get_matches_with: Low index multiples...{:?}", low_index_mults @@ -492,6 +496,11 @@ impl<'help, 'app> Parser<'help, 'app> { ParseResult::ValuesDone }; + debug!( + "Parser::get_matches_with: Checking `{:?}` for pos_counter bump", + n + ); + // If the arg doesn't looks like a new_arg and it's not a // subcommand, don't bump the position counter. let n = ArgStr::new(n); @@ -504,6 +513,7 @@ impl<'help, 'app> Parser<'help, 'app> { debug!("Parser::get_matches_with: Bumping the positional counter..."); pos_counter + 1 } else { + debug!("Parser::get_matches_with: No bump required"); pos_counter } } else if self.is_set(AS::TrailingValues) diff --git a/tests/positionals.rs b/tests/positionals.rs index d085b3169bb6..044699270f24 100644 --- a/tests/positionals.rs +++ b/tests/positionals.rs @@ -1,4 +1,4 @@ -use clap::{App, Arg, ErrorKind}; +use clap::{App, AppSettings, Arg, ArgGroup, ErrorKind}; #[test] fn only_pos_follow() { @@ -307,3 +307,25 @@ fn positional_arg_with_multiple_occurrences() { .arg(Arg::new("arg").multiple_occurrences(true)) .try_get_matches(); } + +/// Test for https://github.com/clap-rs/clap/issues/1794. +#[test] +fn positional_args_with_options() { + let app = clap::App::new("hello") + .bin_name("deno") + .setting(AppSettings::AllowMissingPositional) + .arg(Arg::new("option1").long("option1").takes_value(false)) + .arg(Arg::new("pos1").takes_value(true)) + .group( + ArgGroup::new("arg1") + .args(&["pos1", "option1"]) + .required(true), + ) + .arg(Arg::new("pos2").takes_value(true)); + + let m = app.get_matches_from(&["deno", "--option1", "abcd"]); + assert!(m.is_present("option1")); + assert!(!m.is_present("pos1")); + assert!(m.is_present("pos2")); + assert_eq!(m.value_of("pos2"), Some("abcd")); +}