clap/tests/builder/ignore_errors.rs

127 lines
3.5 KiB
Rust
Raw Normal View History

2022-02-12 03:48:29 +00:00
use clap::{arg, Arg, Command};
#[test]
fn single_short_arg_without_value() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
2021-11-19 20:33:11 +00:00
-c --config [FILE] "Sets a custom config file"
));
2022-02-14 21:47:20 +00:00
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.is_present("config"));
}
#[test]
fn single_long_arg_without_value() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
2021-11-19 20:33:11 +00:00
-c --config [FILE] "Sets a custom config file"
));
2022-02-14 21:47:20 +00:00
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.is_present("config"));
}
#[test]
fn multiple_args_and_final_arg_without_value() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("cmd")
.ignore_errors(true)
2021-11-19 20:33:11 +00:00
.arg(arg!(
-c --config [FILE] "Sets a custom config file"
))
2021-11-19 20:33:11 +00:00
.arg(arg!(
-x --stuff [FILE] "Sets a custom stuff file"
))
2021-11-19 20:33:11 +00:00
.arg(arg!(f: -f "Flag"));
2022-02-14 21:47:20 +00:00
let r = cmd.try_get_matches_from(vec![
"cmd", "-c", "file", "-f", "-x", /* missing: , "some stuff" */
]);
assert!(r.is_ok(), "unexpected error: {:?}", r);
let m = r.unwrap();
assert_eq!(
m.get_one::<String>("config").map(|v| v.as_str()),
Some("file")
);
assert!(m.is_present("f"));
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
}
#[test]
fn multiple_args_and_intermittent_arg_without_value() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("cmd")
.ignore_errors(true)
2021-11-19 20:33:11 +00:00
.arg(arg!(
-c --config[FILE] "Sets a custom config file"
))
2021-11-19 20:33:11 +00:00
.arg(arg!(
-x --stuff[FILE] "Sets a custom stuff file"
))
2021-11-19 20:33:11 +00:00
.arg(arg!(f: -f "Flag"));
2022-02-14 21:47:20 +00:00
let r = cmd.try_get_matches_from(vec![
"cmd", "-x", /* missing: ,"some stuff" */
"-c", "file", "-f",
]);
assert!(r.is_ok(), "unexpected error: {:?}", r);
let m = r.unwrap();
assert_eq!(
m.get_one::<String>("config").map(|v| v.as_str()),
Some("file")
);
assert!(m.is_present("f"));
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
}
#[test]
fn subcommand() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("test")
.ignore_errors(true)
.subcommand(
2022-02-12 03:48:29 +00:00
Command::new("some")
.arg(
Arg::new("test")
.short('t')
.long("test")
.takes_value(true)
.help("testing testing"),
)
.arg(
Arg::new("stuff")
.short('x')
.long("stuff")
.takes_value(true)
.help("stuf value"),
),
)
.arg(Arg::new("other").long("other"));
2022-02-14 21:47:20 +00:00
let m = cmd
.try_get_matches_from(vec![
"myprog",
"some",
"--test", /* missing: ,"some val" */
"-x",
"some other val",
])
.unwrap();
assert_eq!(m.subcommand_name().unwrap(), "some");
let sub_m = m.subcommand_matches("some").unwrap();
assert!(
sub_m.is_present("test"),
"expected subcommand to be present due to partial parsing"
);
assert_eq!(sub_m.get_one::<String>("test").map(|v| v.as_str()), None);
assert_eq!(
sub_m.get_one::<String>("stuff").map(|v| v.as_str()),
Some("some other val")
);
}