Skip to content

Commit

Permalink
Merge pull request #2804 from epage/multiple
Browse files Browse the repository at this point in the history
fix(parser): Allow multiple_occurrecnes with positional args
  • Loading branch information
epage authored Oct 4, 2021
2 parents edd0124 + 8e780e3 commit 62eff1f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
9 changes: 0 additions & 9 deletions src/build/arg/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ pub(crate) fn assert_arg(arg: &Arg) {
);
}

// Positionals should not have multiple_occurrences
if arg.is_positional() {
assert!(
!arg.is_set(ArgSettings::MultipleOccurrences),
"Argument '{}' is a positional argument and can't be set as multiple occurrences",
arg.name
);
}

if arg.is_set(ArgSettings::Required) {
assert!(
arg.default_vals.is_empty(),
Expand Down
61 changes: 61 additions & 0 deletions tests/multiple_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,3 +1319,64 @@ fn number_of_values_preferred_over_value_names() {
["val1", "val2", "val3", "val4"]
);
}

#[test]
fn values_per_occurrence_named() {
let mut a = App::new("test").arg(
Arg::new("pos")
.long("pos")
.number_of_values(2)
.multiple_occurrences(true),
);

let m = a.try_get_matches_from_mut(vec!["myprog", "--pos", "val1", "val2"]);
let m = match m {
Ok(m) => m,
Err(err) => panic!("{}", err),
};
assert_eq!(
m.values_of("pos").unwrap().collect::<Vec<_>>(),
["val1", "val2"]
);

let m = a.try_get_matches_from_mut(vec![
"myprog", "--pos", "val1", "val2", "--pos", "val3", "val4",
]);
let m = match m {
Ok(m) => m,
Err(err) => panic!("{}", err),
};
assert_eq!(
m.values_of("pos").unwrap().collect::<Vec<_>>(),
["val1", "val2", "val3", "val4"]
);
}

#[test]
fn values_per_occurrence_positional() {
let mut a = App::new("test").arg(
Arg::new("pos")
.number_of_values(2)
.multiple_occurrences(true),
);

let m = a.try_get_matches_from_mut(vec!["myprog", "val1", "val2"]);
let m = match m {
Ok(m) => m,
Err(err) => panic!("{}", err),
};
assert_eq!(
m.values_of("pos").unwrap().collect::<Vec<_>>(),
["val1", "val2"]
);

let m = a.try_get_matches_from_mut(vec!["myprog", "val1", "val2", "val3", "val4"]);
let m = match m {
Ok(m) => m,
Err(err) => panic!("{}", err),
};
assert_eq!(
m.values_of("pos").unwrap().collect::<Vec<_>>(),
["val1", "val2", "val3", "val4"]
);
}
11 changes: 0 additions & 11 deletions tests/positionals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,3 @@ fn positional_arg_with_short() {
.arg(Arg::new("arg").index(1).short('a'))
.try_get_matches();
}

#[cfg(debug_assertions)]
#[test]
#[should_panic = "Argument 'arg' is a positional argument and can't be set as multiple occurrences"]
fn positional_arg_with_multiple_occurrences() {
use clap::{App, Arg};

let _ = App::new("test")
.arg(Arg::new("arg").multiple_occurrences(true))
.try_get_matches();
}

0 comments on commit 62eff1f

Please sign in to comment.