• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use super::utils;
2 
3 use clap::{arg, builder::PossibleValue, Arg, ArgAction, Command};
4 
5 static HIDDEN_ARGS: &str = "\
6 tests stuff
7 
8 Usage: test [OPTIONS]
9 
10 Options:
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]
hide_args()18 fn 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 
32 static HIDDEN_SHORT_ARGS: &str = "\
33 hides short args
34 
35 Usage: test [OPTIONS]
36 
37 Options:
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]
hide_short_args()45 fn 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]
hide_short_args_long_help()69 fn hide_short_args_long_help() {
70     static HIDDEN_SHORT_ARGS_LONG_HELP: &str = "\
71 hides short args
72 
73 Usage: test [OPTIONS]
74 
75 Options:
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 
110 static HIDDEN_LONG_ARGS: &str = "\
111 hides long args
112 
113 Usage: test [OPTIONS]
114 
115 Options:
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]
hide_long_args()127 fn 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 
149 static HIDDEN_LONG_ARGS_SHORT_HELP: &str = "\
150 hides long args
151 
152 Usage: test [OPTIONS]
153 
154 Options:
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]
hide_long_args_short_help()162 fn 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 
184 static HIDDEN_POS_ARGS: &str = "\
185 Usage: test [another]
186 
187 Arguments:
188   [another]  another pos
189 
190 Options:
191   -h, --help     Print help
192   -V, --version  Print version
193 ";
194 
195 #[test]
hide_pos_args()196 fn 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 
205 static HIDDEN_SUBCMDS: &str = "\
206 Usage: test
207 
208 Options:
209   -h, --help     Print help
210   -V, --version  Print version
211 ";
212 
213 #[test]
hide_subcmds()214 fn 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 
222 static HIDDEN_OPT_ARGS_ONLY: &str = "\
223 Usage: test
224 
225 After help
226 ";
227 
228 #[test]
hide_opt_args_only()229 fn 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 
242 static HIDDEN_POS_ARGS_ONLY: &str = "\
243 Usage: test
244 
245 After help
246 ";
247 
248 #[test]
hide_pos_args_only()249 fn 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 
262 static HIDDEN_SUBCMDS_ONLY: &str = "\
263 Usage: test
264 
265 After help
266 ";
267 
268 #[test]
hide_subcmds_only()269 fn 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]
hidden_arg_with_possible_value_with_help()283 fn 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 = "\
289 Usage: ctest
290 
291 Options:
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