2022-06-07 18:48:48 +00:00
|
|
|
use clap::{arg, Arg, ArgAction, Command};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("mo_flags_long")
|
2022-09-23 20:18:14 +00:00
|
|
|
.args_override_self(true)
|
2022-06-07 18:48:48 +00:00
|
|
|
.arg(arg!(--multflag "allowed multiple flag").action(ArgAction::SetTrue))
|
|
|
|
.arg(arg!(--flag "disallowed multiple flag").action(ArgAction::SetTrue))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--multflag", "--flag", "--multflag"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("multflag"));
|
2022-06-07 18:48:48 +00:00
|
|
|
assert_eq!(m.get_one::<bool>("multflag").copied(), Some(true));
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2022-06-07 18:48:48 +00:00
|
|
|
assert_eq!(m.get_one::<bool>("flag").copied(), Some(true));
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("mo_flags_short")
|
2022-09-23 20:18:14 +00:00
|
|
|
.args_override_self(true)
|
2022-06-07 18:48:48 +00:00
|
|
|
.arg(arg!(-m --multflag "allowed multiple flag").action(ArgAction::SetTrue))
|
|
|
|
.arg(arg!(-f --flag "disallowed multiple flag").action(ArgAction::SetTrue))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-m", "-f", "-m"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("multflag"));
|
2022-06-07 18:48:48 +00:00
|
|
|
assert_eq!(m.get_one::<bool>("multflag").copied(), Some(true));
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2022-06-07 18:48:48 +00:00
|
|
|
assert_eq!(m.get_one::<bool>("flag").copied(), Some(true));
|
2016-02-02 17:57:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 18:42:01 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_positional() {
|
2022-08-03 16:20:07 +00:00
|
|
|
let cmd = Command::new("test").arg(Arg::new("multi").num_args(1..).action(ArgAction::Append));
|
2021-10-30 18:42:01 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-10-30 18:42:01 +00:00
|
|
|
.clone()
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test"])
|
2021-10-30 18:42:01 +00:00
|
|
|
.expect("zero occurrences work");
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!m.contains_id("multi"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert!(m.get_many::<String>("multi").is_none());
|
2021-10-30 18:42:01 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-10-30 18:42:01 +00:00
|
|
|
.clone()
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "one"])
|
2021-10-30 18:42:01 +00:00
|
|
|
.expect("single occurrence work");
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("multi"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("multi")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
["one"]
|
|
|
|
);
|
2021-10-30 18:42:01 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-10-30 18:42:01 +00:00
|
|
|
.clone()
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "one", "two", "three", "four"])
|
2021-10-30 18:42:01 +00:00
|
|
|
.expect("many occurrences work");
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("multi"));
|
2021-10-30 18:42:01 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("multi")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-10-30 18:42:01 +00:00
|
|
|
["one", "two", "three", "four"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:57:02 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_large_quantity() {
|
2022-06-09 16:09:36 +00:00
|
|
|
let cmd = Command::new("mo_flags_large_qty")
|
|
|
|
.arg(arg!(-m --multflag "allowed multiple flag").action(ArgAction::Count));
|
|
|
|
|
2023-10-12 13:32:59 +00:00
|
|
|
let args: Vec<&str> = vec![""].into_iter().chain(vec!["-m"; 200]).collect();
|
2022-06-09 16:09:36 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(args).unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("multflag"));
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("multflag").copied(), Some(200));
|
|
|
|
|
2023-10-12 13:32:59 +00:00
|
|
|
let args: Vec<&str> = vec![""].into_iter().chain(vec!["-m"; 500]).collect();
|
2022-06-09 16:09:36 +00:00
|
|
|
let m = cmd.try_get_matches_from(args).unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("multflag"));
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("multflag").copied(), Some(u8::MAX));
|
2016-02-02 17:57:02 +00:00
|
|
|
}
|
2019-12-22 17:33:23 +00:00
|
|
|
|
2021-08-14 00:04:49 +00:00
|
|
|
#[cfg(feature = "env")]
|
2019-12-22 17:33:23 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_before_env() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("mo_before_env").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("verbose")
|
2019-12-22 17:33:23 +00:00
|
|
|
.env("VERBOSE")
|
|
|
|
.short('v')
|
|
|
|
.long("verbose")
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Count),
|
2019-12-22 17:33:23 +00:00
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec![""]);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = m.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("verbose").copied(), Some(0));
|
2019-12-22 17:33:23 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["", "-v"]);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = m.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("verbose").copied(), Some(1));
|
2019-12-22 17:33:23 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["", "-vv"]);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = m.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("verbose").copied(), Some(2));
|
2022-06-07 18:48:48 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["", "-vvv"]);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = m.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("verbose").copied(), Some(3));
|
2019-12-22 17:33:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 00:04:49 +00:00
|
|
|
#[cfg(feature = "env")]
|
2019-12-22 17:33:23 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_after_env() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("mo_after_env").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("verbose")
|
2019-12-22 17:33:23 +00:00
|
|
|
.short('v')
|
|
|
|
.long("verbose")
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Count)
|
2019-12-22 17:33:23 +00:00
|
|
|
.env("VERBOSE"),
|
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec![""]);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = m.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("verbose").copied(), Some(0));
|
2019-12-22 17:33:23 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["", "-v"]);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = m.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("verbose").copied(), Some(1));
|
2019-12-22 17:33:23 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["", "-vv"]);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = m.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("verbose").copied(), Some(2));
|
2021-06-17 18:54:24 +00:00
|
|
|
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["", "-vvv"]);
|
2021-10-30 18:42:01 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-06-07 18:48:48 +00:00
|
|
|
let m = m.unwrap();
|
2022-06-09 16:09:36 +00:00
|
|
|
assert_eq!(m.get_one::<u8>("verbose").copied(), Some(3));
|
2021-10-30 18:42:01 +00:00
|
|
|
}
|