forked from clap-rs/clap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropagate_globals_down.rs
89 lines (70 loc) · 3.29 KB
/
propagate_globals_down.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
extern crate clap;
extern crate regex;
#[cfg(test)]
mod tests {
include!("../clap-test.rs");
use clap;
use clap::{App, Arg, SubCommand, AppSettings};
fn setup_app_with_globals_and_subcommands<'a, 'b>() -> clap::App<'a, 'b> {
let global_arg = Arg::with_name("GLOBAL_ARG")
.long("global-arg")
.help(
"Specifies something needed by the subcommands",
)
.global(true)
.takes_value(true);
let double_sub_command = SubCommand::with_name("outer")
.subcommand(SubCommand::with_name("inner"));
App::new("myprog")
.setting(AppSettings::PropagateGlobalValuesDown)
.arg(global_arg)
.subcommand(double_sub_command)
}
fn first_subcommand_can_access_global(arg_vector : Vec<&str>) {
let matches = setup_app_with_globals_and_subcommands().get_matches_from(
arg_vector
);
let sub_match = matches.subcommand_matches("outer").expect("could not access subcommand");
assert_eq!(sub_match.value_of("GLOBAL_ARG").expect("subcommand could not access global arg"),
"some_value", "subcommand did not have expected value for global arg");
}
fn second_subcommand_can_access_global(arg_vector : Vec<&str>) {
let matches = setup_app_with_globals_and_subcommands().get_matches_from(
arg_vector
);
let sub_match = matches.subcommand_matches("outer").expect("could not access subcommand");
let sub_sub_match = sub_match.subcommand_matches("inner").expect("could not access inner sub");
assert_eq!(sub_sub_match.value_of("GLOBAL_ARG").expect("inner subcommand could not access global arg"),
"some_value", "inner subcommand did not have expected value for global arg");
}
#[test]
fn subcommand_can_access_global_arg_if_global_arg_is_first() {
// currently passes
first_subcommand_can_access_global(vec!["myprog", "--global-arg", "some_value", "outer", "inner"]);
}
#[test]
fn subcommand_can_access_global_arg_if_global_arg_is_in_the_middle() {
// currently passes
first_subcommand_can_access_global(vec!["myprog", "outer", "--global-arg", "some_value" ,"inner"]);
}
#[test]
fn first_subcommand_can_access_global_arg_if_global_arg_is_last() {
// currently fails - hypothesis - nothing propagates global args back up
first_subcommand_can_access_global(vec!["myprog", "outer", "inner", "--global-arg", "some_value"]);
}
#[test]
fn second_subcommand_can_access_global_arg_if_global_arg_is_first() {
// currently passes
second_subcommand_can_access_global(vec!["myprog", "--global-arg", "some_value", "outer", "inner"]);
}
#[test]
fn second_subcommand_can_access_global_arg_if_global_arg_is_in_the_middle() {
// currently fails - hypothesis: subcommands do not recursively propagate global args
second_subcommand_can_access_global(vec!["myprog", "outer", "--global-arg", "some_value" ,"inner"]);
}
#[test]
fn second_subcommand_can_access_global_arg_if_global_arg_is_last() {
// currently passes
second_subcommand_can_access_global(vec!["myprog", "outer", "inner", "--global-arg", "some_value"]);
}
}