2016-04-01 06:00:11 +00:00
|
|
|
use clap::App;
|
2021-11-19 20:33:11 +00:00
|
|
|
use clap::{arg, Arg, ArgSettings};
|
2020-02-17 18:14:19 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, Criterion};
|
2020-02-07 06:17:42 +00:00
|
|
|
use std::io::Cursor;
|
2016-04-01 06:00:11 +00:00
|
|
|
|
2018-01-26 03:57:18 +00:00
|
|
|
fn build_help(app: &mut App) -> String {
|
2016-04-01 06:00:11 +00:00
|
|
|
let mut buf = Cursor::new(Vec::with_capacity(50));
|
|
|
|
app.write_help(&mut buf).unwrap();
|
|
|
|
let content = buf.into_inner();
|
|
|
|
String::from_utf8(content).unwrap()
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example1<'c>() -> App<'c> {
|
2016-04-01 06:00:11 +00:00
|
|
|
App::new("MyApp")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.about("Does awesome things")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(
|
|
|
|
-c --config <FILE> "Sets a custom config file"
|
|
|
|
)
|
|
|
|
.required(false),
|
|
|
|
)
|
|
|
|
.arg(arg!(<output> "Sets an optional output file"))
|
|
|
|
.arg(arg!(d: -d ... "Turn debugging information on"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.subcommand(
|
2018-10-19 20:42:13 +00:00
|
|
|
App::new("test")
|
2018-01-25 04:05:05 +00:00
|
|
|
.about("does testing things")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-l --list "lists test values")),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example2<'c>() -> App<'c> {
|
2016-04-01 06:00:11 +00:00
|
|
|
App::new("MyApp")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.about("Does awesome things")
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example3<'c>() -> App<'c> {
|
2016-04-01 06:00:11 +00:00
|
|
|
App::new("MyApp")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("debug")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("turn on debugging information")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('d'),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
|
|
|
.args(&[
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("config")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("sets the config file to use")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('c')
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("the input file to use")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::Required),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--license "display the license file"))
|
|
|
|
.arg(arg!([output] "Supply an output file to use"))
|
|
|
|
.arg(
|
|
|
|
arg!(
|
|
|
|
-i --int <IFACE> "Set an interface to use"
|
|
|
|
)
|
|
|
|
.required(false),
|
|
|
|
)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example4<'c>() -> App<'c> {
|
2016-04-01 06:00:11 +00:00
|
|
|
App::new("MyApp")
|
|
|
|
.about("Parses an input file to do awesome things")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("debug")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("turn on debugging information")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('d')
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("debug"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("config")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("sets the config file to use")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('c')
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("the input file to use")
|
2018-01-25 04:05:05 +00:00
|
|
|
.index(1)
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::Required),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example5<'c>() -> App<'c> {
|
2018-01-25 04:05:05 +00:00
|
|
|
App::new("MyApp").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("awesome")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("turns up the awesome")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('a')
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("awesome")
|
2021-11-22 22:09:39 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example6<'c>() -> App<'c> {
|
2016-04-01 06:00:11 +00:00
|
|
|
App::new("MyApp")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("the input file to use")
|
2018-01-25 04:05:05 +00:00
|
|
|
.index(1)
|
|
|
|
.requires("config")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::Required),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2021-11-18 16:17:15 +00:00
|
|
|
.arg(Arg::new("config").help("the config file to use").index(2))
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example7<'c>() -> App<'c> {
|
2016-04-01 06:00:11 +00:00
|
|
|
App::new("MyApp")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("config"))
|
|
|
|
.arg(Arg::new("output"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("the input file to use")
|
2021-02-24 15:07:57 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2021-02-11 00:12:36 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
|
|
|
.setting(ArgSettings::Required)
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('i')
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("input")
|
|
|
|
.requires("config")
|
|
|
|
.conflicts_with("output"),
|
|
|
|
)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example8<'c>() -> App<'c> {
|
2016-04-01 06:00:11 +00:00
|
|
|
App::new("MyApp")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("config"))
|
|
|
|
.arg(Arg::new("output"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("the input file to use")
|
2021-02-24 15:07:57 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2021-02-11 00:12:36 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
|
|
|
.setting(ArgSettings::Required)
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('i')
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("input")
|
|
|
|
.requires("config")
|
|
|
|
.conflicts_with("output"),
|
|
|
|
)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:34:35 +00:00
|
|
|
fn app_example10<'c>() -> App<'c> {
|
2018-01-25 04:05:05 +00:00
|
|
|
App::new("myapp").about("does awesome things").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("CONFIG")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("The config file to use (default is \"config.json\")")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('c')
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::TakesValue),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example1(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example1();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example1", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example2(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example2();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example2", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example3(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example3();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example3", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example4(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example4();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example4", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example5(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example5();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example5", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example6(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example6();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example6", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example7(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example7();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example7", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example8(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example8();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example8", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example10(c: &mut Criterion) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example10();
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example10", |b| b.iter(|| build_help(&mut app)));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-04-03 04:20:55 +00:00
|
|
|
|
2020-02-17 18:14:19 +00:00
|
|
|
pub fn example4_template(c: &mut Criterion) {
|
2021-10-11 22:01:33 +00:00
|
|
|
let mut app = app_example4().help_template("{bin} {version}\n{author}\n{about}\n\nUSAGE:\n {usage}\n\nOPTIONS:\n{options}\n\nARGS:\n{args}\n");
|
2020-02-17 18:14:19 +00:00
|
|
|
c.bench_function("example4_template", |b| b.iter(|| build_help(&mut app)));
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(
|
|
|
|
benches,
|
|
|
|
example1,
|
|
|
|
example2,
|
|
|
|
example3,
|
|
|
|
example4,
|
|
|
|
example5,
|
|
|
|
example6,
|
|
|
|
example7,
|
|
|
|
example8,
|
|
|
|
example10,
|
|
|
|
example4_template
|
|
|
|
);
|
|
|
|
|
|
|
|
criterion_main!(benches);
|