2022-06-07 18:48:48 +00:00
|
|
|
use clap::{Arg, ArgAction, ArgMatches, Command};
|
2020-02-04 08:10:53 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
fn get_app() -> Command<'static> {
|
|
|
|
Command::new("myprog")
|
2020-02-04 08:10:53 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("GLOBAL_ARG")
|
2020-02-04 08:10:53 +00:00
|
|
|
.long("global-arg")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("Specifies something needed by the subcommands")
|
2020-02-04 08:10:53 +00:00
|
|
|
.global(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
2020-02-04 08:10:53 +00:00
|
|
|
.default_value("default_value"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("GLOBAL_FLAG")
|
2020-02-04 08:10:53 +00:00
|
|
|
.long("global-flag")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("Specifies something needed by the subcommands")
|
2020-02-04 08:10:53 +00:00
|
|
|
.global(true)
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Count),
|
2020-02-04 08:10:53 +00:00
|
|
|
)
|
2022-02-12 03:48:29 +00:00
|
|
|
.subcommand(Command::new("outer").subcommand(Command::new("inner")))
|
2020-02-04 08:10:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
fn get_matches(cmd: Command<'static>, argv: &'static str) -> ArgMatches {
|
|
|
|
cmd.try_get_matches_from(argv.split(' ').collect::<Vec<_>>())
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap()
|
2020-02-04 08:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_outer_matches(m: &ArgMatches) -> &ArgMatches {
|
|
|
|
m.subcommand_matches("outer")
|
|
|
|
.expect("could not access outer subcommand")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_inner_matches(m: &ArgMatches) -> &ArgMatches {
|
|
|
|
get_outer_matches(m)
|
|
|
|
.subcommand_matches("inner")
|
|
|
|
.expect("could not access inner subcommand")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn top_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_one::<String>("GLOBAL_ARG").map(|v| v.as_str()) == val.into()
|
2020-02-04 08:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
|
2022-05-24 15:16:50 +00:00
|
|
|
get_inner_matches(m)
|
|
|
|
.get_one::<String>("GLOBAL_ARG")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
== val.into()
|
2020-02-04 08:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn outer_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
|
2022-05-24 15:16:50 +00:00
|
|
|
get_outer_matches(m)
|
|
|
|
.get_one::<String>("GLOBAL_ARG")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
== val.into()
|
2020-02-04 08:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn top_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
|
2022-06-07 18:48:48 +00:00
|
|
|
(m.is_present("GLOBAL_FLAG") == present)
|
|
|
|
&& (m.get_one::<u64>("GLOBAL_FLAG").copied() == Some(occurrences))
|
2020-02-04 08:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
|
|
|
|
let m = get_inner_matches(m);
|
2022-06-07 18:48:48 +00:00
|
|
|
(m.is_present("GLOBAL_FLAG") == present)
|
|
|
|
&& (m.get_one::<u64>("GLOBAL_FLAG").copied() == Some(occurrences))
|
2020-02-04 08:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn outer_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
|
|
|
|
let m = get_outer_matches(m);
|
2022-06-07 18:48:48 +00:00
|
|
|
(m.is_present("GLOBAL_FLAG") == present)
|
|
|
|
&& (m.get_one::<u64>("GLOBAL_FLAG").copied() == Some(occurrences))
|
2020-02-04 08:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn global_arg_used_top_level() {
|
|
|
|
let m = get_matches(get_app(), "myprog --global-arg=some_value outer inner");
|
|
|
|
|
|
|
|
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]
|
|
|
|
fn global_arg_used_outer() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer --global-arg=some_value inner");
|
|
|
|
|
|
|
|
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]
|
|
|
|
fn global_arg_used_inner() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer inner --global-arg=some_value");
|
|
|
|
|
|
|
|
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]
|
|
|
|
fn global_arg_default_value() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer inner");
|
|
|
|
|
|
|
|
assert!(top_can_access_arg(&m, "default_value"));
|
|
|
|
assert!(inner_can_access_arg(&m, "default_value"));
|
|
|
|
assert!(outer_can_access_arg(&m, "default_value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn global_flag_used_top_level() {
|
|
|
|
let m = get_matches(get_app(), "myprog --global-flag outer inner");
|
|
|
|
|
|
|
|
assert!(top_can_access_flag(&m, true, 1));
|
|
|
|
assert!(inner_can_access_flag(&m, true, 1));
|
|
|
|
assert!(outer_can_access_flag(&m, true, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn global_flag_used_outer() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer --global-flag inner");
|
|
|
|
|
|
|
|
assert!(top_can_access_flag(&m, true, 1));
|
|
|
|
assert!(inner_can_access_flag(&m, true, 1));
|
|
|
|
assert!(outer_can_access_flag(&m, true, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn global_flag_used_inner() {
|
|
|
|
let m = get_matches(get_app(), "myprog outer inner --global-flag");
|
|
|
|
|
|
|
|
assert!(top_can_access_flag(&m, true, 1));
|
|
|
|
assert!(inner_can_access_flag(&m, true, 1));
|
|
|
|
assert!(outer_can_access_flag(&m, true, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
}
|