summaryrefslogtreecommitdiff
path: root/subprojects/clap/tests/builder/global_args.rs
blob: 2f4eae0ee4c53ba309acf6ed6b67fed6cfa66536 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use clap::{arg, Arg, ArgAction, Command};

#[test]
fn issue_1076() {
    let mut cmd = Command::new("myprog")
        .arg(
            Arg::new("GLOBAL_ARG")
                .long("global-arg")
                .help("Specifies something needed by the subcommands")
                .global(true)
                .action(ArgAction::Set)
                .default_value("default_value"),
        )
        .arg(
            Arg::new("GLOBAL_FLAG")
                .long("global-flag")
                .help("Specifies something needed by the subcommands")
                .global(true)
                .action(ArgAction::Set),
        )
        .subcommand(Command::new("outer").subcommand(Command::new("inner")));
    let _ = cmd.try_get_matches_from_mut(vec!["myprog"]);
    let _ = cmd.try_get_matches_from_mut(vec!["myprog"]);
    let _ = cmd.try_get_matches_from_mut(vec!["myprog"]);
}

#[test]
fn propagate_global_arg_in_subcommand_to_subsubcommand_1385() {
    let m1 = Command::new("foo")
        .subcommand(
            Command::new("sub1")
                .arg(
                    Arg::new("arg1")
                        .long("arg1")
                        .action(ArgAction::Set)
                        .global(true),
                )
                .subcommand(Command::new("sub1a")),
        )
        .try_get_matches_from(["foo", "sub1", "--arg1", "v1", "sub1a"])
        .unwrap();
    assert_eq!(
        "v1",
        m1.subcommand_matches("sub1")
            .unwrap()
            .subcommand_matches("sub1a")
            .unwrap()
            .get_one::<String>("arg1")
            .map(|v| v.as_str())
            .unwrap()
    );
}

#[test]
fn propagate_global_arg_to_subcommand_in_subsubcommand_2053() {
    let m = Command::new("opts")
        .arg(arg!(--"global-flag").global(true))
        .arg(arg!(--"global-str" <str>).global(true))
        .subcommand(
            Command::new("test")
                .arg(arg!(--"sub-flag").global(true))
                .arg(arg!(--"sub-str" <str>).global(true))
                .subcommand(Command::new("test")),
        )
        .try_get_matches_from([
            "cmd",
            "test",
            "test",
            "--global-flag",
            "--global-str",
            "hello",
            "--sub-flag",
            "--sub-str",
            "world",
        ])
        .unwrap();
    assert_eq!(
        Some("world"),
        m.subcommand_matches("test")
            .unwrap()
            .get_one::<String>("sub-str")
            .map(|v| v.as_str())
    );
}

#[test]
fn global_arg_available_in_subcommand() {
    let m = Command::new("opt")
        .args([
            Arg::new("global")
                .global(true)
                .long("global")
                .action(ArgAction::SetTrue),
            Arg::new("not")
                .global(false)
                .long("not")
                .action(ArgAction::SetTrue),
        ])
        .subcommand(Command::new("ping"))
        .try_get_matches_from(["opt", "ping", "--global"])
        .unwrap();

    assert!(*m.get_one::<bool>("global").expect("defaulted by clap"));
    assert!(*m
        .subcommand_matches("ping")
        .unwrap()
        .get_one::<bool>("global")
        .expect("defaulted by clap"));
}

#[test]
fn deeply_nested_discovery() {
    let cmd = Command::new("a")
        .arg(arg!(--"long-a").global(true).action(ArgAction::SetTrue))
        .subcommand(
            Command::new("b")
                .arg(arg!(--"long-b").global(true).action(ArgAction::SetTrue))
                .subcommand(
                    Command::new("c")
                        .arg(arg!(--"long-c").global(true).action(ArgAction::SetTrue))
                        .subcommand(Command::new("d")),
                ),
        );

    let m = cmd
        .try_get_matches_from(["a", "b", "c", "d", "--long-a", "--long-b", "--long-c"])
        .unwrap();
    assert!(*m.get_one::<bool>("long-a").expect("defaulted by clap"));
    let m = m.subcommand_matches("b").unwrap();
    assert!(*m.get_one::<bool>("long-b").expect("defaulted by clap"));
    let m = m.subcommand_matches("c").unwrap();
    assert!(*m.get_one::<bool>("long-c").expect("defaulted by clap"));
}

#[test]
fn global_overrides_default() {
    let cmd = Command::new("test")
        .arg(
            Arg::new("name")
                .long("name")
                .global(true)
                .action(ArgAction::Set)
                .default_value("from_default"),
        )
        .subcommand(Command::new("sub"));

    let m = cmd.clone().try_get_matches_from(["test"]).unwrap();
    assert_eq!(
        m.get_one::<String>("name").unwrap().as_str(),
        "from_default"
    );

    let m = cmd
        .clone()
        .try_get_matches_from(["test", "--name", "from_arg"])
        .unwrap();
    assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");

    let m = cmd
        .clone()
        .try_get_matches_from(["test", "--name", "from_arg", "sub"])
        .unwrap();
    assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");

    let m = cmd
        .clone()
        .try_get_matches_from(["test", "sub", "--name", "from_arg"])
        .unwrap();
    assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");
}

#[test]
#[cfg(feature = "env")]
fn global_overrides_env() {
    std::env::set_var("GLOBAL_OVERRIDES_ENV", "from_env");

    let cmd = Command::new("test")
        .arg(
            Arg::new("name")
                .long("name")
                .global(true)
                .action(ArgAction::Set)
                .env("GLOBAL_OVERRIDES_ENV"),
        )
        .subcommand(Command::new("sub"));

    let m = cmd.clone().try_get_matches_from(["test"]).unwrap();
    assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_env");

    let m = cmd
        .clone()
        .try_get_matches_from(["test", "--name", "from_arg"])
        .unwrap();
    assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");

    let m = cmd
        .clone()
        .try_get_matches_from(["test", "--name", "from_arg", "sub"])
        .unwrap();
    assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");

    let m = cmd
        .clone()
        .try_get_matches_from(["test", "sub", "--name", "from_arg"])
        .unwrap();
    assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");
}