2017-10-21 19:55:31 +00:00
|
|
|
extern crate clap;
|
|
|
|
extern crate regex;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
include!("../clap-test.rs");
|
2018-11-14 17:05:06 +00:00
|
|
|
use clap::{App, Arg, ArgMatches, ArgSettings};
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn get_app() -> App<'static> {
|
2017-10-21 19:55:31 +00:00
|
|
|
App::new("myprog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("GLOBAL_ARG")
|
|
|
|
.long("global-arg")
|
|
|
|
.help("Specifies something needed by the subcommands")
|
2018-01-31 20:15:01 +00:00
|
|
|
.setting(ArgSettings::Global)
|
|
|
|
.setting(ArgSettings::TakesValue)
|
2018-01-25 04:05:05 +00:00
|
|
|
.default_value("default_value"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("GLOBAL_FLAG")
|
|
|
|
.long("global-flag")
|
|
|
|
.help("Specifies something needed by the subcommands")
|
2018-01-31 20:15:01 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
|
|
|
.setting(ArgSettings::Global),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("outer").subcommand(App::new("inner")))
|
2017-10-21 19:55:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn get_matches(app: App<'static>, argv: &'static str) -> ArgMatches {
|
2017-10-21 19:55:31 +00:00
|
|
|
app.get_matches_from(argv.split(" ").collect::<Vec<_>>())
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn get_outer_matches<'a>(m: &'a ArgMatches) -> &'a ArgMatches {
|
2018-01-25 04:05:05 +00:00
|
|
|
m.subcommand_matches("outer")
|
|
|
|
.expect("could not access outer subcommand")
|
2017-10-21 19:55:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn get_inner_matches<'a>(m: &'a ArgMatches) -> &'a ArgMatches {
|
2018-01-25 04:05:05 +00:00
|
|
|
get_outer_matches(m)
|
|
|
|
.subcommand_matches("inner")
|
|
|
|
.expect("could not access inner subcommand")
|
2017-10-21 19:55:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn top_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
|
2017-10-21 19:55:31 +00:00
|
|
|
m.value_of("GLOBAL_ARG") == val.into()
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:51:22 +00:00
|
|
|
fn inner_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
|
2017-10-21 19:55:31 +00:00
|
|
|
get_inner_matches(m).value_of("GLOBAL_ARG") == val.into()
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:51:22 +00:00
|
|
|
fn outer_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
|
2017-10-21 19:55:31 +00:00
|
|
|
get_outer_matches(m).value_of("GLOBAL_ARG") == val.into()
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn top_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
|
2017-10-24 01:27:29 +00:00
|
|
|
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn inner_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
|
2017-10-21 19:55:31 +00:00
|
|
|
let m = get_inner_matches(m);
|
|
|
|
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn outer_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
|
2017-10-21 19:55:31 +00:00
|
|
|
let m = get_outer_matches(m);
|
|
|
|
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-10-24 01:27:29 +00:00
|
|
|
fn global_arg_used_top_level() {
|
|
|
|
let m = get_matches(get_app(), "myprog --global-arg=some_value outer inner");
|
2017-10-21 19:55:31 +00:00
|
|
|
|
|
|
|
assert!(top_can_access_arg(&m, "some_value"));
|
|
|
|
assert!(inner_can_access_arg(&m, "some_value"));
|
|
|
|
assert!(outer_can_access_arg(&m, "some_value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-10-24 01:27:29 +00:00
|
|
|
fn global_arg_used_outer() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer --global-arg=some_value inner");
|
2017-10-21 19:55:31 +00:00
|
|
|
|
|
|
|
assert!(top_can_access_arg(&m, "some_value"));
|
|
|
|
assert!(inner_can_access_arg(&m, "some_value"));
|
|
|
|
assert!(outer_can_access_arg(&m, "some_value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-10-24 01:27:29 +00:00
|
|
|
fn global_arg_used_inner() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer inner --global-arg=some_value");
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
assert!(top_can_access_arg(&m, "some_value"));
|
2017-10-21 19:55:31 +00:00
|
|
|
assert!(inner_can_access_arg(&m, "some_value"));
|
|
|
|
assert!(outer_can_access_arg(&m, "some_value"));
|
|
|
|
}
|
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
#[test]
|
|
|
|
fn global_arg_default_value() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer inner");
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
assert!(top_can_access_arg(&m, "default_value"));
|
|
|
|
assert!(inner_can_access_arg(&m, "default_value"));
|
|
|
|
assert!(outer_can_access_arg(&m, "default_value"));
|
|
|
|
}
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
#[test]
|
|
|
|
fn global_flag_used_top_level() {
|
|
|
|
let m = get_matches(get_app(), "myprog --global-flag outer inner");
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
assert!(top_can_access_flag(&m, true, 1));
|
|
|
|
assert!(inner_can_access_flag(&m, true, 1));
|
|
|
|
assert!(outer_can_access_flag(&m, true, 1));
|
|
|
|
}
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
#[test]
|
|
|
|
fn global_flag_used_outer() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer --global-flag inner");
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
assert!(top_can_access_flag(&m, true, 1));
|
|
|
|
assert!(inner_can_access_flag(&m, true, 1));
|
|
|
|
assert!(outer_can_access_flag(&m, true, 1));
|
|
|
|
}
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
#[test]
|
|
|
|
fn global_flag_used_inner() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer inner --global-flag");
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
assert!(top_can_access_flag(&m, true, 1));
|
|
|
|
assert!(inner_can_access_flag(&m, true, 1));
|
|
|
|
assert!(outer_can_access_flag(&m, true, 1));
|
|
|
|
}
|
2018-01-25 04:05:05 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
#[test]
|
|
|
|
fn global_flag_2x_used_top_level() {
|
|
|
|
let m = get_matches(get_app(), "myprog --global-flag --global-flag outer inner");
|
|
|
|
|
|
|
|
assert!(top_can_access_flag(&m, true, 2));
|
|
|
|
assert!(inner_can_access_flag(&m, true, 2));
|
|
|
|
assert!(outer_can_access_flag(&m, true, 2));
|
|
|
|
}
|
2017-10-21 19:55:31 +00:00
|
|
|
|
2017-10-24 01:27:29 +00:00
|
|
|
#[test]
|
|
|
|
fn global_flag_2x_used_inner() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer inner --global-flag --global-flag");
|
|
|
|
|
|
|
|
assert!(top_can_access_flag(&m, true, 2));
|
|
|
|
assert!(inner_can_access_flag(&m, true, 2));
|
|
|
|
assert!(outer_can_access_flag(&m, true, 2));
|
|
|
|
}
|
2017-10-21 19:55:31 +00:00
|
|
|
}
|