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