2021-11-19 20:33:11 +00:00
|
|
|
use clap::{arg, App, AppSettings, Arg};
|
2021-05-13 14:16:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_short_arg_without_value() {
|
2021-11-19 20:33:11 +00:00
|
|
|
let app = App::new("app").setting(AppSettings::IgnoreErrors).arg(arg!(
|
|
|
|
-c --config [FILE] "Sets a custom config file"
|
|
|
|
));
|
2021-05-13 14:16:38 +00:00
|
|
|
|
|
|
|
let r = app.try_get_matches_from(vec!["app", "-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() {
|
2021-11-19 20:33:11 +00:00
|
|
|
let app = App::new("app").setting(AppSettings::IgnoreErrors).arg(arg!(
|
|
|
|
-c --config [FILE] "Sets a custom config file"
|
|
|
|
));
|
2021-05-13 14:16:38 +00:00
|
|
|
|
|
|
|
let r = app.try_get_matches_from(vec!["app", "--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() {
|
|
|
|
let app = App::new("app")
|
|
|
|
.setting(AppSettings::IgnoreErrors)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(
|
|
|
|
-c --config [FILE] "Sets a custom config file"
|
2021-11-18 20:17:31 +00:00
|
|
|
))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(
|
|
|
|
-x --stuff [FILE] "Sets a custom stuff file"
|
2021-11-18 20:17:31 +00:00
|
|
|
))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(f: -f "Flag"));
|
2021-05-13 14:16:38 +00:00
|
|
|
|
|
|
|
let r = app.try_get_matches_from(vec![
|
|
|
|
"app", "-c", "file", "-f", "-x", /* missing: , "some stuff" */
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(r.is_ok(), "unexpected error: {:?}", r);
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.value_of("config"), Some("file"));
|
|
|
|
assert!(m.is_present("f"));
|
|
|
|
assert_eq!(m.value_of("stuff"), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_args_and_intermittent_arg_without_value() {
|
|
|
|
let app = App::new("app")
|
|
|
|
.setting(AppSettings::IgnoreErrors)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(
|
|
|
|
-c --config[FILE] "Sets a custom config file"
|
2021-11-18 20:17:31 +00:00
|
|
|
))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(
|
|
|
|
-x --stuff[FILE] "Sets a custom stuff file"
|
2021-11-18 20:17:31 +00:00
|
|
|
))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(f: -f "Flag"));
|
2021-05-13 14:16:38 +00:00
|
|
|
|
|
|
|
let r = app.try_get_matches_from(vec![
|
|
|
|
"app", "-x", /* missing: ,"some stuff" */
|
|
|
|
"-c", "file", "-f",
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(r.is_ok(), "unexpected error: {:?}", r);
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.value_of("config"), Some("file"));
|
|
|
|
assert!(m.is_present("f"));
|
|
|
|
assert_eq!(m.value_of("stuff"), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subcommand() {
|
|
|
|
let app = App::new("test")
|
|
|
|
.setting(AppSettings::IgnoreErrors)
|
|
|
|
.subcommand(
|
|
|
|
App::new("some")
|
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
|
|
|
.takes_value(true)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2021-05-13 14:16:38 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("stuff")
|
|
|
|
.short('x')
|
|
|
|
.long("stuff")
|
|
|
|
.takes_value(true)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("stuf value"),
|
2021-05-13 14:16:38 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("other").long("other"));
|
|
|
|
|
2021-12-27 18:56:12 +00:00
|
|
|
let m = app
|
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"myprog",
|
|
|
|
"some",
|
|
|
|
"--test", /* missing: ,"some val" */
|
|
|
|
"-x",
|
|
|
|
"some other val",
|
|
|
|
])
|
|
|
|
.unwrap();
|
2021-05-13 14:16:38 +00:00
|
|
|
|
|
|
|
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.value_of("test"), None);
|
|
|
|
assert_eq!(sub_m.value_of("stuff"), Some("some other val"));
|
|
|
|
}
|