2022-02-02 21:41:24 +00:00
|
|
|
use clap::{error::ErrorKind, App, Arg};
|
2015-12-10 06:41:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_long() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-07-23 19:09:42 +00:00
|
|
|
"", "--option", "val1", "--option", "val2", "--option", "val3",
|
2015-12-10 06:41:50 +00:00
|
|
|
]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_short() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_mixed() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-07-23 19:09:42 +00:00
|
|
|
"", "-o", "val1", "--option", "val2", "--option", "val3", "-o", "val4",
|
2015-12-10 06:41:50 +00:00
|
|
|
]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 4);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3", "val4"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_exact_exact() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.number_of_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 00:44:30 +00:00
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_exact_exact_not_mult() {
|
2016-02-05 00:44:30 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2018-01-25 04:05:05 +00:00
|
|
|
.number_of_values(3),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "val2", "val3"]);
|
2016-02-05 00:44:30 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-02-05 00:44:30 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-02-05 00:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_exact_exact_mult() {
|
2016-02-05 00:44:30 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.number_of_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-07-23 19:09:42 +00:00
|
|
|
"", "-o", "val1", "val2", "val3", "-o", "val4", "val5", "val6",
|
2016-02-05 00:44:30 +00:00
|
|
|
]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-02-05 00:44:30 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 2);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3", "val4", "val5", "val6"]
|
|
|
|
);
|
2016-02-05 00:44:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 06:41:50 +00:00
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_exact_less() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.number_of_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "-o", "val2"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::WrongNumberOfValues);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_exact_more() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.number_of_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-07-23 19:09:42 +00:00
|
|
|
"", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4",
|
2015-12-10 06:41:50 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::WrongNumberOfValues);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_min_exact() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.min_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_min_less() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.min_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "-o", "val2"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::TooFewValues);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-05 03:15:10 +00:00
|
|
|
fn option_short_min_more_mult_occurs() {
|
2018-01-25 21:23:12 +00:00
|
|
|
let res = App::new("multiple_values")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("arg").required(true))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.min_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-07-23 19:09:42 +00:00
|
|
|
"", "pos", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4",
|
2015-12-10 06:41:50 +00:00
|
|
|
]);
|
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
|
2018-01-25 21:23:12 +00:00
|
|
|
let m = res.unwrap();
|
2015-12-10 06:41:50 +00:00
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
2016-02-05 03:15:10 +00:00
|
|
|
assert!(m.is_present("arg"));
|
2015-12-10 06:41:50 +00:00
|
|
|
assert_eq!(m.occurrences_of("option"), 4);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3", "val4"]
|
|
|
|
);
|
2016-02-05 03:15:10 +00:00
|
|
|
assert_eq!(m.value_of("arg"), Some("pos"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_short_min_more_single_occur() {
|
2018-01-25 21:23:12 +00:00
|
|
|
let res = App::new("multiple_values")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("arg").required(true))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2018-01-25 04:05:05 +00:00
|
|
|
.min_values(3),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "pos", "-o", "val1", "val2", "val3", "val4"]);
|
2018-01-25 04:05:05 +00:00
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
|
2018-01-25 21:23:12 +00:00
|
|
|
let m = res.unwrap();
|
2016-02-05 03:15:10 +00:00
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3", "val4"]
|
|
|
|
);
|
2016-02-05 03:15:10 +00:00
|
|
|
assert_eq!(m.value_of("arg"), Some("pos"));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_max_exact() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.max_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_max_less() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.max_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "-o", "val2"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 2);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_max_more() {
|
2015-12-10 06:41:50 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.max_values(3)
|
|
|
|
.multiple_occurrences(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-07-23 19:09:42 +00:00
|
|
|
"", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4",
|
2015-12-10 06:41:50 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +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]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("pos")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple positionals")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_exact_exact() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("pos")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple positionals")
|
2018-01-25 04:05:05 +00:00
|
|
|
.number_of_values(3),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_exact_less() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("pos")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple positionals")
|
2018-01-25 04:05:05 +00:00
|
|
|
.number_of_values(3),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::WrongNumberOfValues);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_exact_more() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("pos")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple positionals")
|
2018-01-25 04:05:05 +00:00
|
|
|
.number_of_values(3),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3", "val4"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::WrongNumberOfValues);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_min_exact() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2021-11-18 16:17:15 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").min_values(3))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_min_less() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2021-11-18 16:17:15 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").min_values(3))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::TooFewValues);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_min_more() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2021-11-18 16:17:15 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").min_values(3))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3", "val4"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 4);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3", "val4"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_max_exact() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2021-11-18 16:17:15 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").max_values(3))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 3);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_max_less() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2021-11-18 16:17:15 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").max_values(3))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("pos"));
|
|
|
|
assert_eq!(m.occurrences_of("pos"), 2);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_max_more() {
|
2016-01-13 16:28:24 +00:00
|
|
|
let m = App::new("multiple_values")
|
2021-11-18 16:17:15 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").max_values(3))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3", "val4"]);
|
2016-01-13 16:28:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::TooManyValues);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
2016-01-26 07:05:32 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_long_equals() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.use_delimiter(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option=val1,val2,val3"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_long_space() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.use_delimiter(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option", "val1,val2,val3"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_short_equals() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.use_delimiter(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o=val1,val2,val3"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_short_space() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.use_delimiter(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1,val2,val3"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_short_no_space() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.use_delimiter(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-oval1,val2,val3"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_positional() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.use_delimiter(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "val1,val2,val3"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-26 07:05:32 +00:00
|
|
|
}
|
2016-01-26 15:14:27 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn different_sep() {
|
2016-01-26 15:14:27 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-08-13 18:54:56 +00:00
|
|
|
.value_delimiter(';'),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option=val1;val2;val3"]);
|
2016-01-26 15:14:27 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 15:14:27 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-26 15:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn different_sep_positional() {
|
2016-01-26 15:14:27 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-08-13 18:54:56 +00:00
|
|
|
.value_delimiter(';'),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "val1;val2;val3"]);
|
2016-01-26 15:14:27 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 15:14:27 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-26 15:14:27 +00:00
|
|
|
}
|
2016-01-26 15:52:31 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn no_sep() {
|
2016-01-26 15:52:31 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.use_delimiter(false),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option=val1,val2,val3"]);
|
2016-01-26 15:52:31 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 15:52:31 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
|
|
|
assert_eq!(m.value_of("option").unwrap(), "val1,val2,val3");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn no_sep_positional() {
|
2016-01-26 15:52:31 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-06-16 05:28:25 +00:00
|
|
|
.takes_value(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.use_delimiter(false),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "val1,val2,val3"]);
|
2016-01-26 15:52:31 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-26 15:52:31 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
|
|
|
assert_eq!(m.value_of("option").unwrap(), "val1,val2,val3");
|
|
|
|
}
|
2016-09-29 13:55:05 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_long() {
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.use_delimiter(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.require_delimiter(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple_values(true)
|
|
|
|
.index(1),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option", "val1", "val2", "val3"]);
|
2016-09-29 13:55:05 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val1"]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("args").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val2", "val3"]
|
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_long_with_equal() {
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.use_delimiter(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.require_delimiter(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple_values(true)
|
|
|
|
.index(1),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option=val1", "val2", "val3"]);
|
2016-09-29 13:55:05 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val1"]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("args").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val2", "val3"]
|
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_short_with_space() {
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.use_delimiter(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.require_delimiter(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple_values(true)
|
|
|
|
.index(1),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "val2", "val3"]);
|
2016-09-29 13:55:05 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val1"]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("args").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val2", "val3"]
|
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_short_with_no_space() {
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.use_delimiter(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.require_delimiter(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple_values(true)
|
|
|
|
.index(1),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-oval1", "val2", "val3"]);
|
2016-09-29 13:55:05 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val1"]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("args").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val2", "val3"]
|
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_short_with_equal() {
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.use_delimiter(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.require_delimiter(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple_values(true)
|
|
|
|
.index(1),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o=val1", "val2", "val3"]);
|
2016-09-29 13:55:05 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val1"]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("args").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["val2", "val3"]
|
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_complex() {
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = App::new("multiple_values")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
|
|
|
.multiple_occurrences(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.use_delimiter(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.require_delimiter(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple_values(true)
|
|
|
|
.index(1),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2016-09-29 13:55:05 +00:00
|
|
|
"",
|
|
|
|
"val1",
|
2018-01-25 04:05:05 +00:00
|
|
|
"-oval2",
|
|
|
|
"val3",
|
|
|
|
"-o",
|
|
|
|
"val4",
|
|
|
|
"val5",
|
|
|
|
"-o=val6",
|
|
|
|
"val7",
|
|
|
|
"--option=val8",
|
|
|
|
"val9",
|
|
|
|
"--option",
|
|
|
|
"val10",
|
|
|
|
"val11",
|
|
|
|
"-oval12,val13",
|
|
|
|
"val14",
|
|
|
|
"-o",
|
|
|
|
"val15,val16",
|
|
|
|
"val17",
|
|
|
|
"-o=val18,val19",
|
|
|
|
"val20",
|
|
|
|
"--option=val21,val22",
|
|
|
|
"val23",
|
|
|
|
"--option",
|
|
|
|
"val24,val25",
|
|
|
|
"val26",
|
2016-09-29 13:55:05 +00:00
|
|
|
]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-29 13:55:05 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.occurrences_of("option"), 10);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
&[
|
|
|
|
"val2", "val4", "val6", "val8", "val10", "val12", "val13", "val15", "val16", "val18",
|
|
|
|
"val19", "val21", "val22", "val24", "val25",
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("args").unwrap().collect::<Vec<_>>(),
|
|
|
|
&[
|
|
|
|
"val1", "val3", "val5", "val7", "val9", "val11", "val14", "val17", "val20", "val23",
|
|
|
|
"val26",
|
|
|
|
]
|
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
2016-11-01 20:15:06 +00:00
|
|
|
|
2019-06-25 23:02:53 +00:00
|
|
|
// This tests a programmer error and will only succeed with debug_assertions
|
|
|
|
#[cfg(debug_assertions)]
|
2016-11-01 20:15:06 +00:00
|
|
|
#[test]
|
2020-04-09 22:33:16 +00:00
|
|
|
#[should_panic = "When using a positional argument with \
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true) that is *not the last* positional argument, the last \
|
2020-10-08 22:27:13 +00:00
|
|
|
positional argument (i.e. the one with the highest index) *must* have \
|
2020-04-09 22:33:16 +00:00
|
|
|
.required(true) or .last(true) set."]
|
2016-11-01 20:15:06 +00:00
|
|
|
fn low_index_positional_not_required() {
|
|
|
|
let _ = App::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("target").index(2))
|
2019-06-25 23:02:53 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2016-11-01 20:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 23:02:53 +00:00
|
|
|
// This tests a programmer error and will only succeed with debug_assertions
|
|
|
|
#[cfg(debug_assertions)]
|
2016-11-01 20:15:06 +00:00
|
|
|
#[test]
|
2021-06-16 05:28:25 +00:00
|
|
|
#[should_panic = "Only one positional argument with .multiple_values(true) \
|
2020-04-09 22:33:16 +00:00
|
|
|
set is allowed per command, unless the second one also has .last(true) set"]
|
2016-11-01 20:15:06 +00:00
|
|
|
fn low_index_positional_last_multiple_too() {
|
|
|
|
let _ = App::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("target")
|
|
|
|
.index(2)
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2019-06-25 23:02:53 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2016-11-01 20:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 23:02:53 +00:00
|
|
|
// This tests a programmer error and will only succeed with debug_assertions
|
|
|
|
#[cfg(debug_assertions)]
|
2016-11-01 20:15:06 +00:00
|
|
|
#[test]
|
2020-04-09 22:33:16 +00:00
|
|
|
#[should_panic = "Only the last positional argument, or second to \
|
2021-06-16 05:28:25 +00:00
|
|
|
last positional argument may be set to .multiple_values(true)"]
|
2016-11-01 20:15:06 +00:00
|
|
|
fn low_index_positional_too_far_back() {
|
|
|
|
let _ = App::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("target").required(true).index(2))
|
|
|
|
.arg(Arg::new("target2").required(true).index(3))
|
2019-06-25 23:02:53 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2016-11-01 20:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn low_index_positional() {
|
|
|
|
let m = App::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("target").index(2).required(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["lip", "file1", "file2", "file3", "target"]);
|
2016-11-01 20:15:06 +00:00
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2016-11-01 20:15:06 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("files"));
|
|
|
|
assert_eq!(m.occurrences_of("files"), 3);
|
|
|
|
assert!(m.is_present("target"));
|
|
|
|
assert_eq!(m.occurrences_of("target"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("files").unwrap().collect::<Vec<_>>(),
|
|
|
|
["file1", "file2", "file3"]
|
|
|
|
);
|
2016-11-01 20:15:06 +00:00
|
|
|
assert_eq!(m.value_of("target").unwrap(), "target");
|
|
|
|
}
|
|
|
|
|
2016-11-02 03:32:26 +00:00
|
|
|
#[test]
|
|
|
|
fn low_index_positional_in_subcmd() {
|
|
|
|
let m = App::new("lip")
|
2018-01-25 04:05:05 +00:00
|
|
|
.subcommand(
|
2018-10-19 20:42:13 +00:00
|
|
|
App::new("test")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("target").index(2).required(true)),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["lip", "test", "file1", "file2", "file3", "target"]);
|
2016-11-02 03:32:26 +00:00
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2016-11-02 03:32:26 +00:00
|
|
|
let m = m.unwrap();
|
2018-01-25 04:05:05 +00:00
|
|
|
let sm = m.subcommand_matches("test").unwrap();
|
2016-11-02 03:32:26 +00:00
|
|
|
|
|
|
|
assert!(sm.is_present("files"));
|
|
|
|
assert_eq!(sm.occurrences_of("files"), 3);
|
|
|
|
assert!(sm.is_present("target"));
|
|
|
|
assert_eq!(sm.occurrences_of("target"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
sm.values_of("files").unwrap().collect::<Vec<_>>(),
|
|
|
|
["file1", "file2", "file3"]
|
|
|
|
);
|
2016-11-02 03:32:26 +00:00
|
|
|
assert_eq!(sm.value_of("target").unwrap(), "target");
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:15:06 +00:00
|
|
|
#[test]
|
|
|
|
fn low_index_positional_with_option() {
|
|
|
|
let m = App::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.required(true)
|
|
|
|
.index(1)
|
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("target").index(2).required(true))
|
|
|
|
.arg(Arg::new("opt").long("option").takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-07-23 19:09:42 +00:00
|
|
|
"lip", "file1", "file2", "file3", "target", "--option", "test",
|
2016-11-01 20:15:06 +00:00
|
|
|
]);
|
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2016-11-01 20:15:06 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("files"));
|
|
|
|
assert_eq!(m.occurrences_of("files"), 3);
|
|
|
|
assert!(m.is_present("target"));
|
|
|
|
assert_eq!(m.occurrences_of("target"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("files").unwrap().collect::<Vec<_>>(),
|
|
|
|
["file1", "file2", "file3"]
|
|
|
|
);
|
2016-11-01 20:15:06 +00:00
|
|
|
assert_eq!(m.value_of("target").unwrap(), "target");
|
|
|
|
assert_eq!(m.value_of("opt").unwrap(), "test");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn low_index_positional_with_flag() {
|
|
|
|
let m = App::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("target").index(2).required(true))
|
|
|
|
.arg(Arg::new("flg").long("flag"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["lip", "file1", "file2", "file3", "target", "--flag"]);
|
2016-11-01 20:15:06 +00:00
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2016-11-01 20:15:06 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("files"));
|
|
|
|
assert_eq!(m.occurrences_of("files"), 3);
|
|
|
|
assert!(m.is_present("target"));
|
|
|
|
assert_eq!(m.occurrences_of("target"), 1);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("files").unwrap().collect::<Vec<_>>(),
|
|
|
|
["file1", "file2", "file3"]
|
|
|
|
);
|
2016-11-01 20:15:06 +00:00
|
|
|
assert_eq!(m.value_of("target").unwrap(), "target");
|
|
|
|
assert!(m.is_present("flg"));
|
2016-11-02 03:32:26 +00:00
|
|
|
}
|
2017-01-01 19:43:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_value_terminator_option() {
|
|
|
|
let m = App::new("lip")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("files")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('f')
|
2018-01-25 04:05:05 +00:00
|
|
|
.value_terminator(";")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("other"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["lip", "-f", "val1", "val2", ";", "otherval"]);
|
2017-01-01 19:43:07 +00:00
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2017-01-01 19:43:07 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("other"));
|
|
|
|
assert_eq!(m.occurrences_of("other"), 1);
|
|
|
|
assert!(m.is_present("files"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("files").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2"]
|
|
|
|
);
|
2017-01-01 19:43:07 +00:00
|
|
|
assert_eq!(m.value_of("other"), Some("otherval"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_value_terminator_option_other_arg() {
|
|
|
|
let m = App::new("lip")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("files")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('f')
|
2018-01-25 04:05:05 +00:00
|
|
|
.value_terminator(";")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("other"))
|
|
|
|
.arg(Arg::new("flag").short('F'))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["lip", "-f", "val1", "val2", "-F", "otherval"]);
|
2017-01-01 19:43:07 +00:00
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2017-01-01 19:43:07 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("other"));
|
|
|
|
assert!(m.is_present("files"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("files").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2"]
|
|
|
|
);
|
2017-01-01 19:43:07 +00:00
|
|
|
assert_eq!(m.value_of("other"), Some("otherval"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:34:25 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_vals_with_hyphen() {
|
2018-01-09 15:24:24 +00:00
|
|
|
let res = App::new("do")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cmds")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.allow_hyphen_values(true)
|
|
|
|
.value_terminator(";"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("location"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
"do",
|
|
|
|
"find",
|
|
|
|
"-type",
|
|
|
|
"f",
|
|
|
|
"-name",
|
|
|
|
"special",
|
|
|
|
";",
|
|
|
|
"/home/clap",
|
|
|
|
]);
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
|
2018-01-09 15:24:24 +00:00
|
|
|
|
|
|
|
let m = res.unwrap();
|
2017-01-02 19:34:25 +00:00
|
|
|
let cmds: Vec<_> = m.values_of("cmds").unwrap().collect();
|
|
|
|
assert_eq!(&cmds, &["find", "-type", "f", "-name", "special"]);
|
|
|
|
assert_eq!(m.value_of("location"), Some("/home/clap"));
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
2019-05-28 08:17:21 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1480_max_values_consumes_extra_arg_1() {
|
|
|
|
let res = App::new("prog")
|
2021-06-16 05:28:25 +00:00
|
|
|
.arg(Arg::new("field").max_values(1).long("field"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("positional").required(true).index(1))
|
2019-05-28 08:17:21 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--field", "1", "file"]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2019-05-28 08:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1480_max_values_consumes_extra_arg_2() {
|
|
|
|
let res = App::new("prog")
|
2021-06-16 05:28:25 +00:00
|
|
|
.arg(Arg::new("field").max_values(1).long("field"))
|
2019-05-28 08:17:21 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--field", "1", "2"]);
|
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument);
|
2019-05-28 08:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1480_max_values_consumes_extra_arg_3() {
|
|
|
|
let res = App::new("prog")
|
2021-06-16 05:28:25 +00:00
|
|
|
.arg(Arg::new("field").max_values(1).long("field"))
|
2019-05-28 08:17:21 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--field", "1", "2", "3"]);
|
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument);
|
2019-05-28 08:17:21 +00:00
|
|
|
}
|
2021-02-16 03:58:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_2229() {
|
|
|
|
let m = App::new("multiple_values")
|
|
|
|
.arg(
|
|
|
|
Arg::new("pos")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple positionals")
|
2021-02-16 03:58:58 +00:00
|
|
|
.number_of_values(3),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"myprog", "val1", "val2", "val3", "val4", "val5", "val6",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_err()); // This panics, because `m.is_err() == false`.
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::WrongNumberOfValues);
|
2021-02-16 03:58:58 +00:00
|
|
|
}
|
2021-08-13 23:22:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn value_names_building_num_vals() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("pos")
|
|
|
|
.long("pos")
|
|
|
|
.value_names(&["who", "what", "why"]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog", "--pos", "val1", "val2", "val3"]);
|
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2021-08-13 23:22:38 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn value_names_building_num_vals_for_positional() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.arg(Arg::new("pos").value_names(&["who", "what", "why"]))
|
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
|
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2021-08-13 23:22:38 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn number_of_values_preferred_over_value_names() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.arg(
|
|
|
|
Arg::new("pos")
|
|
|
|
.long("pos")
|
|
|
|
.number_of_values(4)
|
|
|
|
.value_names(&["who", "what", "why"]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog", "--pos", "val1", "val2", "val3", "val4"]);
|
|
|
|
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
2021-08-13 23:22:38 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("pos").unwrap().collect::<Vec<_>>(),
|
|
|
|
["val1", "val2", "val3", "val4"]
|
|
|
|
);
|
|
|
|
}
|
2021-10-04 17:09:54 +00:00
|
|
|
|
|
|
|
#[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"]
|
|
|
|
);
|
|
|
|
}
|