2022-02-12 03:48:29 +00:00
|
|
|
use clap::{arg, error::ErrorKind, Arg, 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")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(arg!(--multflag "allowed multiple flag").multiple_occurrences(true))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag "disallowed multiple flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--multflag", "--flag", "--multflag"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("multflag"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag"), 2);
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("mo_flags_short")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(arg!(-m --multflag "allowed multiple flag").multiple_occurrences(true))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "disallowed multiple flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-m", "-f", "-m"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("multflag"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag"), 2);
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_mixed() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("mo_flags_mixed")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(arg!(-m --multflag1 "allowed multiple flag").multiple_occurrences(true))
|
|
|
|
.arg(arg!(-n --multflag2 "another allowed multiple flag").multiple_occurrences(true))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "disallowed multiple flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
"",
|
|
|
|
"-m",
|
|
|
|
"-f",
|
|
|
|
"-n",
|
|
|
|
"--multflag1",
|
|
|
|
"-m",
|
|
|
|
"--multflag2",
|
2021-12-27 18:56:12 +00:00
|
|
|
])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("multflag1"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag1"), 3);
|
|
|
|
assert!(m.is_present("multflag2"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag2"), 2);
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 1);
|
2016-02-02 17:57:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 18:42:01 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_positional() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test").arg(Arg::new("multi").multiple_occurrences(true));
|
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()
|
|
|
|
.try_get_matches_from(&["test"])
|
|
|
|
.expect("zero occurrences work");
|
|
|
|
assert!(!m.is_present("multi"));
|
|
|
|
assert_eq!(m.occurrences_of("multi"), 0);
|
|
|
|
assert!(m.values_of("multi").is_none());
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-10-30 18:42:01 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(&["test", "one"])
|
|
|
|
.expect("single occurrence work");
|
|
|
|
assert!(m.is_present("multi"));
|
|
|
|
assert_eq!(m.occurrences_of("multi"), 1);
|
|
|
|
assert_eq!(m.values_of("multi").unwrap().collect::<Vec<_>>(), ["one"]);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-10-30 18:42:01 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(&["test", "one", "two", "three", "four"])
|
|
|
|
.expect("many occurrences work");
|
|
|
|
assert!(m.is_present("multi"));
|
|
|
|
assert_eq!(m.occurrences_of("multi"), 4);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("multi").unwrap().collect::<Vec<_>>(),
|
|
|
|
["one", "two", "three", "four"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:57:02 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_occurrences_of_flags_large_quantity() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let args: Vec<&str> = vec![""]
|
|
|
|
.into_iter()
|
|
|
|
.chain(vec!["-m"; 1024].into_iter())
|
|
|
|
.collect();
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("mo_flags_large_qty")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(arg!(-m --multflag "allowed multiple flag").multiple_occurrences(true))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(args)
|
|
|
|
.unwrap();
|
2016-02-02 17:57:02 +00:00
|
|
|
assert!(m.is_present("multflag"));
|
|
|
|
assert_eq!(m.occurrences_of("multflag"), 1024);
|
|
|
|
}
|
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")
|
|
|
|
.takes_value(false)
|
|
|
|
.multiple_occurrences(true),
|
|
|
|
);
|
|
|
|
|
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());
|
2019-12-22 17:33:23 +00:00
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 0);
|
|
|
|
|
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());
|
2019-12-22 17:33:23 +00:00
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 1);
|
|
|
|
|
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());
|
2019-12-22 17:33:23 +00:00
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
|
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());
|
2019-12-22 17:33:23 +00:00
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
.takes_value(false)
|
|
|
|
.multiple_occurrences(true)
|
|
|
|
.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());
|
2019-12-22 17:33:23 +00:00
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 0);
|
|
|
|
|
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());
|
2019-12-22 17:33:23 +00:00
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 1);
|
|
|
|
|
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());
|
2019-12-22 17:33:23 +00:00
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
|
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());
|
2019-12-22 17:33:23 +00:00
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
|
|
|
|
}
|
2021-06-17 18:54:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn max_occurrences_implies_multiple_occurrences() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("prog").arg(
|
2021-06-17 18:54:24 +00:00
|
|
|
Arg::new("verbose")
|
|
|
|
.short('v')
|
|
|
|
.long("verbose")
|
|
|
|
.max_occurrences(3),
|
|
|
|
);
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.try_get_matches_from(vec!["prog", "-vvv"]);
|
2021-06-17 18:54:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
|
|
|
|
|
|
|
|
// One max should not imply multiple occurrences
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("prog").arg(
|
2021-06-17 18:54:24 +00:00
|
|
|
Arg::new("verbose")
|
|
|
|
.short('v')
|
|
|
|
.long("verbose")
|
|
|
|
.max_occurrences(1),
|
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.try_get_matches_from(vec!["prog", "-vvv"]);
|
2021-06-17 18:54:24 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnexpectedMultipleUsage);
|
2021-06-17 18:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn max_occurrences_try_inputs() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("prog").arg(
|
2021-06-17 18:54:24 +00:00
|
|
|
Arg::new("verbose")
|
|
|
|
.short('v')
|
|
|
|
.long("verbose")
|
|
|
|
.max_occurrences(3),
|
|
|
|
);
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["prog", "-v"]);
|
2021-06-17 18:54:24 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 1);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["prog", "-vv"]);
|
2021-06-17 18:54:24 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["prog", "-vvv"]);
|
2021-06-17 18:54:24 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["prog", "-vvvv"]);
|
2021-06-17 18:54:24 +00:00
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::TooManyOccurrences);
|
2021-06-17 18:54:24 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-06-17 18:54:24 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["prog", "-v", "-v", "-v"]);
|
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-06-17 18:54:24 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["prog", "-v", "-vv", "-v"]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::TooManyOccurrences);
|
2021-06-17 18:54:24 +00:00
|
|
|
}
|
2021-10-30 18:42:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn max_occurrences_positional() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("prog").arg(Arg::new("verbose").max_occurrences(3));
|
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["prog", "v"]);
|
2021-10-30 18:42:01 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 1);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(vec!["prog", "v", "v"]);
|
2021-10-30 18:42:01 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-10-30 18:42:01 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["prog", "v", "v", "v"]);
|
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
|
|
|
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-10-30 18:42:01 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["prog", "v", "v", "v", "v"]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::TooManyOccurrences);
|
2021-10-30 18:42:01 +00:00
|
|
|
}
|