2022-06-09 20:03:28 -05:00
|
|
|
use clap::{arg, Arg, ArgAction, Command};
|
2021-05-13 16:16:38 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_short_arg_without_value() {
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
|
2021-11-19 14:33:11 -06:00
|
|
|
-c --config [FILE] "Sets a custom config file"
|
|
|
|
));
|
2021-05-13 16:16:38 +02:00
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let r = cmd.try_get_matches_from(vec!["cmd", "-c" /* missing: , "config file" */]);
|
2021-05-13 16:16:38 +02:00
|
|
|
|
|
|
|
assert!(r.is_ok(), "unexpected error: {:?}", r);
|
|
|
|
let m = r.unwrap();
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(m.contains_id("config"));
|
2021-05-13 16:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_long_arg_without_value() {
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
|
2021-11-19 14:33:11 -06:00
|
|
|
-c --config [FILE] "Sets a custom config file"
|
|
|
|
));
|
2021-05-13 16:16:38 +02:00
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let r = cmd.try_get_matches_from(vec!["cmd", "--config" /* missing: , "config file" */]);
|
2021-05-13 16:16:38 +02:00
|
|
|
|
|
|
|
assert!(r.is_ok(), "unexpected error: {:?}", r);
|
|
|
|
let m = r.unwrap();
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(m.contains_id("config"));
|
2021-05-13 16:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_args_and_final_arg_without_value() {
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("cmd")
|
2022-02-10 11:51:40 -06:00
|
|
|
.ignore_errors(true)
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(
|
|
|
|
-c --config [FILE] "Sets a custom config file"
|
2021-11-18 14:17:31 -06:00
|
|
|
))
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(
|
|
|
|
-x --stuff [FILE] "Sets a custom stuff file"
|
2021-11-18 14:17:31 -06:00
|
|
|
))
|
2022-06-09 20:03:28 -05:00
|
|
|
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue));
|
2021-05-13 16:16:38 +02:00
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let r = cmd.try_get_matches_from(vec![
|
|
|
|
"cmd", "-c", "file", "-f", "-x", /* missing: , "some stuff" */
|
2021-05-13 16:16:38 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(r.is_ok(), "unexpected error: {:?}", r);
|
|
|
|
let m = r.unwrap();
|
2022-05-24 10:16:50 -05:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("config").map(|v| v.as_str()),
|
|
|
|
Some("file")
|
|
|
|
);
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
|
2022-06-02 16:32:47 -05:00
|
|
|
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
|
2021-05-13 16:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_args_and_intermittent_arg_without_value() {
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("cmd")
|
2022-02-10 11:51:40 -06:00
|
|
|
.ignore_errors(true)
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(
|
|
|
|
-c --config[FILE] "Sets a custom config file"
|
2021-11-18 14:17:31 -06:00
|
|
|
))
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(
|
|
|
|
-x --stuff[FILE] "Sets a custom stuff file"
|
2021-11-18 14:17:31 -06:00
|
|
|
))
|
2022-06-09 20:03:28 -05:00
|
|
|
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue));
|
2021-05-13 16:16:38 +02:00
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let r = cmd.try_get_matches_from(vec![
|
|
|
|
"cmd", "-x", /* missing: ,"some stuff" */
|
2021-05-13 16:16:38 +02:00
|
|
|
"-c", "file", "-f",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(r.is_ok(), "unexpected error: {:?}", r);
|
|
|
|
let m = r.unwrap();
|
2022-05-24 10:16:50 -05:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("config").map(|v| v.as_str()),
|
|
|
|
Some("file")
|
|
|
|
);
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
|
2022-06-02 16:32:47 -05:00
|
|
|
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
|
2021-05-13 16:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subcommand() {
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("test")
|
2022-02-10 11:51:40 -06:00
|
|
|
.ignore_errors(true)
|
2021-05-13 16:16:38 +02:00
|
|
|
.subcommand(
|
2022-02-11 21:48:29 -06:00
|
|
|
Command::new("some")
|
2021-05-13 16:16:38 +02:00
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
|
|
|
.takes_value(true)
|
2021-11-18 10:17:15 -06:00
|
|
|
.help("testing testing"),
|
2021-05-13 16:16:38 +02:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("stuff")
|
|
|
|
.short('x')
|
|
|
|
.long("stuff")
|
|
|
|
.takes_value(true)
|
2021-11-18 10:17:15 -06:00
|
|
|
.help("stuf value"),
|
2021-05-13 16:16:38 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("other").long("other"));
|
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let m = cmd
|
2021-12-27 12:56:12 -06:00
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"myprog",
|
|
|
|
"some",
|
|
|
|
"--test", /* missing: ,"some val" */
|
|
|
|
"-x",
|
|
|
|
"some other val",
|
|
|
|
])
|
|
|
|
.unwrap();
|
2021-05-13 16:16:38 +02:00
|
|
|
|
|
|
|
assert_eq!(m.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_m = m.subcommand_matches("some").unwrap();
|
|
|
|
assert!(
|
2022-06-09 20:03:28 -05:00
|
|
|
sub_m.contains_id("test"),
|
2021-05-13 16:16:38 +02:00
|
|
|
"expected subcommand to be present due to partial parsing"
|
|
|
|
);
|
2022-06-02 16:32:47 -05:00
|
|
|
assert_eq!(sub_m.get_one::<String>("test").map(|v| v.as_str()), None);
|
2022-05-24 10:16:50 -05:00
|
|
|
assert_eq!(
|
|
|
|
sub_m.get_one::<String>("stuff").map(|v| v.as_str()),
|
|
|
|
Some("some other val")
|
|
|
|
);
|
2021-05-13 16:16:38 +02:00
|
|
|
}
|