]> git.proxmox.com Git - rustc.git/blame - vendor/clap/benches/03_complex.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / vendor / clap / benches / 03_complex.rs
CommitLineData
04454e1e
FG
1use clap::{arg, Arg, Command};
2use criterion::{criterion_group, criterion_main, Criterion};
3
4static OPT3_VALS: [&str; 2] = ["fast", "slow"];
5static POS3_VALS: [&str; 2] = ["vi", "emacs"];
6
7macro_rules! create_app {
8 () => {{
9 Command::new("claptests")
10 .version("0.1")
11 .about("tests clap library")
12 .author("Kevin K. <kbknapp@gmail.com>")
13 .arg(arg!(-o --option <opt> ... "tests options").required(false))
14 .arg(arg!([positional] "tests positionals"))
15 .arg(arg!(-f --flag ... "tests flags").global(true))
16 .args(&[
17 arg!(flag2: -F "tests flags with exclusions")
18 .conflicts_with("flag")
19 .requires("option2"),
20 arg!(option2: --"long-option-2" <option2> "tests long options with exclusions")
21 .required(false)
22 .conflicts_with("option")
23 .requires("positional2"),
24 arg!([positional2] "tests positionals with exclusions"),
25 arg!(-O --Option <option3> "tests options with specific value sets")
26 .required(false)
27 .possible_values(OPT3_VALS),
28 arg!([positional3] ... "tests positionals with specific values")
29 .possible_values(POS3_VALS),
30 arg!(--multvals "Tests multiple values not mult occs").required(false).value_names(&["one", "two"]),
31 arg!(
32 --multvalsmo "Tests multiple values, not mult occs"
33 ).multiple_values(true).required(false).value_names(&["one", "two"]),
34 arg!(--minvals2 <minvals> ... "Tests 2 min vals").min_values(2).multiple_values(true).required(false),
35 arg!(--maxvals3 <maxvals> ... "Tests 3 max vals").max_values(3).multiple_values(true).required(false),
36 ])
37 .subcommand(
38 Command::new("subcmd")
39 .about("tests subcommands")
40 .version("0.1")
41 .author("Kevin K. <kbknapp@gmail.com>")
42 .arg(arg!(-o --option <scoption> ... "tests options").required(false))
43 .arg(arg!([scpositional] "tests positionals"))
44 )
45 }};
46}
47
48pub fn build_from_builder(c: &mut Criterion) {
49 c.bench_function("build_from_builder", |b| {
50 b.iter(|| {
51 Command::new("claptests")
52 .version("0.1")
53 .about("tests clap library")
54 .author("Kevin K. <kbknapp@gmail.com>")
55 .arg(
56 Arg::new("opt")
57 .help("tests options")
58 .short('o')
59 .long("option")
60 .takes_value(true)
61 .multiple_values(true)
62 .multiple_occurrences(true),
63 )
64 .arg(Arg::new("positional").help("tests positionals").index(1))
65 .arg(
66 Arg::new("flag")
67 .short('f')
68 .help("tests flags")
69 .long("flag")
70 .global(true)
71 .multiple_occurrences(true),
72 )
73 .arg(
74 Arg::new("flag2")
75 .short('F')
76 .help("tests flags with exclusions")
77 .conflicts_with("flag")
78 .requires("option2"),
79 )
80 .arg(
81 Arg::new("option2")
82 .help("tests long options with exclusions")
83 .conflicts_with("option")
84 .requires("positional2")
85 .takes_value(true)
86 .long("long-option-2"),
87 )
88 .arg(
89 Arg::new("positional2")
90 .index(3)
91 .help("tests positionals with exclusions"),
92 )
93 .arg(
94 Arg::new("option3")
95 .short('O')
96 .long("Option")
97 .takes_value(true)
98 .help("tests options with specific value sets")
99 .possible_values(OPT3_VALS),
100 )
101 .arg(
102 Arg::new("positional3")
103 .takes_value(true)
104 .multiple_values(true)
105 .multiple_occurrences(true)
106 .help("tests positionals with specific values")
107 .index(4)
108 .possible_values(POS3_VALS),
109 )
110 .arg(
111 Arg::new("multvals")
112 .long("multvals")
113 .help("Tests multiple values, not mult occs")
114 .value_names(&["one", "two"]),
115 )
116 .arg(
117 Arg::new("multvalsmo")
118 .long("multvalsmo")
119 .takes_value(true)
120 .multiple_values(true)
121 .multiple_occurrences(true)
122 .help("Tests multiple values, not mult occs")
123 .value_names(&["one", "two"]),
124 )
125 .arg(
126 Arg::new("minvals")
127 .long("minvals2")
128 .takes_value(true)
129 .multiple_values(true)
130 .multiple_occurrences(true)
131 .help("Tests 2 min vals")
132 .min_values(2),
133 )
134 .arg(
135 Arg::new("maxvals")
136 .long("maxvals3")
137 .takes_value(true)
138 .multiple_values(true)
139 .multiple_occurrences(true)
140 .help("Tests 3 max vals")
141 .max_values(3),
142 )
143 .subcommand(
144 Command::new("subcmd")
145 .about("tests subcommands")
146 .version("0.1")
147 .author("Kevin K. <kbknapp@gmail.com>")
148 .arg(
149 Arg::new("scoption")
150 .short('o')
151 .long("option")
152 .takes_value(true)
153 .multiple_values(true)
154 .multiple_occurrences(true)
155 .help("tests options"),
156 )
157 .arg(Arg::new("scpositional").index(1).help("tests positionals")),
158 )
159 })
160 });
161}
162
163pub fn parse_complex(c: &mut Criterion) {
164 c.bench_function("parse_complex", |b| {
165 b.iter(|| create_app!().get_matches_from(vec![""]))
166 });
167}
168
169pub fn parse_complex_with_flag(c: &mut Criterion) {
170 c.bench_function("parse_complex_with_flag", |b| {
171 b.iter(|| create_app!().get_matches_from(vec!["myprog", "-f"]))
172 });
173}
174
175pub fn parse_complex_with_opt(c: &mut Criterion) {
176 c.bench_function("parse_complex_with_opt", |b| {
177 b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1"]))
178 });
179}
180
181pub fn parse_complex_with_pos(c: &mut Criterion) {
182 c.bench_function("parse_complex_with_pos", |b| {
183 b.iter(|| create_app!().get_matches_from(vec!["myprog", "arg1"]))
184 });
185}
186
187pub fn parse_complex_with_sc(c: &mut Criterion) {
188 c.bench_function("parse_complex_with_sc", |b| {
189 b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd"]))
190 });
191}
192
193pub fn parse_complex_with_sc_flag(c: &mut Criterion) {
194 c.bench_function("parse_complex_with_sc_flag", |b| {
195 b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "-f"]))
196 });
197}
198
199pub fn parse_complex_with_sc_opt(c: &mut Criterion) {
200 c.bench_function("parse_complex_with_sc_opt", |b| {
201 b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "-o", "option1"]))
202 });
203}
204
205pub fn parse_complex_with_sc_pos(c: &mut Criterion) {
206 c.bench_function("parse_complex_with_sc_pos", |b| {
207 b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "arg1"]))
208 });
209}
210
211pub fn parse_complex1(c: &mut Criterion) {
212 c.bench_function("parse_complex1", |b| {
213 b.iter(|| {
214 create_app!().get_matches_from(vec![
215 "myprog",
216 "-ff",
217 "-o",
218 "option1",
219 "arg1",
220 "-O",
221 "fast",
222 "arg2",
223 "--multvals",
224 "one",
225 "two",
226 "emacs",
227 ])
228 })
229 });
230}
231
232pub fn parse_complex2(c: &mut Criterion) {
233 c.bench_function("parse_complex2", |b| {
234 b.iter(|| {
235 create_app!().get_matches_from(vec![
236 "myprog",
237 "arg1",
238 "-f",
239 "arg2",
240 "--long-option-2",
241 "some",
242 "-O",
243 "slow",
244 "--multvalsmo",
245 "one",
246 "two",
247 "--minvals2",
248 "3",
249 "2",
250 "1",
251 ])
252 })
253 });
254}
255
256pub fn parse_args_negate_scs(c: &mut Criterion) {
257 c.bench_function("parse_args_negate_scs", |b| {
258 b.iter(|| {
259 create_app!()
260 .args_conflicts_with_subcommands(true)
261 .get_matches_from(vec![
262 "myprog",
263 "arg1",
264 "-f",
265 "arg2",
266 "--long-option-2",
267 "some",
268 "-O",
269 "slow",
270 "--multvalsmo",
271 "one",
272 "two",
273 "--minvals2",
274 "3",
275 "2",
276 "1",
277 ])
278 })
279 });
280}
281
282pub fn parse_complex_with_sc_complex(c: &mut Criterion) {
283 c.bench_function("parse_complex_with_sc_complex", |b| {
284 b.iter(|| {
285 create_app!().get_matches_from(vec!["myprog", "subcmd", "-f", "-o", "option1", "arg1"])
286 })
287 });
288}
289
290criterion_group!(
291 benches,
292 build_from_builder,
293 parse_complex,
294 parse_complex_with_flag,
295 parse_complex_with_opt,
296 parse_complex_with_pos,
297 parse_complex_with_sc,
298 parse_complex_with_sc_flag,
299 parse_complex_with_sc_opt,
300 parse_complex_with_sc_pos,
301 parse_complex1,
302 parse_complex2,
303 parse_args_negate_scs,
304 parse_complex_with_sc_complex
305);
306
307criterion_main!(benches);