2022-02-12 03:48:29 +00:00
|
|
|
use clap::{Arg, Command};
|
2016-09-18 19:24:59 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_default_no_delim() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("no_delim")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(Arg::new("option").long("option").takes_value(true))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option", "val1,val2,val3"]);
|
2016-09-18 19:24:59 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-18 19:24:59 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
|
|
|
|
"val1,val2,val3"
|
|
|
|
);
|
2016-09-18 19:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_eq_no_delim() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("no_delim")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(Arg::new("option").long("option").takes_value(true))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option=val1,val2,val3"]);
|
2016-09-18 19:24:59 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-18 19:24:59 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
|
|
|
|
"val1,val2,val3"
|
|
|
|
);
|
2016-09-18 19:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_s_eq_no_delim() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("no_delim")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(Arg::new("option").short('o').takes_value(true))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o=val1,val2,val3"]);
|
2016-09-18 19:24:59 +00:00
|
|
|
|
2017-05-06 23:12:07 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err());
|
2016-09-18 19:24:59 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
|
|
|
|
"val1,val2,val3"
|
|
|
|
);
|
2016-09-18 19:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_s_default_no_delim() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("no_delim")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(Arg::new("option").short('o').takes_value(true))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1,val2,val3"]);
|
2016-09-18 19:24:59 +00:00
|
|
|
|
2017-05-06 23:12:07 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err());
|
2016-09-18 19:24:59 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
|
|
|
|
"val1,val2,val3"
|
|
|
|
);
|
2016-09-18 19:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_s_no_space_no_delim() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("no_delim")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(Arg::new("option").short('o').takes_value(true))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1,val2,val3"]);
|
2016-09-18 19:24:59 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-18 19:24:59 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
|
|
|
|
"val1,val2,val3"
|
|
|
|
);
|
2016-09-18 19:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_s_no_space_mult_no_delim() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("no_delim")
|
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-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.multiple_values(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1,val2,val3"]);
|
2016-09-18 19:24:59 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-18 19:24:59 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
|
|
|
|
"val1,val2,val3"
|
|
|
|
);
|
2016-09-18 19:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_eq_mult_def_delim() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("no_delim")
|
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("opt")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
2022-02-09 17:16:34 +00:00
|
|
|
.use_value_delimiter(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=val1,val2,val3"]);
|
2016-09-18 19:24:59 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-09-18 19:24:59 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("option")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
&["val1", "val2", "val3"]
|
|
|
|
);
|
2016-09-18 19:24:59 +00:00
|
|
|
}
|