2015-12-10 06:41:50 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
2016-01-21 05:18:53 +00:00
|
|
|
use clap::{App, Arg, ErrorKind};
|
2015-12-10 06:41:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_long() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.long("option")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"--option", "val1",
|
|
|
|
"--option", "val2",
|
|
|
|
"--option", "val3",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_short() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
"-o", "val3",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_mixed() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.long("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"--option", "val2",
|
|
|
|
"--option", "val3",
|
|
|
|
"-o", "val4",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 4);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3", "val4"]));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_exact_exact() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true)
|
|
|
|
.number_of_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
"-o", "val3",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_exact_less() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true)
|
|
|
|
.number_of_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumValues);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_exact_more() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true)
|
|
|
|
.number_of_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
"-o", "val3",
|
|
|
|
"-o", "val4",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumValues);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_min_exact() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.min_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
"-o", "val3",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_min_less() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.min_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::TooFewValues);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_min_more() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.min_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
"-o", "val3",
|
|
|
|
"-o", "val4",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 4);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3", "val4"]));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_max_exact() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.max_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
"-o", "val3",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_max_less() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.max_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 2);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), vec!["val1", "val2"]));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-01-13 16:28:24 +00:00
|
|
|
fn multiple_values_of_option_max_more() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("option")
|
|
|
|
.short("o")
|
|
|
|
.help("multiple options")
|
|
|
|
.takes_value(true)
|
|
|
|
.max_values(3))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"",
|
|
|
|
"-o", "val1",
|
|
|
|
"-o", "val2",
|
|
|
|
"-o", "val3",
|
|
|
|
"-o", "val4",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::TooManyValues);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.multiple(true))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("pos").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]));
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_exact_exact() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.multiple(true)
|
|
|
|
.number_of_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("pos").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]));
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_exact_less() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.multiple(true)
|
|
|
|
.number_of_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumValues);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_exact_more() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.multiple(true)
|
|
|
|
.number_of_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3", "val4"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumValues);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_min_exact() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.min_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("pos").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]));
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_min_less() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.min_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::TooFewValues);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_min_more() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.min_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3", "val4"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 4);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("pos").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3", "val4"]));
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_max_exact() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.max_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 3);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("pos").unwrap().collect::<Vec<_>>(), vec!["val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_max_less() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.max_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 2);
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("pos").unwrap().collect::<Vec<_>>(), vec!["val1", "val2"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_values_of_positional_max_more() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(Arg::with_name("pos")
|
|
|
|
.help("multiple positionals")
|
|
|
|
.index(1)
|
|
|
|
.max_values(3))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3", "val4"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::TooManyValues);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|