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