2020-02-04 08:10:53 +00:00
|
|
|
mod utils;
|
2017-10-24 14:45:33 +00:00
|
|
|
|
2020-02-04 08:10:53 +00:00
|
|
|
use clap::{App, Arg};
|
2017-10-24 14:45:33 +00:00
|
|
|
|
2021-02-07 11:58:43 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1076() {
|
|
|
|
let mut app = App::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")
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("Specifies something needed by the subcommands")
|
2020-02-04 08:10:53 +00:00
|
|
|
.global(true)
|
|
|
|
.takes_value(true)
|
|
|
|
.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")
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("Specifies something needed by the subcommands")
|
2021-06-16 05:28:25 +00:00
|
|
|
.global(true)
|
|
|
|
.takes_value(true),
|
2020-02-04 08:10:53 +00:00
|
|
|
)
|
2021-02-07 11:58:43 +00:00
|
|
|
.subcommand(App::new("outer").subcommand(App::new("inner")));
|
2020-02-04 08:10:53 +00:00
|
|
|
let _ = app.try_get_matches_from_mut(vec!["myprog"]);
|
|
|
|
let _ = app.try_get_matches_from_mut(vec!["myprog"]);
|
|
|
|
let _ = app.try_get_matches_from_mut(vec!["myprog"]);
|
2017-10-24 14:45:33 +00:00
|
|
|
}
|
2020-12-12 13:55:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn propagate_global_arg_in_subcommand_to_subsubcommand_1385() {
|
|
|
|
let m1 = App::new("foo")
|
|
|
|
.subcommand(
|
|
|
|
App::new("sub1")
|
|
|
|
.arg(Arg::new("arg1").long("arg1").takes_value(true).global(true))
|
|
|
|
.subcommand(App::new("sub1a")),
|
|
|
|
)
|
|
|
|
.get_matches_from(&["foo", "sub1", "--arg1", "v1", "sub1a"]);
|
|
|
|
assert_eq!(
|
|
|
|
"v1",
|
|
|
|
m1.subcommand_matches("sub1")
|
|
|
|
.unwrap()
|
|
|
|
.subcommand_matches("sub1a")
|
|
|
|
.unwrap()
|
|
|
|
.value_of("arg1")
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-07 11:58:43 +00:00
|
|
|
fn propagate_global_arg_to_subcommand_in_subsubcommand_2053() {
|
2020-12-12 13:55:49 +00:00
|
|
|
let m = App::new("opts")
|
|
|
|
.arg(Arg::from("--global-flag").global(true))
|
|
|
|
.arg(
|
|
|
|
Arg::from("--global-str <global-str>")
|
|
|
|
.required(false)
|
|
|
|
.global(true),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
App::new("test")
|
|
|
|
.arg(Arg::from("--sub-flag").global(true))
|
|
|
|
.arg(
|
|
|
|
Arg::from("--sub-str <sub-str>")
|
|
|
|
.required(false)
|
|
|
|
.global(true),
|
|
|
|
)
|
|
|
|
.subcommand(App::new("test")),
|
|
|
|
)
|
|
|
|
.get_matches_from(&[
|
|
|
|
"app",
|
|
|
|
"test",
|
|
|
|
"test",
|
|
|
|
"--global-flag",
|
|
|
|
"--global-str",
|
|
|
|
"hello",
|
|
|
|
"--sub-flag",
|
|
|
|
"--sub-str",
|
|
|
|
"world",
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
Some("world"),
|
|
|
|
m.subcommand_matches("test").unwrap().value_of("sub-str")
|
|
|
|
);
|
|
|
|
}
|
2021-05-18 21:21:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn global_arg_available_in_subcommand() {
|
|
|
|
let m = App::new("opt")
|
|
|
|
.args(&[
|
|
|
|
Arg::new("global").global(true).long("global"),
|
|
|
|
Arg::new("not").global(false).long("not"),
|
|
|
|
])
|
|
|
|
.subcommand(App::new("ping"))
|
|
|
|
.get_matches_from(&["opt", "ping", "--global"]);
|
|
|
|
|
|
|
|
assert!(m.is_present("global"));
|
|
|
|
assert!(m.subcommand_matches("ping").unwrap().is_present("global"));
|
|
|
|
}
|