2022-07-25 19:17:01 -05:00
|
|
|
use clap::{error::ErrorKind, Arg, ArgAction, Command};
|
2021-09-16 14:21:43 -04:00
|
|
|
|
2022-08-31 09:29:00 -05:00
|
|
|
static HELP: &str = "\
|
2022-09-07 11:03:55 -05:00
|
|
|
Usage: prog [OPTIONS]
|
2021-09-16 14:21:43 -04:00
|
|
|
|
2022-08-26 09:40:23 -05:00
|
|
|
Options:
|
2022-09-07 15:29:15 -05:00
|
|
|
-a
|
|
|
|
-b
|
|
|
|
-c
|
|
|
|
-h, --help Print help information
|
2021-09-16 14:21:43 -04:00
|
|
|
";
|
|
|
|
|
2022-08-15 14:29:46 -05:00
|
|
|
fn cmd() -> Command {
|
2022-02-11 21:48:29 -06:00
|
|
|
Command::new("prog")
|
2021-09-16 14:21:43 -04:00
|
|
|
.arg(
|
|
|
|
Arg::new("a")
|
|
|
|
.short('a')
|
2022-07-25 19:17:01 -05:00
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-12 12:40:58 -05:00
|
|
|
.required_unless_present_any(["b", "c"])
|
|
|
|
.conflicts_with_all(["b", "c"]),
|
2021-09-16 14:21:43 -04:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("b")
|
|
|
|
.short('b')
|
2022-07-25 19:17:01 -05:00
|
|
|
.action(ArgAction::SetTrue)
|
2021-09-16 14:21:43 -04:00
|
|
|
.required_unless_present("a")
|
|
|
|
.requires("c"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("c")
|
|
|
|
.short('c')
|
2022-07-25 19:17:01 -05:00
|
|
|
.action(ArgAction::SetTrue)
|
2021-09-16 14:21:43 -04:00
|
|
|
.required_unless_present("a")
|
|
|
|
.requires("b"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn valid_cases() {
|
2022-02-14 15:47:20 -06:00
|
|
|
let res = cmd().try_get_matches_from(vec!["", "-a"]);
|
2021-12-27 13:57:38 -06:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2022-02-14 15:47:20 -06:00
|
|
|
let res = cmd().clone().try_get_matches_from(vec!["", "-b", "-c"]);
|
2021-12-27 13:57:38 -06:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2022-02-14 15:47:20 -06:00
|
|
|
let res = cmd().try_get_matches_from(vec!["", "-c", "-b"]);
|
2021-12-27 13:57:38 -06:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2021-09-16 14:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_text() {
|
2022-02-14 15:47:20 -06:00
|
|
|
let res = cmd().try_get_matches_from(vec!["prog", "--help"]);
|
2021-09-16 14:21:43 -04:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2022-01-25 16:19:28 -06:00
|
|
|
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
|
2022-01-04 07:22:40 +00:00
|
|
|
println!("{}", err);
|
2021-09-16 14:21:43 -04:00
|
|
|
assert_eq!(err.to_string(), HELP);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 09:59:04 -05:00
|
|
|
#[cfg(feature = "error-context")]
|
2021-09-16 14:21:43 -04:00
|
|
|
fn no_duplicate_error() {
|
2022-09-19 09:59:04 -05:00
|
|
|
static ONLY_B_ERROR: &str = "\
|
|
|
|
error: The following required arguments were not provided:
|
|
|
|
-c
|
|
|
|
|
|
|
|
Usage: prog -b -c
|
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let res = cmd().try_get_matches_from(vec!["", "-b"]);
|
2021-09-16 14:21:43 -04:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2022-01-25 16:19:28 -06:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2021-09-16 14:21:43 -04:00
|
|
|
assert_eq!(err.to_string(), ONLY_B_ERROR);
|
|
|
|
|
2022-09-19 09:59:04 -05:00
|
|
|
static ONLY_C_ERROR: &str = "\
|
|
|
|
error: The following required arguments were not provided:
|
|
|
|
-b
|
|
|
|
|
|
|
|
Usage: prog -c -b
|
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let res = cmd().try_get_matches_from(vec!["", "-c"]);
|
2021-09-16 14:21:43 -04:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2022-01-25 16:19:28 -06:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2021-09-16 14:21:43 -04:00
|
|
|
assert_eq!(err.to_string(), ONLY_C_ERROR);
|
|
|
|
}
|