1use clap::builder::PossibleValue;
2
3pub fn basic_command(name: &'static str) -> clap::Command {
4    clap::Command::new(name)
5        .arg(
6            clap::Arg::new("config")
7                .short('c')
8                .global(true)
9                .action(clap::ArgAction::SetTrue),
10        )
11        .arg(
12            clap::Arg::new("v")
13                .short('v')
14                .conflicts_with("config")
15                .action(clap::ArgAction::SetTrue),
16        )
17        .subcommand(
18            clap::Command::new("test").about("Subcommand").arg(
19                clap::Arg::new("debug")
20                    .short('d')
21                    .action(clap::ArgAction::Count),
22            ),
23        )
24}
25
26pub fn feature_sample_command(name: &'static str) -> clap::Command {
27    clap::Command::new(name)
28        .version("3.0")
29        .propagate_version(true)
30        .about("Tests completions")
31        .arg(
32            clap::Arg::new("file")
33                .value_hint(clap::ValueHint::FilePath)
34                .help("some input file"),
35        )
36        .arg(
37            clap::Arg::new("config")
38                .action(clap::ArgAction::Count)
39                .help("some config file")
40                .short('c')
41                .visible_short_alias('C')
42                .long("config")
43                .visible_alias("conf"),
44        )
45        .arg(clap::Arg::new("choice").value_parser(["first", "second"]))
46        .subcommand(
47            clap::Command::new("test").about("tests things").arg(
48                clap::Arg::new("case")
49                    .long("case")
50                    .action(clap::ArgAction::Set)
51                    .help("the case to test"),
52            ),
53        )
54}
55
56pub fn special_commands_command(name: &'static str) -> clap::Command {
57    feature_sample_command(name)
58        .subcommand(
59            clap::Command::new("some_cmd")
60                .about("tests other things")
61                .arg(
62                    clap::Arg::new("config")
63                        .long("config")
64                        .action(clap::ArgAction::Set)
65                        .help("the other case to test"),
66                ),
67        )
68        .subcommand(clap::Command::new("some-cmd-with-hyphens").alias("hyphen"))
69}
70
71pub fn quoting_command(name: &'static str) -> clap::Command {
72    clap::Command::new(name)
73        .version("3.0")
74        .arg(
75            clap::Arg::new("single-quotes")
76                .long("single-quotes")
77                .action(clap::ArgAction::SetTrue)
78                .help("Can be 'always', 'auto', or 'never'"),
79        )
80        .arg(
81            clap::Arg::new("double-quotes")
82                .long("double-quotes")
83                .action(clap::ArgAction::SetTrue)
84                .help("Can be \"always\", \"auto\", or \"never\""),
85        )
86        .arg(
87            clap::Arg::new("backticks")
88                .long("backticks")
89                .action(clap::ArgAction::SetTrue)
90                .help("For more information see `echo test`"),
91        )
92        .arg(
93            clap::Arg::new("backslash")
94                .long("backslash")
95                .action(clap::ArgAction::SetTrue)
96                .help("Avoid '\\n'"),
97        )
98        .arg(
99            clap::Arg::new("brackets")
100                .long("brackets")
101                .action(clap::ArgAction::SetTrue)
102                .help("List packages [filter]"),
103        )
104        .arg(
105            clap::Arg::new("expansions")
106                .long("expansions")
107                .action(clap::ArgAction::SetTrue)
108                .help("Execute the shell command with $SHELL"),
109        )
110        .subcommands([
111            clap::Command::new("cmd-single-quotes").about("Can be 'always', 'auto', or 'never'"),
112            clap::Command::new("cmd-double-quotes")
113                .about("Can be \"always\", \"auto\", or \"never\""),
114            clap::Command::new("cmd-backticks").about("For more information see `echo test`"),
115            clap::Command::new("cmd-backslash").about("Avoid '\\n'"),
116            clap::Command::new("cmd-brackets").about("List packages [filter]"),
117            clap::Command::new("cmd-expansions").about("Execute the shell command with $SHELL"),
118        ])
119}
120
121pub fn aliases_command(name: &'static str) -> clap::Command {
122    clap::Command::new(name)
123        .version("3.0")
124        .about("testing bash completions")
125        .arg(
126            clap::Arg::new("flag")
127                .short('f')
128                .visible_short_alias('F')
129                .long("flag")
130                .action(clap::ArgAction::SetTrue)
131                .visible_alias("flg")
132                .help("cmd flag"),
133        )
134        .arg(
135            clap::Arg::new("option")
136                .short('o')
137                .visible_short_alias('O')
138                .long("option")
139                .visible_alias("opt")
140                .help("cmd option")
141                .action(clap::ArgAction::Set),
142        )
143        .arg(clap::Arg::new("positional"))
144}
145
146pub fn sub_subcommands_command(name: &'static str) -> clap::Command {
147    feature_sample_command(name).subcommand(
148        clap::Command::new("some_cmd")
149            .about("top level subcommand")
150            .subcommand(
151                clap::Command::new("sub_cmd").about("sub-subcommand").arg(
152                    clap::Arg::new("config")
153                        .long("config")
154                        .action(clap::ArgAction::Set)
155                        .value_parser([clap::builder::PossibleValue::new(
156                            "Lest quotes aren't escaped.",
157                        )])
158                        .help("the other case to test"),
159                ),
160            ),
161    )
162}
163
164pub fn value_hint_command(name: &'static str) -> clap::Command {
165    clap::Command::new(name)
166        .arg(
167            clap::Arg::new("choice")
168                .long("choice")
169                .action(clap::ArgAction::Set)
170                .value_parser(["bash", "fish", "zsh"]),
171        )
172        .arg(
173            clap::Arg::new("unknown")
174                .long("unknown")
175                .value_hint(clap::ValueHint::Unknown),
176        )
177        .arg(
178            clap::Arg::new("other")
179                .long("other")
180                .value_hint(clap::ValueHint::Other),
181        )
182        .arg(
183            clap::Arg::new("path")
184                .long("path")
185                .short('p')
186                .value_hint(clap::ValueHint::AnyPath),
187        )
188        .arg(
189            clap::Arg::new("file")
190                .long("file")
191                .short('f')
192                .value_hint(clap::ValueHint::FilePath),
193        )
194        .arg(
195            clap::Arg::new("dir")
196                .long("dir")
197                .short('d')
198                .value_hint(clap::ValueHint::DirPath),
199        )
200        .arg(
201            clap::Arg::new("exe")
202                .long("exe")
203                .short('e')
204                .value_hint(clap::ValueHint::ExecutablePath),
205        )
206        .arg(
207            clap::Arg::new("cmd_name")
208                .long("cmd-name")
209                .value_hint(clap::ValueHint::CommandName),
210        )
211        .arg(
212            clap::Arg::new("cmd")
213                .long("cmd")
214                .short('c')
215                .value_hint(clap::ValueHint::CommandString),
216        )
217        .arg(
218            clap::Arg::new("command_with_args")
219                .action(clap::ArgAction::Set)
220                .num_args(1..)
221                .trailing_var_arg(true)
222                .value_hint(clap::ValueHint::CommandWithArguments),
223        )
224        .arg(
225            clap::Arg::new("user")
226                .short('u')
227                .long("user")
228                .value_hint(clap::ValueHint::Username),
229        )
230        .arg(
231            clap::Arg::new("host")
232                .short('H')
233                .long("host")
234                .value_hint(clap::ValueHint::Hostname),
235        )
236        .arg(
237            clap::Arg::new("url")
238                .long("url")
239                .value_hint(clap::ValueHint::Url),
240        )
241        .arg(
242            clap::Arg::new("email")
243                .long("email")
244                .value_hint(clap::ValueHint::EmailAddress),
245        )
246}
247
248pub fn hidden_option_command(name: &'static str) -> clap::Command {
249    clap::Command::new(name)
250        .arg(
251            clap::Arg::new("config")
252                .long("config")
253                .action(clap::ArgAction::Set),
254        )
255        .arg(
256            clap::Arg::new("no-config")
257                .long("no-config")
258                .hide(true)
259                .overrides_with("config"),
260        )
261}
262
263pub fn env_value_command(name: &'static str) -> clap::Command {
264    clap::Command::new(name).arg(
265        clap::Arg::new("config")
266            .short('c')
267            .long_help("Set configuration file path")
268            .required(false)
269            .action(clap::ArgAction::Set)
270            .default_value("config.toml")
271            .env("CONFIG_FILE"),
272    )
273}
274
275pub fn assert_matches_path(expected_path: impl AsRef<std::path::Path>, cmd: clap::Command) {
276    let mut buf = vec![];
277    clap_mangen::Man::new(cmd).render(&mut buf).unwrap();
278
279    snapbox::Assert::new()
280        .action_env("SNAPSHOTS")
281        .matches_path(expected_path, buf);
282}
283
284pub fn possible_values_command(name: &'static str) -> clap::Command {
285    clap::Command::new(name)
286        .arg(
287            clap::Arg::new("choice")
288                .long("choice")
289                .action(clap::ArgAction::Set)
290                .value_parser(["bash", "fish", "zsh"]),
291        )
292        .arg(
293            clap::Arg::new("method")
294                .long("method")
295                .action(clap::ArgAction::Set)
296                .value_parser([
297                    PossibleValue::new("fast").help("use the Fast method"),
298                    PossibleValue::new("slow").help("use the slow method"),
299                    PossibleValue::new("normal")
300                        .help("use normal mode")
301                        .hide(true),
302                ]),
303        )
304        .arg(
305            clap::Arg::new("positional_choice")
306                .action(clap::ArgAction::Set)
307                .help("Pick the Position you want the command to run in")
308                .value_parser([
309                    PossibleValue::new("left").help("run left adjusted"),
310                    PossibleValue::new("right"),
311                    PossibleValue::new("center").hide(true),
312                ]),
313        )
314}
315
316pub fn value_name_without_arg(name: &'static str) -> clap::Command {
317    clap::Command::new(name).arg(
318        clap::Arg::new("flag")
319            .long("flag")
320            .value_name("SPURIOUS")
321            .action(clap::ArgAction::SetTrue),
322    )
323}
324