test(parser): Show conditional requireds and exclusive

This commit is contained in:
Ed Page 2024-06-06 14:56:40 -05:00
parent 5e3386bb40
commit 65e90cd31f

View file

@ -684,6 +684,126 @@ fn exclusive_with_required() {
cmd.clone().try_get_matches_from(["bug", "--test"]).unwrap(); cmd.clone().try_get_matches_from(["bug", "--test"]).unwrap();
} }
#[test]
fn exclusive_with_required_unless_present() {
let cmd = Command::new("bug")
.arg(
Arg::new("exclusive")
.long("exclusive")
.action(ArgAction::SetTrue)
.exclusive(true),
)
.arg(
Arg::new("required")
.long("required")
.action(ArgAction::SetTrue)
.required_unless_present("alternative"),
)
.arg(
Arg::new("alternative")
.long("alternative")
.action(ArgAction::SetTrue),
);
cmd.clone()
.try_get_matches_from(["bug", "--required"])
.unwrap();
cmd.clone()
.try_get_matches_from(["bug", "--alternative"])
.unwrap();
cmd.clone().try_get_matches_from(["bug"]).unwrap_err();
cmd.clone()
.try_get_matches_from(["bug", "--exclusive", "--required"])
.unwrap_err();
cmd.clone()
.try_get_matches_from(["bug", "--exclusive"])
.unwrap_err();
}
#[test]
fn exclusive_with_required_unless_present_any() {
let cmd = Command::new("bug")
.arg(
Arg::new("exclusive")
.long("exclusive")
.action(ArgAction::SetTrue)
.exclusive(true),
)
.arg(
Arg::new("required")
.long("required")
.action(ArgAction::SetTrue)
.required_unless_present_any(["alternative"]),
)
.arg(
Arg::new("alternative")
.long("alternative")
.action(ArgAction::SetTrue),
);
cmd.clone()
.try_get_matches_from(["bug", "--required"])
.unwrap();
cmd.clone()
.try_get_matches_from(["bug", "--alternative"])
.unwrap();
cmd.clone().try_get_matches_from(["bug"]).unwrap_err();
cmd.clone()
.try_get_matches_from(["bug", "--exclusive", "--required"])
.unwrap_err();
cmd.clone()
.try_get_matches_from(["bug", "--exclusive"])
.unwrap_err();
}
#[test]
fn exclusive_with_required_unless_present_all() {
let cmd = Command::new("bug")
.arg(
Arg::new("exclusive")
.long("exclusive")
.action(ArgAction::SetTrue)
.exclusive(true),
)
.arg(
Arg::new("required")
.long("required")
.action(ArgAction::SetTrue)
.required_unless_present_all(["alternative"]),
)
.arg(
Arg::new("alternative")
.long("alternative")
.action(ArgAction::SetTrue),
);
cmd.clone()
.try_get_matches_from(["bug", "--required"])
.unwrap();
cmd.clone()
.try_get_matches_from(["bug", "--alternative"])
.unwrap();
cmd.clone().try_get_matches_from(["bug"]).unwrap_err();
cmd.clone()
.try_get_matches_from(["bug", "--exclusive", "--required"])
.unwrap_err();
cmd.clone()
.try_get_matches_from(["bug", "--exclusive"])
.unwrap_err();
}
#[test] #[test]
#[cfg(feature = "error-context")] #[cfg(feature = "error-context")]
fn option_conflicts_with_subcommand() { fn option_conflicts_with_subcommand() {