1 use clap::Command;
2 use clap::{arg, Arg, ArgAction};
3 use criterion::{criterion_group, criterion_main, Criterion};
4
build_help(cmd: &mut Command) -> String5 fn build_help(cmd: &mut Command) -> String {
6 let help = cmd.render_help();
7 help.to_string()
8 }
9
app_example1() -> Command10 fn app_example1() -> Command {
11 Command::new("MyApp")
12 .version("1.0")
13 .author("Kevin K. <kbknapp@gmail.com>")
14 .about("Does awesome things")
15 .arg(
16 arg!(
17 -c --config <FILE> "Sets a custom config file"
18 )
19 .required(false),
20 )
21 .arg(arg!(<output> "Sets an optional output file"))
22 .arg(arg!(d: -d ... "Turn debugging information on"))
23 .subcommand(
24 Command::new("test")
25 .about("does testing things")
26 .arg(arg!(-l --list "lists test values")),
27 )
28 }
29
app_example2() -> Command30 fn app_example2() -> Command {
31 Command::new("MyApp")
32 .version("1.0")
33 .author("Kevin K. <kbknapp@gmail.com>")
34 .about("Does awesome things")
35 }
36
app_example3() -> Command37 fn app_example3() -> Command {
38 Command::new("MyApp")
39 .arg(
40 Arg::new("debug")
41 .help("turn on debugging information")
42 .short('d')
43 .action(ArgAction::SetTrue),
44 )
45 .args([
46 Arg::new("config")
47 .help("sets the config file to use")
48 .action(ArgAction::Set)
49 .short('c')
50 .long("config"),
51 Arg::new("input")
52 .help("the input file to use")
53 .required(true),
54 ])
55 .arg(arg!(--license "display the license file"))
56 .arg(arg!([output] "Supply an output file to use"))
57 .arg(
58 arg!(
59 -i --int <IFACE> "Set an interface to use"
60 )
61 .required(false),
62 )
63 }
64
app_example4() -> Command65 fn app_example4() -> Command {
66 Command::new("MyApp")
67 .about("Parses an input file to do awesome things")
68 .version("1.0")
69 .author("Kevin K. <kbknapp@gmail.com>")
70 .arg(
71 Arg::new("debug")
72 .help("turn on debugging information")
73 .short('d')
74 .action(ArgAction::SetTrue)
75 .long("debug"),
76 )
77 .arg(
78 Arg::new("config")
79 .help("sets the config file to use")
80 .short('c')
81 .long("config"),
82 )
83 .arg(
84 Arg::new("input")
85 .help("the input file to use")
86 .index(1)
87 .required(true),
88 )
89 }
90
app_example5() -> Command91 fn app_example5() -> Command {
92 Command::new("MyApp").arg(
93 Arg::new("awesome")
94 .help("turns up the awesome")
95 .short('a')
96 .long("awesome")
97 .action(ArgAction::Count),
98 )
99 }
100
app_example6() -> Command101 fn app_example6() -> Command {
102 Command::new("MyApp")
103 .arg(
104 Arg::new("input")
105 .help("the input file to use")
106 .index(1)
107 .requires("config")
108 .required(true),
109 )
110 .arg(Arg::new("config").help("the config file to use").index(2))
111 }
112
app_example7() -> Command113 fn app_example7() -> Command {
114 Command::new("MyApp")
115 .arg(Arg::new("config"))
116 .arg(Arg::new("output"))
117 .arg(
118 Arg::new("input")
119 .help("the input file to use")
120 .num_args(1..)
121 .action(ArgAction::Append)
122 .required(true)
123 .short('i')
124 .long("input")
125 .requires("config")
126 .conflicts_with("output"),
127 )
128 }
129
app_example8() -> Command130 fn app_example8() -> Command {
131 Command::new("MyApp")
132 .arg(Arg::new("config"))
133 .arg(Arg::new("output"))
134 .arg(
135 Arg::new("input")
136 .help("the input file to use")
137 .num_args(1..)
138 .action(ArgAction::Append)
139 .required(true)
140 .short('i')
141 .long("input")
142 .requires("config")
143 .conflicts_with("output"),
144 )
145 }
146
app_example10() -> Command147 fn app_example10() -> Command {
148 Command::new("myapp").about("does awesome things").arg(
149 Arg::new("CONFIG")
150 .help("The config file to use (default is \"config.json\")")
151 .short('c')
152 .action(ArgAction::Set),
153 )
154 }
155
example1(c: &mut Criterion)156 pub fn example1(c: &mut Criterion) {
157 let mut cmd = app_example1();
158 c.bench_function("example1", |b| b.iter(|| build_help(&mut cmd)));
159 }
160
example2(c: &mut Criterion)161 pub fn example2(c: &mut Criterion) {
162 let mut cmd = app_example2();
163 c.bench_function("example2", |b| b.iter(|| build_help(&mut cmd)));
164 }
165
example3(c: &mut Criterion)166 pub fn example3(c: &mut Criterion) {
167 let mut cmd = app_example3();
168 c.bench_function("example3", |b| b.iter(|| build_help(&mut cmd)));
169 }
170
example4(c: &mut Criterion)171 pub fn example4(c: &mut Criterion) {
172 let mut cmd = app_example4();
173 c.bench_function("example4", |b| b.iter(|| build_help(&mut cmd)));
174 }
175
example5(c: &mut Criterion)176 pub fn example5(c: &mut Criterion) {
177 let mut cmd = app_example5();
178 c.bench_function("example5", |b| b.iter(|| build_help(&mut cmd)));
179 }
180
example6(c: &mut Criterion)181 pub fn example6(c: &mut Criterion) {
182 let mut cmd = app_example6();
183 c.bench_function("example6", |b| b.iter(|| build_help(&mut cmd)));
184 }
185
example7(c: &mut Criterion)186 pub fn example7(c: &mut Criterion) {
187 let mut cmd = app_example7();
188 c.bench_function("example7", |b| b.iter(|| build_help(&mut cmd)));
189 }
190
example8(c: &mut Criterion)191 pub fn example8(c: &mut Criterion) {
192 let mut cmd = app_example8();
193 c.bench_function("example8", |b| b.iter(|| build_help(&mut cmd)));
194 }
195
example10(c: &mut Criterion)196 pub fn example10(c: &mut Criterion) {
197 let mut cmd = app_example10();
198 c.bench_function("example10", |b| b.iter(|| build_help(&mut cmd)));
199 }
200
example4_template(c: &mut Criterion)201 pub fn example4_template(c: &mut Criterion) {
202 let mut cmd = app_example4().help_template("{name} {version}\n{author}\n{about}\n\nUSAGE:\n {usage}\n\nOPTIONS:\n{options}\n\nARGS:\n{args}\n");
203 c.bench_function("example4_template", |b| b.iter(|| build_help(&mut cmd)));
204 }
205
206 criterion_group!(
207 benches,
208 example1,
209 example2,
210 example3,
211 example4,
212 example5,
213 example6,
214 example7,
215 example8,
216 example10,
217 example4_template
218 );
219
220 criterion_main!(benches);
221