2022-02-12 03:48:29 +00:00
|
|
|
use clap::{error::ErrorKind, Arg, Command};
|
2021-09-16 18:21:43 +00:00
|
|
|
|
|
|
|
static HELP: &str = "prog
|
|
|
|
|
|
|
|
USAGE:
|
2021-10-11 22:01:33 +00:00
|
|
|
prog [OPTIONS]
|
2021-09-16 18:21:43 +00:00
|
|
|
|
2021-10-11 22:01:33 +00:00
|
|
|
OPTIONS:
|
2021-10-08 17:05:40 +00:00
|
|
|
-a
|
|
|
|
-b
|
|
|
|
-c
|
|
|
|
-h, --help Print help information
|
2021-09-16 18:21:43 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
static ONLY_B_ERROR: &str = "error: The following required arguments were not provided:
|
|
|
|
-c
|
|
|
|
|
|
|
|
USAGE:
|
2022-02-02 01:04:53 +00:00
|
|
|
prog [OPTIONS] -b -c
|
2021-09-16 18:21:43 +00:00
|
|
|
|
|
|
|
For more information try --help
|
|
|
|
";
|
|
|
|
|
|
|
|
static ONLY_C_ERROR: &str = "error: The following required arguments were not provided:
|
|
|
|
-b
|
|
|
|
|
|
|
|
USAGE:
|
2022-02-02 01:04:53 +00:00
|
|
|
prog [OPTIONS] -c -b
|
2021-09-16 18:21:43 +00:00
|
|
|
|
|
|
|
For more information try --help
|
|
|
|
";
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
fn app() -> Command<'static> {
|
|
|
|
Command::new("prog")
|
2021-09-16 18:21:43 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("a")
|
|
|
|
.short('a')
|
|
|
|
.required_unless_present_any(&["b", "c"])
|
|
|
|
.conflicts_with_all(&["b", "c"]),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("b")
|
|
|
|
.short('b')
|
|
|
|
.required_unless_present("a")
|
|
|
|
.requires("c"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("c")
|
|
|
|
.short('c')
|
|
|
|
.required_unless_present("a")
|
|
|
|
.requires("b"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn valid_cases() {
|
|
|
|
let res = app().try_get_matches_from(vec!["", "-a"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2021-09-16 18:21:43 +00:00
|
|
|
let res = app().clone().try_get_matches_from(vec!["", "-b", "-c"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2021-09-16 18:21:43 +00:00
|
|
|
let res = app().try_get_matches_from(vec!["", "-c", "-b"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2021-09-16 18:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_text() {
|
|
|
|
let res = app().try_get_matches_from(vec!["prog", "--help"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
|
2022-01-04 07:22:40 +00:00
|
|
|
println!("{}", err);
|
2021-09-16 18:21:43 +00:00
|
|
|
assert_eq!(err.to_string(), HELP);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_duplicate_error() {
|
|
|
|
let res = app().try_get_matches_from(vec!["", "-b"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2021-09-16 18:21:43 +00:00
|
|
|
assert_eq!(err.to_string(), ONLY_B_ERROR);
|
|
|
|
|
|
|
|
let res = app().try_get_matches_from(vec!["", "-c"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2021-09-16 18:21:43 +00:00
|
|
|
assert_eq!(err.to_string(), ONLY_C_ERROR);
|
|
|
|
}
|