Skip to content

Commit

Permalink
test: add validation test for #1794
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed Aug 5, 2021
1 parent 8b6034e commit d4e3f52
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion tests/positionals.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{App, Arg, ErrorKind};
use clap::{App, AppSettings, Arg, ArgGroup, ErrorKind};

#[test]
fn only_pos_follow() {
Expand Down Expand Up @@ -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"));
}

0 comments on commit d4e3f52

Please sign in to comment.