2022-06-07 18:48:48 +00:00
|
|
|
use clap::{error::ErrorKind, Arg, ArgAction, Command};
|
2024-05-24 15:13:50 +00:00
|
|
|
use snapbox::assert_data_eq;
|
|
|
|
use snapbox::str;
|
2015-12-10 06:41:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
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();
|
|
|
|
|
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"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
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();
|
|
|
|
|
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"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_mixed() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
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();
|
|
|
|
|
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", "val4"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_exact_exact() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2022-07-27 18:28:10 +00:00
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"", "-o", "val1", "val2", "val3", "-o", "val4", "val5", "val6",
|
|
|
|
]);
|
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();
|
|
|
|
|
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<_>>(),
|
2022-07-27 18:28:10 +00:00
|
|
|
["val1", "val2", "val3", "val4", "val5", "val6"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3),
|
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-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();
|
|
|
|
|
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-02-05 00:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_exact_exact_mult() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
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();
|
|
|
|
|
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", "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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
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());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::WrongNumberOfValues);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: 3 values required for '-o <option> <option> <option>' but 1 was provided
|
|
|
|
|
|
|
|
Usage: multiple_values [OPTIONS]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_exact_more() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
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());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::WrongNumberOfValues);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: 3 values required for '-o <option> <option> <option>' but 1 was provided
|
|
|
|
|
|
|
|
Usage: multiple_values [OPTIONS]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_min_exact() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3..)
|
2022-07-28 22:13:41 +00:00
|
|
|
.action(ArgAction::Set),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2022-07-28 22:13:41 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "val2", "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();
|
|
|
|
|
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"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_min_less() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3..)
|
2022-07-28 22:13:41 +00:00
|
|
|
.action(ArgAction::Set),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2022-07-28 22:13:41 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "val2"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::TooFewValues);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
2024-06-10 14:16:53 +00:00
|
|
|
error: 3 values required by '-o <option> <option> <option>...'; only 2 were provided
|
2024-06-10 14:15:48 +00:00
|
|
|
|
|
|
|
Usage: multiple_values [OPTIONS]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-05 03:15:10 +00:00
|
|
|
fn option_short_min_more_mult_occurs() {
|
2022-07-26 21:54:14 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3..)
|
2022-07-28 22:13:41 +00:00
|
|
|
.action(ArgAction::Set),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2022-07-28 22:13:41 +00:00
|
|
|
.try_get_matches_from(vec!["", "pos", "-o", "val1", "val2", "val3", "val4"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
2022-07-26 21:54:14 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
let m = m.unwrap();
|
2015-12-10 06:41:50 +00:00
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
|
|
|
assert!(m.contains_id("arg"));
|
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", "val4"]
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("arg").map(|v| v.as_str()), Some("pos"));
|
2016-02-05 03:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_short_min_more_single_occur() {
|
2022-07-26 21:54:14 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(3..),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
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-07-26 21:54:14 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
let m = m.unwrap();
|
2016-02-05 03:15:10 +00:00
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
|
|
|
assert!(m.contains_id("arg"));
|
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", "val4"]
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("arg").map(|v| v.as_str()), Some("pos"));
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_max_exact() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..=3)
|
2022-07-29 02:40:40 +00:00
|
|
|
.action(ArgAction::Set),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2022-07-29 02:40:40 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "val2", "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();
|
|
|
|
|
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"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_max_less() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..=3)
|
2022-07-29 02:40:40 +00:00
|
|
|
.action(ArgAction::Set),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2022-07-29 02:40:40 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "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();
|
|
|
|
|
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"]
|
|
|
|
);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 21:51:41 +00:00
|
|
|
#[test]
|
|
|
|
fn option_max_zero() {
|
|
|
|
let m = Command::new("multiple_values")
|
|
|
|
.arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('o')
|
|
|
|
.help("multiple options")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..=3)
|
2022-07-29 02:40:40 +00:00
|
|
|
.action(ArgAction::Set),
|
2022-07-28 21:51:41 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "-o"]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::InvalidValue);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: a value is required for '-o <option>...' but none was supplied
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2022-07-28 21:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_max_zero_eq() {
|
|
|
|
let m = Command::new("multiple_values")
|
|
|
|
.arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('o')
|
|
|
|
.help("multiple options")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..=3)
|
2022-07-29 02:40:40 +00:00
|
|
|
.action(ArgAction::Set),
|
2022-07-28 21:51:41 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "-o="]);
|
|
|
|
|
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.contains_id("option"));
|
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("option")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
[""]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-10 06:41:50 +00:00
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn option_max_more() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..=3)
|
2022-07-29 02:40:40 +00:00
|
|
|
.action(ArgAction::Set),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2022-07-29 02:40:40 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "val1", "val2", "val3", "val4"]);
|
2015-12-10 06:41:50 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
2022-07-29 02:40:40 +00:00
|
|
|
// Can end up being TooManyValues or UnknownArgument
|
2024-06-10 14:15:48 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: unexpected argument 'val4' found
|
|
|
|
|
|
|
|
Usage: multiple_values [OPTIONS]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2015-12-10 06:41:50 +00:00
|
|
|
}
|
2016-01-13 16:28:24 +00:00
|
|
|
|
2022-07-28 21:16:14 +00:00
|
|
|
#[test]
|
|
|
|
fn optional_value() {
|
2022-09-23 20:18:14 +00:00
|
|
|
let mut cmd = Command::new("test").args_override_self(true).arg(
|
2022-07-28 21:16:14 +00:00
|
|
|
Arg::new("port")
|
|
|
|
.short('p')
|
|
|
|
.value_name("NUM")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(0..=1),
|
2022-07-28 21:16:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let r = cmd.try_get_matches_from_mut(["test", "-p42"]);
|
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.contains_id("port"));
|
|
|
|
assert_eq!(m.get_one::<String>("port").unwrap(), "42");
|
|
|
|
|
|
|
|
let r = cmd.try_get_matches_from_mut(["test", "-p"]);
|
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.contains_id("port"));
|
|
|
|
assert!(m.get_one::<String>("port").is_none());
|
|
|
|
|
|
|
|
let r = cmd.try_get_matches_from_mut(["test", "-p", "24", "-p", "42"]);
|
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.contains_id("port"));
|
|
|
|
assert_eq!(m.get_one::<String>("port").unwrap(), "42");
|
|
|
|
|
2022-09-22 14:39:24 +00:00
|
|
|
let help = cmd.render_help().to_string();
|
2024-06-10 14:15:48 +00:00
|
|
|
assert_data_eq!(
|
|
|
|
help,
|
|
|
|
str![[r#"
|
2024-05-24 15:13:50 +00:00
|
|
|
Usage: test [OPTIONS]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-p [<NUM>]
|
|
|
|
-h, --help Print help
|
|
|
|
|
2024-06-10 14:15:48 +00:00
|
|
|
"#]]
|
|
|
|
);
|
2022-07-28 21:16:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 16:28:24 +00:00
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("pos"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_exact_exact() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("pos"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_exact_less() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(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());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::WrongNumberOfValues);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: 3 values required for '[pos] [pos] [pos]' but 2 were provided
|
|
|
|
|
|
|
|
Usage: myprog [pos] [pos] [pos]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_exact_more() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(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());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::WrongNumberOfValues);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: 3 values required for '[pos] [pos] [pos]' but 4 were provided
|
|
|
|
|
|
|
|
Usage: myprog [pos] [pos] [pos]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_min_exact() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("pos"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_min_less() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(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());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::TooFewValues);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
2024-06-10 14:16:53 +00:00
|
|
|
error: 3 values required by '[pos] [pos] [pos]...'; only 2 were provided
|
2024-06-10 14:15:48 +00:00
|
|
|
|
|
|
|
Usage: myprog [pos] [pos] [pos]...
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_min_more() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("pos"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(1..=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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("pos"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_max_less() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(1..=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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("pos"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["val1", "val2"]
|
|
|
|
);
|
2016-01-13 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn positional_max_more() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(1..=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());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::TooManyValues);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: unexpected value 'val4' for '[pos]...' found; no more were expected
|
|
|
|
|
|
|
|
Usage: myprog [pos]...
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 15:59:42 +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 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();
|
|
|
|
|
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-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_long_space() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 15:59:42 +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 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();
|
|
|
|
|
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-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_short_equals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 15:59:42 +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!["", "-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();
|
|
|
|
|
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-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_short_space() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 15:59:42 +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!["", "-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();
|
|
|
|
|
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-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_short_no_space() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 15:59:42 +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!["", "-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();
|
|
|
|
|
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-01-26 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn sep_positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 15:59:42 +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 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();
|
|
|
|
|
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-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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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();
|
|
|
|
|
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-01-26 15:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn different_sep_positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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();
|
|
|
|
|
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-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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 15:59:42 +00:00
|
|
|
.action(ArgAction::Set),
|
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: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();
|
|
|
|
|
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-01-26 15:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn no_sep_positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 15:59:42 +00:00
|
|
|
.action(ArgAction::Set),
|
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: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();
|
|
|
|
|
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-01-26 15:52:31 +00:00
|
|
|
}
|
2016-09-29 13:55:05 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 19:48:50 +00:00
|
|
|
.num_args(1)
|
|
|
|
.value_delimiter(','),
|
2021-06-16 05:28:25 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2021-06-16 05:28:25 +00:00
|
|
|
.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();
|
|
|
|
|
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<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val1"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("args")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val2", "val3"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_long_with_equal() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-08-03 19:48:50 +00:00
|
|
|
.num_args(1)
|
|
|
|
.value_delimiter(','),
|
2021-06-16 05:28:25 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2021-06-16 05:28:25 +00:00
|
|
|
.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();
|
|
|
|
|
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<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val1"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("args")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val2", "val3"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_short_with_space() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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')
|
2022-08-03 19:48:50 +00:00
|
|
|
.num_args(1)
|
|
|
|
.value_delimiter(','),
|
2021-06-16 05:28:25 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2021-06-16 05:28:25 +00:00
|
|
|
.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();
|
|
|
|
|
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<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val1"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("args")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val2", "val3"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_short_with_no_space() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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')
|
2022-08-03 19:48:50 +00:00
|
|
|
.num_args(1)
|
|
|
|
.value_delimiter(','),
|
2021-06-16 05:28:25 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2021-06-16 05:28:25 +00:00
|
|
|
.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();
|
|
|
|
|
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<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val1"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("args")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val2", "val3"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_short_with_equal() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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')
|
2022-08-03 19:48:50 +00:00
|
|
|
.num_args(1)
|
|
|
|
.value_delimiter(','),
|
2021-06-16 05:28:25 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("args")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2021-06-16 05:28:25 +00:00
|
|
|
.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();
|
|
|
|
|
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<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val1"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("args")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["val2", "val3"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2016-09-29 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 20:15:33 +00:00
|
|
|
fn req_delimiter_complex() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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')
|
2022-08-03 19:48:50 +00:00
|
|
|
.num_args(1)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append)
|
2022-08-03 19:48:50 +00:00
|
|
|
.value_delimiter(','),
|
2021-06-16 05:28:25 +00:00
|
|
|
)
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("args").num_args(1..).index(1))
|
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();
|
|
|
|
|
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<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
[
|
2018-01-25 04:05:05 +00:00
|
|
|
"val2", "val4", "val6", "val8", "val10", "val12", "val13", "val15", "val16", "val18",
|
|
|
|
"val19", "val21", "val22", "val24", "val25",
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("args")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
[
|
2018-01-25 04:05:05 +00:00
|
|
|
"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]
|
2023-05-17 21:33:36 +00:00
|
|
|
#[should_panic = "Positional argument `[target]` *must* have `required(true)` or `last(true)` set \
|
|
|
|
because a prior positional argument (`<files>...`) has `num_args(1..)`"]
|
2016-11-01 20:15:06 +00:00
|
|
|
fn low_index_positional_not_required() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-02-24 15:07:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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]
|
2022-08-04 14:08:43 +00:00
|
|
|
#[should_panic = "Only one positional argument with `.num_args(1..)` \
|
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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-02-24 15:07:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("target")
|
|
|
|
.index(2)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-02-24 15:07:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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 \
|
2022-08-04 14:08:43 +00:00
|
|
|
last positional argument may be set to `.num_args(1..)`"]
|
2016-11-01 20:15:06 +00:00
|
|
|
fn low_index_positional_too_far_back() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-02-24 15:07:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-02-24 15:07:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("files"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["file1", "file2", "file3"]
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("target"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("target").map(|v| v.as_str()).unwrap(),
|
|
|
|
"target"
|
|
|
|
);
|
2016-11-01 20:15:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 03:32:26 +00:00
|
|
|
#[test]
|
|
|
|
fn low_index_positional_in_subcmd() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("lip")
|
2018-01-25 04:05:05 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("test")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-02-24 15:07:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(sm.contains_id("files"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
sm.get_many::<String>("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["file1", "file2", "file3"]
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(sm.contains_id("target"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
sm.get_one::<String>("target").map(|v| v.as_str()).unwrap(),
|
|
|
|
"target"
|
|
|
|
);
|
2016-11-02 03:32:26 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 20:15:06 +00:00
|
|
|
#[test]
|
|
|
|
fn low_index_positional_with_option() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.required(true)
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("target").index(2).required(true))
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(Arg::new("opt").long("option").action(ArgAction::Set))
|
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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("files"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["file1", "file2", "file3"]
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("target"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("target").map(|v| v.as_str()).unwrap(),
|
|
|
|
"target"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()).unwrap(),
|
|
|
|
"test"
|
|
|
|
);
|
2016-11-01 20:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn low_index_positional_with_flag() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("lip")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-02-24 15:07:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("target").index(2).required(true))
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(Arg::new("flg").long("flag").action(ArgAction::SetTrue))
|
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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("files"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["file1", "file2", "file3"]
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("target"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("target").map(|v| v.as_str()).unwrap(),
|
|
|
|
"target"
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flg").expect("defaulted by clap"));
|
2016-11-02 03:32:26 +00:00
|
|
|
}
|
2017-01-01 19:43:07 +00:00
|
|
|
|
2022-07-20 22:44:49 +00:00
|
|
|
#[test]
|
|
|
|
fn low_index_positional_with_extra_flags() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.arg(Arg::new("yes").long("yes").action(ArgAction::SetTrue))
|
|
|
|
.arg(Arg::new("one").long("one").action(ArgAction::Set))
|
|
|
|
.arg(Arg::new("two").long("two").action(ArgAction::Set))
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("input").num_args(1..).required(true))
|
2022-07-20 22:44:49 +00:00
|
|
|
.arg(Arg::new("output").required(true));
|
|
|
|
let m = cmd.try_get_matches_from([
|
|
|
|
"test", "--one", "1", "--two", "2", "3", "4", "5", "6", "7", "8",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("input")
|
|
|
|
.unwrap()
|
|
|
|
.map(String::from)
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec![
|
|
|
|
"3".to_owned(),
|
|
|
|
"4".to_owned(),
|
|
|
|
"5".to_owned(),
|
|
|
|
"6".to_owned(),
|
|
|
|
"7".to_owned()
|
|
|
|
],
|
|
|
|
);
|
|
|
|
assert_eq!(m.get_one::<String>("output").unwrap(), "8");
|
|
|
|
assert_eq!(m.get_one::<String>("one").unwrap(), "1");
|
|
|
|
assert_eq!(m.get_one::<String>("two").unwrap(), "2");
|
2023-01-23 15:23:38 +00:00
|
|
|
assert!(!m.get_flag("yes"));
|
2022-07-20 22:44:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 19:43:07 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_value_terminator_option() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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(";")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("other"));
|
|
|
|
assert!(m.contains_id("files"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["val1", "val2"]
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("other").map(|v| v.as_str()),
|
|
|
|
Some("otherval")
|
|
|
|
);
|
2017-01-01 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_value_terminator_option_other_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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(";")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("other"))
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(Arg::new("flag").short('F').action(ArgAction::SetTrue))
|
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();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("other"));
|
|
|
|
assert!(m.contains_id("files"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-01-25 04:05:05 +00:00
|
|
|
["val1", "val2"]
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("other").map(|v| v.as_str()),
|
|
|
|
Some("otherval")
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2017-01-01 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 21:05:38 +00:00
|
|
|
#[test]
|
|
|
|
fn all_multiple_value_terminator() {
|
|
|
|
let m = Command::new("lip")
|
|
|
|
.arg(
|
|
|
|
Arg::new("files")
|
|
|
|
.value_terminator(";")
|
|
|
|
.action(ArgAction::Set)
|
|
|
|
.num_args(0..),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("other").num_args(0..))
|
|
|
|
.try_get_matches_from(vec!["test", "value", ";"]);
|
|
|
|
|
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.contains_id("files"));
|
2023-07-21 21:41:08 +00:00
|
|
|
assert!(!m.contains_id("other"));
|
2023-07-21 21:05:38 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
["value".to_owned()],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:34:25 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_vals_with_hyphen() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("do")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cmds")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
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();
|
2022-05-24 15:16:50 +00:00
|
|
|
let cmds: Vec<_> = m
|
|
|
|
.get_many::<String>("cmds")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
2017-01-02 19:34:25 +00:00
|
|
|
assert_eq!(&cmds, &["find", "-type", "f", "-name", "special"]);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("location").map(|v| v.as_str()),
|
|
|
|
Some("/home/clap")
|
|
|
|
);
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
2019-05-28 08:17:21 +00:00
|
|
|
|
2023-05-19 15:36:43 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_positional_multiple_values() {
|
2023-05-19 15:39:05 +00:00
|
|
|
let res = Command::new("do")
|
2023-05-19 15:36:43 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("cmd1")
|
|
|
|
.action(ArgAction::Set)
|
|
|
|
.num_args(1..)
|
|
|
|
.allow_hyphen_values(true)
|
|
|
|
.value_terminator(";"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("cmd2")
|
|
|
|
.action(ArgAction::Set)
|
|
|
|
.num_args(1..)
|
|
|
|
.allow_hyphen_values(true)
|
|
|
|
.value_terminator(";"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"do",
|
|
|
|
"find",
|
|
|
|
"-type",
|
|
|
|
"f",
|
|
|
|
"-name",
|
|
|
|
"special",
|
|
|
|
";",
|
|
|
|
"/home/clap",
|
|
|
|
"foo",
|
|
|
|
]);
|
2023-05-19 15:39:05 +00:00
|
|
|
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
|
|
|
|
|
|
|
|
let m = res.unwrap();
|
|
|
|
let cmd1: Vec<_> = m
|
|
|
|
.get_many::<String>("cmd1")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
2023-05-19 15:41:44 +00:00
|
|
|
assert_eq!(&cmd1, &["find", "-type", "f", "-name", "special"]);
|
2023-05-19 15:39:05 +00:00
|
|
|
let cmd2: Vec<_> = m
|
|
|
|
.get_many::<String>("cmd2")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
2023-05-19 15:41:44 +00:00
|
|
|
assert_eq!(&cmd2, &["/home/clap", "foo"]);
|
2023-05-19 15:36:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 16:04:48 +00:00
|
|
|
#[test]
|
|
|
|
fn value_terminator_has_higher_precedence_than_allow_hyphen_values() {
|
|
|
|
let res = Command::new("do")
|
|
|
|
.arg(
|
|
|
|
Arg::new("cmd1")
|
|
|
|
.action(ArgAction::Set)
|
|
|
|
.num_args(1..)
|
|
|
|
.allow_hyphen_values(true)
|
|
|
|
.value_terminator("--foo"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("cmd2")
|
|
|
|
.action(ArgAction::Set)
|
|
|
|
.num_args(1..)
|
|
|
|
.allow_hyphen_values(true)
|
|
|
|
.value_terminator(";"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"do",
|
|
|
|
"find",
|
|
|
|
"-type",
|
|
|
|
"f",
|
|
|
|
"-name",
|
|
|
|
"special",
|
|
|
|
"--foo",
|
|
|
|
"/home/clap",
|
|
|
|
"foo",
|
|
|
|
]);
|
|
|
|
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
|
|
|
|
|
|
|
|
let m = res.unwrap();
|
|
|
|
let cmd1: Vec<_> = m
|
|
|
|
.get_many::<String>("cmd1")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
|
|
|
assert_eq!(&cmd1, &["find", "-type", "f", "-name", "special"]);
|
|
|
|
let cmd2: Vec<_> = m
|
|
|
|
.get_many::<String>("cmd2")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
|
|
|
assert_eq!(&cmd2, &["/home/clap", "foo"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn value_terminator_restores_escaping_disabled_by_allow_hyphen_values() {
|
|
|
|
let res = Command::new("do")
|
|
|
|
.arg(
|
|
|
|
Arg::new("cmd1")
|
|
|
|
.action(ArgAction::Set)
|
|
|
|
.num_args(1..)
|
|
|
|
.allow_hyphen_values(true)
|
|
|
|
.value_terminator("--"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("cmd2")
|
|
|
|
.action(ArgAction::Set)
|
|
|
|
.num_args(1..)
|
|
|
|
.allow_hyphen_values(true)
|
|
|
|
.value_terminator(";"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"do",
|
|
|
|
"find",
|
|
|
|
"-type",
|
|
|
|
"f",
|
|
|
|
"-name",
|
|
|
|
"special",
|
|
|
|
"--",
|
|
|
|
"/home/clap",
|
|
|
|
"foo",
|
|
|
|
]);
|
|
|
|
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
|
|
|
|
|
|
|
|
let m = res.unwrap();
|
|
|
|
let cmd1: Vec<_> = m
|
|
|
|
.get_many::<String>("cmd1")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
|
|
|
assert_eq!(&cmd1, &["find", "-type", "f", "-name", "special"]);
|
|
|
|
let cmd2: Vec<_> = m
|
|
|
|
.get_many::<String>("cmd2")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
|
|
|
assert_eq!(&cmd2, &["/home/clap", "foo"]);
|
|
|
|
}
|
|
|
|
|
2019-05-28 08:17:21 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1480_max_values_consumes_extra_arg_1() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("prog")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("field").num_args(..=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() {
|
2024-06-10 14:15:48 +00:00
|
|
|
let m = Command::new("prog")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("field").num_args(..=1).long("field"))
|
2019-05-28 08:17:21 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--field", "1", "2"]);
|
|
|
|
|
2024-06-10 14:15:48 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: unexpected argument '2' found
|
|
|
|
|
|
|
|
Usage: prog [OPTIONS]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2019-05-28 08:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1480_max_values_consumes_extra_arg_3() {
|
2024-06-10 14:15:48 +00:00
|
|
|
let m = Command::new("prog")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("field").num_args(..=1).long("field"))
|
2019-05-28 08:17:21 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--field", "1", "2", "3"]);
|
|
|
|
|
2024-06-10 14:15:48 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: unexpected argument '2' found
|
|
|
|
|
|
|
|
Usage: prog [OPTIONS]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2019-05-28 08:17:21 +00:00
|
|
|
}
|
2021-02-16 03:58:58 +00:00
|
|
|
|
2021-08-13 23:22:38 +00:00
|
|
|
#[test]
|
|
|
|
fn value_names_building_num_vals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("test")
|
2021-08-13 23:22:38 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("pos")
|
|
|
|
.long("pos")
|
2022-08-15 18:21:05 +00:00
|
|
|
.value_names(["who", "what", "why"]),
|
2021-08-13 23:22:38 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog", "--pos", "val1", "val2", "val3"]);
|
|
|
|
|
2022-07-26 21:54:14 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2021-08-13 23:22:38 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-08-13 23:22:38 +00:00
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn value_names_building_num_vals_for_positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("test")
|
2022-08-15 18:21:05 +00:00
|
|
|
.arg(Arg::new("pos").value_names(["who", "what", "why"]))
|
2021-08-13 23:22:38 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
|
|
|
|
|
2022-07-26 21:54:14 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2021-08-13 23:22:38 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-08-13 23:22:38 +00:00
|
|
|
["val1", "val2", "val3"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-08-03 16:20:07 +00:00
|
|
|
fn num_args_preferred_over_value_names() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("test")
|
2021-08-13 23:22:38 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("pos")
|
|
|
|
.long("pos")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(4)
|
2022-08-15 18:21:05 +00:00
|
|
|
.value_names(["who", "what", "why"]),
|
2021-08-13 23:22:38 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog", "--pos", "val1", "val2", "val3", "val4"]);
|
|
|
|
|
2022-07-26 21:54:14 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2021-08-13 23:22:38 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-08-13 23:22:38 +00:00
|
|
|
["val1", "val2", "val3", "val4"]
|
|
|
|
);
|
|
|
|
}
|
2021-10-04 17:09:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn values_per_occurrence_named() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let mut a = Command::new("test").arg(
|
2021-10-04 17:09:54 +00:00
|
|
|
Arg::new("pos")
|
|
|
|
.long("pos")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(2)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
2021-10-04 17:09:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let m = a.try_get_matches_from_mut(vec!["myprog", "--pos", "val1", "val2"]);
|
2022-07-26 21:54:14 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
let m = m.unwrap();
|
2021-10-04 17:09:54 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-10-04 17:09:54 +00:00
|
|
|
["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,
|
2023-05-04 19:53:36 +00:00
|
|
|
Err(err) => panic!("{err}"),
|
2021-10-04 17:09:54 +00:00
|
|
|
};
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-10-04 17:09:54 +00:00
|
|
|
["val1", "val2", "val3", "val4"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn values_per_occurrence_positional() {
|
2022-08-03 16:20:07 +00:00
|
|
|
let mut a = Command::new("test").arg(Arg::new("pos").num_args(2).action(ArgAction::Append));
|
2021-10-04 17:09:54 +00:00
|
|
|
|
|
|
|
let m = a.try_get_matches_from_mut(vec!["myprog", "val1", "val2"]);
|
2022-07-26 21:54:14 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
let m = m.unwrap();
|
2021-10-04 17:09:54 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("pos")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-10-04 17:09:54 +00:00
|
|
|
["val1", "val2"]
|
|
|
|
);
|
|
|
|
}
|
2022-07-26 21:54:14 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_2229() {
|
|
|
|
let m = Command::new("multiple_values")
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(Arg::new("pos").help("multiple positionals").num_args(3))
|
2022-07-26 21:54:14 +00:00
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"myprog", "val1", "val2", "val3", "val4", "val5", "val6",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
2024-06-10 14:15:48 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::WrongNumberOfValues);
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
assert_data_eq!(err.to_string(), str![[r#"
|
|
|
|
error: 3 values required for '[pos] [pos] [pos]' but 6 were provided
|
|
|
|
|
|
|
|
Usage: myprog [pos] [pos] [pos]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
|
|
|
|
"#]]);
|
2022-07-26 21:54:14 +00:00
|
|
|
}
|
2022-11-10 04:10:32 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-09-25 15:18:37 +00:00
|
|
|
#[should_panic = "Argument 'pos` is positional and it must take a value but action is SetTrue"]
|
2022-11-10 04:10:32 +00:00
|
|
|
fn disallow_positionals_without_values() {
|
|
|
|
let cmd = Command::new("test").arg(Arg::new("pos").num_args(0));
|
|
|
|
cmd.debug_assert();
|
|
|
|
}
|