Skip to content

Commit

Permalink
Merge pull request #1198 from willmurphyscode/port-1161-fix-to-v3
Browse files Browse the repository at this point in the history
Port 1161 fix to v3
  • Loading branch information
kbknapp authored Mar 4, 2018
2 parents bd08e73 + 23a48ea commit 1567d80
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::iter::Peekable;
use std::mem;

// Third party
use vec_map::VecMap;
use map::{self, VecMap};

// Internal
use INTERNAL_ERROR_MSG;
Expand Down Expand Up @@ -109,6 +109,7 @@ where
}
}


#[cfg_attr(feature = "lints", allow(block_in_if_condition_stmt))]
fn _verify_positionals(&mut self) -> bool {
debugln!("Parser::_verify_positionals;");
Expand All @@ -118,7 +119,18 @@ where
// Firt we verify that the index highest supplied index, is equal to the number of
// positional arguments to verify there are no gaps (i.e. supplying an index of 1 and 3
// but no 2)
let highest_idx = self.positionals.keys().last().unwrap_or(0);
#[cfg(feature = "vec_map")]
fn _highest_idx(map: &VecMap<&str>) -> usize {
map.keys().last().unwrap_or(0)
}

#[cfg(not(feature = "vec_map"))]
fn _highest_idx(map: &VecMap<&str>) -> usize {
*map.keys().last().unwrap_or(&0)
}

let highest_idx = _highest_idx(&self.positionals);

let num_p = self.positionals.len();

assert!(
Expand Down Expand Up @@ -369,7 +381,8 @@ where
self.unset(AS::ValidNegNumFound);
// Is this a new argument, or values from a previous option?
let starts_new_arg = self.is_new_arg(&arg_os, needs_val_of);
if arg_os.starts_with(b"--") && arg_os.len_() == 2 && starts_new_arg {
if !self.is_set(AS::TrailingValues) &&
arg_os.starts_with(b"--") && arg_os.len_() == 2 && starts_new_arg {
debugln!("Parser::get_matches_with: setting TrailingVals=true");
self.set(AS::TrailingValues);
continue;
Expand Down
2 changes: 2 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ mod vec_map {

pub fn values(&self) -> Values<V> { self.inner.values() }

pub fn keys(&self) -> btree_map::Keys<usize, V> { self.inner.keys() }

pub fn iter(&self) -> Iter<V> {
Iter {
inner: self.inner.iter(),
Expand Down
20 changes: 20 additions & 0 deletions tests/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,23 @@ fn issue_1031_args_with_same_name_no_more_vals() {
assert_eq!(m.value_of("ui-path"), Some("value"));
assert_eq!(m.subcommand_name(), Some("signer"));
}

#[test]
fn issue_1161_multiple_hyphen_hyphen() {
// from example 22
let res = App::new("myprog")
.arg(Arg::with_name("eff").short("f"))
.arg(Arg::with_name("pea").short("p").takes_value(true))
.arg(Arg::with_name("slop").multiple(true).last(true))
.get_matches_from_safe(vec!["-f", "-p=bob", "--", "sloppy", "slop", "-a", "--", "subprogram", "position", "args"]);

assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
let m = res.unwrap();

let expected = Some(vec!["sloppy", "slop", "-a", "--", "subprogram", "position", "args"]);
let actual = m
.values_of("slop")
.map(|vals| vals.collect::<Vec<_>>());

assert_eq!(expected, actual);
}

0 comments on commit 1567d80

Please sign in to comment.