clap/tests/builder/double_require.rs

91 lines
2.3 KiB
Rust
Raw Normal View History

use clap::{error::ErrorKind, Arg, ArgAction, Command};
static HELP: &str = "\
Usage: prog [OPTIONS]
Options:
-a
-b
-c
-h, --help Print help
";
fn cmd() -> Command {
2022-02-12 03:48:29 +00:00
Command::new("prog")
.arg(
Arg::new("a")
.short('a')
.action(ArgAction::SetTrue)
.required_unless_present_any(["b", "c"])
.conflicts_with_all(["b", "c"]),
)
.arg(
Arg::new("b")
.short('b')
.action(ArgAction::SetTrue)
.required_unless_present("a")
.requires("c"),
)
.arg(
Arg::new("c")
.short('c')
.action(ArgAction::SetTrue)
.required_unless_present("a")
.requires("b"),
)
}
#[test]
fn valid_cases() {
2022-02-14 21:47:20 +00:00
let res = cmd().try_get_matches_from(vec!["", "-a"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
2022-02-14 21:47:20 +00:00
let res = cmd().clone().try_get_matches_from(vec!["", "-b", "-c"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
2022-02-14 21:47:20 +00:00
let res = cmd().try_get_matches_from(vec!["", "-c", "-b"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
}
#[test]
fn help_text() {
2022-02-14 21:47:20 +00:00
let res = cmd().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);
println!("{err}");
assert_eq!(err.to_string(), HELP);
}
#[test]
#[cfg(feature = "error-context")]
fn no_duplicate_error() {
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 21:47:20 +00:00
let res = cmd().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);
assert_eq!(err.to_string(), ONLY_B_ERROR);
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 21:47:20 +00:00
let res = cmd().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);
assert_eq!(err.to_string(), ONLY_C_ERROR);
}