Debian Codegraph @

debian/rust-vendor/clap_complete/examples/exhaustive.rs

raw
1 use clap::builder::PossibleValue;
2 use clap_complete::{Generator, Shell, generate};
3
4 fn main() {
5 #[cfg(feature = "unstable-dynamic")]
6 clap_complete::CompleteEnv::with_factory(cli)
7 // Avoid tests snapshotting a path into `target/`
8 .completer("exhaustive")
9 .complete();
10
11 let matches = cli().get_matches();
12 if let Some(generator) = matches.get_one::<Shell>("generate") {
13 let mut cmd = cli();
14 eprintln!("Generating completion file for {generator}...");
15 print_completions(*generator, &mut cmd);
16 return;
17 }
18
19 println!("{matches:?}");
20 }
21
22 fn print_completions<G: Generator>(generator: G, cmd: &mut clap::Command) {
23 generate(
24 generator,
25 cmd,
26 cmd.get_name().to_owned(),
27 &mut std::io::stdout(),
28 );
29 }
30
31 const EMPTY: [&str; 0] = [];
32
33 #[allow(clippy::let_and_return)]
34 fn cli() -> clap::Command {
35 clap::Command::new("exhaustive")
36 .args([
37 clap::Arg::new("generate")
38 .long("generate")
39 .value_name("SHELL")
40 .value_parser(clap::value_parser!(Shell))
41 .help("generate"),
42 clap::Arg::new("empty-choice")
43 .long("empty-choice")
44 .value_parser(EMPTY),
45 ])
46 .subcommands([
47 clap::Command::new("empty")
48 .disable_help_subcommand(true)
49 .disable_help_flag(true),
50 clap::Command::new("global")
51 .version("3.0")
52 .propagate_version(true)
53 .args([clap::Arg::new("global")
54 .long("global")
55 .global(true)
56 .action(clap::ArgAction::SetTrue)
57 .help("everywhere")])
58 .subcommands([
59 clap::Command::new("one").subcommand(clap::Command::new("one-one")),
60 clap::Command::new("two"),
61 ]),
62 clap::Command::new("action").args([
63 clap::Arg::new("set-true")
64 .long("set-true")
65 .action(clap::ArgAction::SetTrue)
66 .help("bool"),
67 clap::Arg::new("set")
68 .long("set")
69 .action(clap::ArgAction::Set)
70 .help("value"),
71 clap::Arg::new("count")
72 .long("count")
73 .action(clap::ArgAction::Count)
74 .help("number"),
75 clap::Arg::new("choice")
76 .long("choice")
77 .value_parser(["first", "second"])
78 .help("enum"),
79 ]),
80 clap::Command::new("quote")
81 .args([
82 clap::Arg::new("single-quotes")
83 .long("single-quotes")
84 .action(clap::ArgAction::SetTrue)
85 .help("Can be 'always', 'auto', or 'never'"),
86 clap::Arg::new("double-quotes")
87 .long("double-quotes")
88 .action(clap::ArgAction::SetTrue)
89 .help("Can be \"always\", \"auto\", or \"never\""),
90 clap::Arg::new("backticks")
91 .long("backticks")
92 .action(clap::ArgAction::SetTrue)
93 .help("For more information see `echo test`"),
94 clap::Arg::new("backslash")
95 .long("backslash")
96 .action(clap::ArgAction::SetTrue)
97 .help("Avoid '\\n'"),
98 clap::Arg::new("brackets")
99 .long("brackets")
100 .action(clap::ArgAction::SetTrue)
101 .help("List packages [filter]"),
102 clap::Arg::new("expansions")
103 .long("expansions")
104 .action(clap::ArgAction::SetTrue)
105 .help("Execute the shell command with $SHELL"),
106 clap::Arg::new("choice")
107 .long("choice")
108 .action(clap::ArgAction::Set)
109 .value_parser(clap::builder::PossibleValuesParser::new([
110 PossibleValue::new("another shell").help("something with a space"),
111 PossibleValue::new("bash").help("bash (shell)"),
112 PossibleValue::new("fish").help("fish shell"),
113 PossibleValue::new("zsh").help("zsh shell"),
114 ])),
115 ])
116 .subcommands([
117 clap::Command::new("cmd-single-quotes")
118 .about("Can be 'always', 'auto', or 'never'"),
119 clap::Command::new("cmd-double-quotes")
120 .about("Can be \"always\", \"auto\", or \"never\""),
121 clap::Command::new("cmd-backticks")
122 .about("For more information see `echo test`"),
123 clap::Command::new("cmd-backslash").about("Avoid '\\n'"),
124 clap::Command::new("cmd-brackets").about("List packages [filter]"),
125 clap::Command::new("cmd-expansions")
126 .about("Execute the shell command with $SHELL"),
127 clap::Command::new("escape-help").about("\\tab\t\"'\nNew Line"),
128 ]),
129 clap::Command::new("value").args([
130 clap::Arg::new("delim").long("delim").value_delimiter(','),
131 clap::Arg::new("tuple").long("tuple").num_args(2),
132 clap::Arg::new("require-eq")
133 .long("require-eq")
134 .require_equals(true),
135 clap::Arg::new("term").num_args(1..).value_terminator(";"),
136 ]),
137 clap::Command::new("pacman").subcommands([
138 clap::Command::new("one").long_flag("one").short_flag('o'),
139 clap::Command::new("two").long_flag("two").short_flag('t'),
140 ]),
141 clap::Command::new("last")
142 .args([clap::Arg::new("first"), clap::Arg::new("free").last(true)]),
143 clap::Command::new("alias").args([
144 clap::Arg::new("flag")
145 .short('f')
146 .visible_short_alias('F')
147 .long("flag")
148 .action(clap::ArgAction::SetTrue)
149 .visible_alias("flg")
150 .help("cmd flag"),
151 clap::Arg::new("option")
152 .short('o')
153 .visible_short_alias('O')
154 .long("option")
155 .visible_alias("opt")
156 .help("cmd option")
157 .action(clap::ArgAction::Set),
158 clap::Arg::new("positional"),
159 ]),
160 clap::Command::new("hint").args([
161 clap::Arg::new("choice")
162 .long("choice")
163 .action(clap::ArgAction::Set)
164 .value_parser(["bash", "fish", "zsh"]),
165 clap::Arg::new("unknown")
166 .long("unknown")
167 .value_hint(clap::ValueHint::Unknown),
168 clap::Arg::new("other")
169 .long("other")
170 .value_hint(clap::ValueHint::Other),
171 clap::Arg::new("path")
172 .long("path")
173 .short('p')
174 .value_hint(clap::ValueHint::AnyPath),
175 clap::Arg::new("file")
176 .long("file")
177 .short('f')
178 .value_hint(clap::ValueHint::FilePath),
179 clap::Arg::new("dir")
180 .long("dir")
181 .short('d')
182 .value_hint(clap::ValueHint::DirPath),
183 clap::Arg::new("exe")
184 .long("exe")
185 .short('e')
186 .value_hint(clap::ValueHint::ExecutablePath),
187 clap::Arg::new("cmd_name")
188 .long("cmd-name")
189 .value_hint(clap::ValueHint::CommandName),
190 clap::Arg::new("cmd")
191 .long("cmd")
192 .short('c')
193 .value_hint(clap::ValueHint::CommandString),
194 clap::Arg::new("command_with_args")
195 .action(clap::ArgAction::Set)
196 .num_args(1..)
197 .trailing_var_arg(true)
198 .value_hint(clap::ValueHint::CommandWithArguments),
199 clap::Arg::new("user")
200 .short('u')
201 .long("user")
202 .value_hint(clap::ValueHint::Username),
203 clap::Arg::new("host")
204 .short('H')
205 .long("host")
206 .value_hint(clap::ValueHint::Hostname),
207 clap::Arg::new("url")
208 .long("url")
209 .value_hint(clap::ValueHint::Url),
210 clap::Arg::new("email")
211 .long("email")
212 .value_hint(clap::ValueHint::EmailAddress),
213 ]),
214 ])
215 }