mirror of
https://github.com/clap-rs/clap
synced 2024-12-12 13:52:34 +00:00
test(parser): Verify defaulting on errors
This commit is contained in:
parent
3f5c05ce38
commit
76d0049330
1 changed files with 31 additions and 12 deletions
|
@ -4,28 +4,38 @@ use super::utils;
|
|||
|
||||
#[test]
|
||||
fn single_short_arg_without_value() {
|
||||
let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
|
||||
let cmd = Command::new("cmd")
|
||||
.ignore_errors(true)
|
||||
.arg(arg!(
|
||||
-c --config <FILE> "Sets a custom config file"
|
||||
));
|
||||
))
|
||||
.arg(arg!(--"unset-flag"));
|
||||
|
||||
let r = cmd.try_get_matches_from(vec!["cmd", "-c" /* missing: , "config file" */]);
|
||||
|
||||
assert!(r.is_ok(), "unexpected error: {r:?}");
|
||||
let m = r.unwrap();
|
||||
assert!(m.contains_id("config"));
|
||||
assert_eq!(m.get_one::<String>("config").cloned(), None);
|
||||
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_long_arg_without_value() {
|
||||
let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
|
||||
let cmd = Command::new("cmd")
|
||||
.ignore_errors(true)
|
||||
.arg(arg!(
|
||||
-c --config <FILE> "Sets a custom config file"
|
||||
));
|
||||
))
|
||||
.arg(arg!(--"unset-flag"));
|
||||
|
||||
let r = cmd.try_get_matches_from(vec!["cmd", "--config" /* missing: , "config file" */]);
|
||||
|
||||
assert!(r.is_ok(), "unexpected error: {r:?}");
|
||||
let m = r.unwrap();
|
||||
assert!(m.contains_id("config"));
|
||||
assert_eq!(m.get_one::<String>("config").cloned(), None);
|
||||
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -38,7 +48,8 @@ fn multiple_args_and_final_arg_without_value() {
|
|||
.arg(arg!(
|
||||
-x --stuff <FILE> "Sets a custom stuff file"
|
||||
))
|
||||
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue));
|
||||
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue))
|
||||
.arg(arg!(--"unset-flag"));
|
||||
|
||||
let r = cmd.try_get_matches_from(vec![
|
||||
"cmd", "-c", "file", "-f", "-x", /* missing: , "some stuff" */
|
||||
|
@ -50,8 +61,9 @@ fn multiple_args_and_final_arg_without_value() {
|
|||
m.get_one::<String>("config").map(|v| v.as_str()),
|
||||
Some("file")
|
||||
);
|
||||
assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
|
||||
assert_eq!(m.get_one::<bool>("f").copied(), Some(true));
|
||||
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
|
||||
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -64,7 +76,8 @@ fn multiple_args_and_intermittent_arg_without_value() {
|
|||
.arg(arg!(
|
||||
-x --stuff <FILE> "Sets a custom stuff file"
|
||||
))
|
||||
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue));
|
||||
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue))
|
||||
.arg(arg!(--"unset-flag"));
|
||||
|
||||
let r = cmd.try_get_matches_from(vec![
|
||||
"cmd", "-x", /* missing: ,"some stuff" */
|
||||
|
@ -77,8 +90,9 @@ fn multiple_args_and_intermittent_arg_without_value() {
|
|||
m.get_one::<String>("config").map(|v| v.as_str()),
|
||||
Some("file")
|
||||
);
|
||||
assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
|
||||
assert_eq!(m.get_one::<bool>("f").copied(), Some(true));
|
||||
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
|
||||
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -100,9 +114,11 @@ fn subcommand() {
|
|||
.long("stuff")
|
||||
.action(ArgAction::Set)
|
||||
.help("stuf value"),
|
||||
),
|
||||
)
|
||||
.arg(Arg::new("other").long("other"));
|
||||
.arg(arg!(--"unset-flag")),
|
||||
)
|
||||
.arg(Arg::new("other").long("other"))
|
||||
.arg(arg!(--"unset-flag"));
|
||||
|
||||
let m = cmd
|
||||
.try_get_matches_from(vec![
|
||||
|
@ -125,6 +141,9 @@ fn subcommand() {
|
|||
sub_m.get_one::<String>("stuff").map(|v| v.as_str()),
|
||||
Some("some other val")
|
||||
);
|
||||
assert_eq!(sub_m.get_one::<bool>("unset-flag").copied(), Some(false));
|
||||
|
||||
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue