2015-08-27 21:03:45 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
2018-01-31 20:15:01 +00:00
|
|
|
use clap::{App, Arg, ArgSettings};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_long() {
|
2016-09-10 22:18:43 +00:00
|
|
|
let m = App::new("mo_flags_long")
|
2018-01-31 20:15:01 +00:00
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("--multflag 'allowed multiple flag'")
|
2018-01-31 20:15:01 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
|
|
|
)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--flag 'disallowed multiple flag'"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.get_matches_from(vec!["", "--multflag", "--flag", "--multflag"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("multflag"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag"), 2);
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_short() {
|
2016-09-10 22:18:43 +00:00
|
|
|
let m = App::new("mo_flags_short")
|
2018-01-31 20:15:01 +00:00
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-m --multflag 'allowed multiple flag'")
|
2018-01-31 20:15:01 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
|
|
|
)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f --flag 'disallowed multiple flag'"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.get_matches_from(vec!["", "-m", "-f", "-m"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("multflag"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag"), 2);
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_mixed() {
|
2016-09-10 22:18:43 +00:00
|
|
|
let m = App::new("mo_flags_mixed")
|
2018-01-31 20:15:01 +00:00
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-m, --multflag1 'allowed multiple flag'")
|
2018-01-31 20:15:01 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
|
|
|
)
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-n, --multflag2 'another allowed multiple flag'")
|
2018-01-31 20:15:01 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
|
|
|
)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f, --flag 'disallowed multiple flag'"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.get_matches_from(vec![
|
|
|
|
"",
|
|
|
|
"-m",
|
|
|
|
"-f",
|
|
|
|
"-n",
|
|
|
|
"--multflag1",
|
|
|
|
"-m",
|
|
|
|
"--multflag2",
|
|
|
|
]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("multflag1"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag1"), 3);
|
|
|
|
assert!(m.is_present("multflag2"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag2"), 2);
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 1);
|
2016-02-02 17:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_large_quantity() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let args: Vec<&str> = vec![""]
|
|
|
|
.into_iter()
|
|
|
|
.chain(vec!["-m"; 1024].into_iter())
|
|
|
|
.collect();
|
2016-09-10 22:18:43 +00:00
|
|
|
let m = App::new("mo_flags_larg_qty")
|
2018-01-31 20:15:01 +00:00
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-m --multflag 'allowed multiple flag'")
|
2018-01-31 20:15:01 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
|
|
|
)
|
2018-01-25 04:05:05 +00:00
|
|
|
.get_matches_from(args);
|
2016-02-02 17:57:02 +00:00
|
|
|
assert!(m.is_present("multflag"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag"), 1024);
|
|
|
|
}
|