1use super::utils;
2
3use clap::{arg, builder::PossibleValue, Arg, ArgAction, Command};
4
5static HIDDEN_ARGS: &str = "\
6tests stuff
7
8Usage: test [OPTIONS]
9
10Options:
11  -F, --flag2         some other flag
12      --option <opt>  some option
13  -h, --help          Print help
14  -V, --version       Print version
15";
16
17#[test]
18fn hide_args() {
19    let cmd = Command::new("test")
20        .author("Kevin K.")
21        .about("tests stuff")
22        .version("1.4")
23        .args([
24            arg!(-f --flag "some flag").hide(true),
25            arg!(-F --flag2 "some other flag"),
26            arg!(--option <opt> "some option"),
27            Arg::new("DUMMY").hide(true),
28        ]);
29    utils::assert_output(cmd, "test --help", HIDDEN_ARGS, false);
30}
31
32static HIDDEN_SHORT_ARGS: &str = "\
33hides short args
34
35Usage: test [OPTIONS]
36
37Options:
38  -v, --visible  This text should be visible
39  -h, --help     Print help (see more with '--help')
40  -V, --version  Print version
41";
42
43/// Ensure hide with short option
44#[test]
45fn hide_short_args() {
46    let cmd = Command::new("test")
47        .about("hides short args")
48        .author("Steve P.")
49        .version("2.31.2")
50        .args([
51            Arg::new("cfg")
52                .short('c')
53                .long("config")
54                .hide_short_help(true)
55                .action(ArgAction::SetTrue)
56                .help("Some help text describing the --config arg"),
57            Arg::new("visible")
58                .short('v')
59                .long("visible")
60                .action(ArgAction::SetTrue)
61                .help("This text should be visible"),
62        ]);
63
64    utils::assert_output(cmd, "test -h", HIDDEN_SHORT_ARGS, false);
65}
66
67/// Ensure visible with opposite option
68#[test]
69fn hide_short_args_long_help() {
70    static HIDDEN_SHORT_ARGS_LONG_HELP: &str = "\
71hides short args
72
73Usage: test [OPTIONS]
74
75Options:
76  -c, --config
77          Some help text describing the --config arg
78
79  -v, --visible
80          This text should be visible
81
82  -h, --help
83          Print help (see a summary with '-h')
84
85  -V, --version
86          Print version
87";
88
89    let cmd = Command::new("test")
90        .about("hides short args")
91        .author("Steve P.")
92        .version("2.31.2")
93        .args([
94            Arg::new("cfg")
95                .short('c')
96                .long("config")
97                .hide_short_help(true)
98                .action(ArgAction::SetTrue)
99                .help("Some help text describing the --config arg"),
100            Arg::new("visible")
101                .short('v')
102                .long("visible")
103                .action(ArgAction::SetTrue)
104                .help("This text should be visible"),
105        ]);
106
107    utils::assert_output(cmd, "test --help", HIDDEN_SHORT_ARGS_LONG_HELP, false);
108}
109
110static HIDDEN_LONG_ARGS: &str = "\
111hides long args
112
113Usage: test [OPTIONS]
114
115Options:
116  -v, --visible
117          This text should be visible
118
119  -h, --help
120          Print help (see a summary with '-h')
121
122  -V, --version
123          Print version
124";
125
126#[test]
127fn hide_long_args() {
128    let cmd = Command::new("test")
129        .about("hides long args")
130        .author("Steve P.")
131        .version("2.31.2")
132        .args([
133            Arg::new("cfg")
134                .short('c')
135                .long("config")
136                .hide_long_help(true)
137                .action(ArgAction::SetTrue)
138                .help("Some help text describing the --config arg"),
139            Arg::new("visible")
140                .short('v')
141                .long("visible")
142                .action(ArgAction::SetTrue)
143                .help("This text should be visible"),
144        ]);
145
146    utils::assert_output(cmd, "test --help", HIDDEN_LONG_ARGS, false);
147}
148
149static HIDDEN_LONG_ARGS_SHORT_HELP: &str = "\
150hides long args
151
152Usage: test [OPTIONS]
153
154Options:
155  -c, --config   Some help text describing the --config arg
156  -v, --visible  This text should be visible
157  -h, --help     Print help (see more with '--help')
158  -V, --version  Print version
159";
160
161#[test]
162fn hide_long_args_short_help() {
163    let cmd = Command::new("test")
164        .about("hides long args")
165        .author("Steve P.")
166        .version("2.31.2")
167        .args([
168            Arg::new("cfg")
169                .short('c')
170                .long("config")
171                .hide_long_help(true)
172                .action(ArgAction::SetTrue)
173                .help("Some help text describing the --config arg"),
174            Arg::new("visible")
175                .short('v')
176                .long("visible")
177                .action(ArgAction::SetTrue)
178                .help("This text should be visible"),
179        ]);
180
181    utils::assert_output(cmd, "test -h", HIDDEN_LONG_ARGS_SHORT_HELP, false);
182}
183
184static HIDDEN_POS_ARGS: &str = "\
185Usage: test [another]
186
187Arguments:
188  [another]  another pos
189
190Options:
191  -h, --help     Print help
192  -V, --version  Print version
193";
194
195#[test]
196fn hide_pos_args() {
197    let cmd = Command::new("test").version("1.4").args([
198        Arg::new("pos").help("some pos").hide(true),
199        Arg::new("another").help("another pos"),
200    ]);
201
202    utils::assert_output(cmd, "test --help", HIDDEN_POS_ARGS, false);
203}
204
205static HIDDEN_SUBCMDS: &str = "\
206Usage: test
207
208Options:
209  -h, --help     Print help
210  -V, --version  Print version
211";
212
213#[test]
214fn hide_subcmds() {
215    let cmd = Command::new("test")
216        .version("1.4")
217        .subcommand(Command::new("sub").hide(true));
218
219    utils::assert_output(cmd, "test --help", HIDDEN_SUBCMDS, false);
220}
221
222static HIDDEN_OPT_ARGS_ONLY: &str = "\
223Usage: test
224
225After help
226";
227
228#[test]
229fn hide_opt_args_only() {
230    let cmd = Command::new("test")
231        .version("1.4")
232        .after_help("After help")
233        .disable_help_flag(true)
234        .disable_version_flag(true)
235        .arg(arg!(-h - -help).action(ArgAction::Help).hide(true))
236        .arg(arg!(-v - -version).hide(true))
237        .arg(arg!(--option <opt> "some option").hide(true));
238
239    utils::assert_output(cmd, "test --help", HIDDEN_OPT_ARGS_ONLY, false);
240}
241
242static HIDDEN_POS_ARGS_ONLY: &str = "\
243Usage: test
244
245After help
246";
247
248#[test]
249fn hide_pos_args_only() {
250    let cmd = Command::new("test")
251        .version("1.4")
252        .after_help("After help")
253        .disable_help_flag(true)
254        .disable_version_flag(true)
255        .arg(arg!(-h - -help).action(ArgAction::Help).hide(true))
256        .arg(arg!(-v - -version).hide(true))
257        .args([Arg::new("pos").help("some pos").hide(true)]);
258
259    utils::assert_output(cmd, "test --help", HIDDEN_POS_ARGS_ONLY, false);
260}
261
262static HIDDEN_SUBCMDS_ONLY: &str = "\
263Usage: test
264
265After help
266";
267
268#[test]
269fn hide_subcmds_only() {
270    let cmd = Command::new("test")
271        .version("1.4")
272        .after_help("After help")
273        .disable_help_flag(true)
274        .disable_version_flag(true)
275        .arg(arg!(-h - -help).action(ArgAction::Help).hide(true))
276        .arg(arg!(-v - -version).hide(true))
277        .subcommand(Command::new("sub").hide(true));
278
279    utils::assert_output(cmd, "test --help", HIDDEN_SUBCMDS_ONLY, false);
280}
281
282#[test]
283fn hidden_arg_with_possible_value_with_help() {
284    // Normally the presence of a possible value with a help text triggers a
285    // change of the --help help text by appending `(see more with '--help')`
286    // or `(see a summary with '-h')`. When the argument is completely hidden
287    // we however do not want it to trigger that change.
288    static POS_VALS_HELP: &str = "\
289Usage: ctest
290
291Options:
292  -h, --help  Print help
293";
294    let app = Command::new("ctest").arg(
295        Arg::new("pos")
296            .hide(true)
297            .value_parser([
298                PossibleValue::new("fast"),
299                PossibleValue::new("slow").help("not as fast"),
300            ])
301            .action(ArgAction::Set),
302    );
303    utils::assert_output(app, "ctest --help", POS_VALS_HELP, false);
304}
305