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                        .hide(true)
65                        .action(clap::ArgAction::Set)
66                        .require_equals(true)
67                        .help("the other case to test"),
68                )
69                .arg(clap::Arg::new("path").num_args(1..)),
70        )
71        .subcommand(clap::Command::new("some-cmd-with-hyphens").alias("hyphen"))
72        .subcommand(clap::Command::new("some-hidden-cmd").hide(true))
73}
74
75pub fn quoting_command(name: &'static str) -> clap::Command {
76    clap::Command::new(name)
77        .version("3.0")
78        .arg(
79            clap::Arg::new("single-quotes")
80                .long("single-quotes")
81                .action(clap::ArgAction::SetTrue)
82                .help("Can be 'always', 'auto', or 'never'"),
83        )
84        .arg(
85            clap::Arg::new("double-quotes")
86                .long("double-quotes")
87                .action(clap::ArgAction::SetTrue)
88                .help("Can be \"always\", \"auto\", or \"never\""),
89        )
90        .arg(
91            clap::Arg::new("backticks")
92                .long("backticks")
93                .action(clap::ArgAction::SetTrue)
94                .help("For more information see `echo test`"),
95        )
96        .arg(
97            clap::Arg::new("backslash")
98                .long("backslash")
99                .action(clap::ArgAction::SetTrue)
100                .help("Avoid '\\n'"),
101        )
102        .arg(
103            clap::Arg::new("brackets")
104                .long("brackets")
105                .action(clap::ArgAction::SetTrue)
106                .help("List packages [filter]"),
107        )
108        .arg(
109            clap::Arg::new("expansions")
110                .long("expansions")
111                .action(clap::ArgAction::SetTrue)
112                .help("Execute the shell command with $SHELL"),
113        )
114        .subcommands([
115            clap::Command::new("cmd-single-quotes").about("Can be 'always', 'auto', or 'never'"),
116            clap::Command::new("cmd-double-quotes")
117                .about("Can be \"always\", \"auto\", or \"never\""),
118            clap::Command::new("cmd-backticks").about("For more information see `echo test`"),
119            clap::Command::new("cmd-backslash").about("Avoid '\\n'"),
120            clap::Command::new("cmd-brackets").about("List packages [filter]"),
121            clap::Command::new("cmd-expansions").about("Execute the shell command with $SHELL"),
122        ])
123}
124
125pub fn aliases_command(name: &'static str) -> clap::Command {
126    clap::Command::new(name)
127        .version("3.0")
128        .about("testing bash completions")
129        .arg(
130            clap::Arg::new("flag")
131                .short('f')
132                .visible_short_alias('F')
133                .long("flag")
134                .action(clap::ArgAction::SetTrue)
135                .visible_alias("flg")
136                .help("cmd flag"),
137        )
138        .arg(
139            clap::Arg::new("option")
140                .short('o')
141                .visible_short_alias('O')
142                .long("option")
143                .visible_alias("opt")
144                .help("cmd option")
145                .action(clap::ArgAction::Set),
146        )
147        .arg(clap::Arg::new("positional"))
148}
149
150pub fn sub_subcommands_command(name: &'static str) -> clap::Command {
151    feature_sample_command(name).subcommand(
152        clap::Command::new("some_cmd")
153            .about("top level subcommand")
154            .visible_alias("some_cmd_alias")
155            .subcommand(
156                clap::Command::new("sub_cmd").about("sub-subcommand").arg(
157                    clap::Arg::new("config")
158                        .long("config")
159                        .action(clap::ArgAction::Set)
160                        .value_parser([
161                            PossibleValue::new("Lest quotes, aren't escaped.")
162                                .help("help,with,comma"),
163                            PossibleValue::new("Second to trigger display of options"),
164                        ])
165                        .help("the other case to test"),
166                ),
167            ),
168    )
169}
170
171pub fn value_hint_command(name: &'static str) -> clap::Command {
172    clap::Command::new(name)
173        .arg(
174            clap::Arg::new("choice")
175                .long("choice")
176                .action(clap::ArgAction::Set)
177                .value_parser(["bash", "fish", "zsh"]),
178        )
179        .arg(
180            clap::Arg::new("unknown")
181                .long("unknown")
182                .value_hint(clap::ValueHint::Unknown),
183        )
184        .arg(
185            clap::Arg::new("other")
186                .long("other")
187                .value_hint(clap::ValueHint::Other),
188        )
189        .arg(
190            clap::Arg::new("path")
191                .long("path")
192                .short('p')
193                .value_hint(clap::ValueHint::AnyPath),
194        )
195        .arg(
196            clap::Arg::new("file")
197                .long("file")
198                .short('f')
199                .value_hint(clap::ValueHint::FilePath),
200        )
201        .arg(
202            clap::Arg::new("dir")
203                .long("dir")
204                .short('d')
205                .value_hint(clap::ValueHint::DirPath),
206        )
207        .arg(
208            clap::Arg::new("exe")
209                .long("exe")
210                .short('e')
211                .value_hint(clap::ValueHint::ExecutablePath),
212        )
213        .arg(
214            clap::Arg::new("cmd_name")
215                .long("cmd-name")
216                .value_hint(clap::ValueHint::CommandName),
217        )
218        .arg(
219            clap::Arg::new("cmd")
220                .long("cmd")
221                .short('c')
222                .value_hint(clap::ValueHint::CommandString),
223        )
224        .arg(
225            clap::Arg::new("command_with_args")
226                .action(clap::ArgAction::Set)
227                .num_args(1..)
228                .trailing_var_arg(true)
229                .value_hint(clap::ValueHint::CommandWithArguments),
230        )
231        .arg(
232            clap::Arg::new("user")
233                .short('u')
234                .long("user")
235                .value_hint(clap::ValueHint::Username),
236        )
237        .arg(
238            clap::Arg::new("host")
239                .short('H')
240                .long("host")
241                .value_hint(clap::ValueHint::Hostname),
242        )
243        .arg(
244            clap::Arg::new("url")
245                .long("url")
246                .value_hint(clap::ValueHint::Url),
247        )
248        .arg(
249            clap::Arg::new("email")
250                .long("email")
251                .value_hint(clap::ValueHint::EmailAddress),
252        )
253}
254
255pub fn value_terminator_command(name: &'static str) -> clap::Command {
256    clap::Command::new(name).arg(
257        clap::Arg::new("arguments")
258            .help("multi-valued argument with a value terminator")
259            .num_args(1..)
260            .value_terminator(";"),
261    )
262}
263
264pub fn two_multi_valued_arguments_command(name: &'static str) -> clap::Command {
265    clap::Command::new(name)
266        .arg(
267            clap::Arg::new("first")
268                .help("first multi-valued argument")
269                .num_args(1..),
270        )
271        .arg(
272            clap::Arg::new("second")
273                .help("second multi-valued argument")
274                .raw(true),
275        )
276}
277
278pub fn assert_matches_path(
279    expected_path: impl AsRef<std::path::Path>,
280    gen: impl clap_complete::Generator,
281    mut cmd: clap::Command,
282    name: &'static str,
283) {
284    let mut buf = vec![];
285    clap_complete::generate(gen, &mut cmd, name, &mut buf);
286
287    snapbox::Assert::new()
288        .action_env("SNAPSHOTS")
289        .normalize_paths(false)
290        .matches_path(expected_path, buf);
291}
292