clap/benches/04_new_help.rs

220 lines
6.2 KiB
Rust
Raw Normal View History

use clap::App;
2018-11-14 17:05:06 +00:00
use clap::{Arg, ArgSettings};
2020-02-17 18:14:19 +00:00
use criterion::{criterion_group, criterion_main, Criterion};
use std::io::Cursor;
2018-01-26 03:57:18 +00:00
fn build_help(app: &mut App) -> String {
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()
}
fn app_example1<'c>() -> App<'c> {
App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
2018-11-14 17:05:06 +00:00
.arg("-c, --config=[FILE] 'Sets a custom config file'")
2018-09-01 17:42:31 +00:00
.arg("<output> 'Sets an optional output file'")
.arg("-d... 'Turn debugging information on'")
2018-01-25 04:05:05 +00:00
.subcommand(
App::new("test")
2018-01-25 04:05:05 +00:00
.about("does testing things")
2018-09-01 17:42:31 +00:00
.arg("-l, --list 'lists test values'"),
2018-01-25 04:05:05 +00:00
)
}
fn app_example2<'c>() -> App<'c> {
App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
}
fn app_example3<'c>() -> App<'c> {
App::new("MyApp")
2018-01-25 04:05:05 +00:00
.arg(
Arg::new("debug")
.about("turn on debugging information")
.short('d'),
2018-01-25 04:05:05 +00:00
)
.args(&[
Arg::new("config")
.about("sets the config file to use")
.setting(ArgSettings::TakesValue)
.short('c')
2018-01-25 04:05:05 +00:00
.long("config"),
Arg::new("input")
.about("the input file to use")
2018-01-25 04:05:05 +00:00
.index(1)
.setting(ArgSettings::Required),
2018-01-25 04:05:05 +00:00
])
.arg("--license 'display the license file'")
2018-11-14 03:16:02 +00:00
.arg("[output] 'Supply an output file to use'")
.arg("-i, --int=[IFACE] 'Set an interface to use'")
}
fn app_example4<'c>() -> App<'c> {
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(
Arg::new("debug")
.about("turn on debugging information")
.short('d')
2018-01-25 04:05:05 +00:00
.long("debug"),
)
.arg(
Arg::new("config")
.about("sets the config file to use")
.short('c')
2018-01-25 04:05:05 +00:00
.long("config"),
)
.arg(
Arg::new("input")
.about("the input file to use")
2018-01-25 04:05:05 +00:00
.index(1)
.setting(ArgSettings::Required),
2018-01-25 04:05:05 +00:00
)
}
fn app_example5<'c>() -> App<'c> {
2018-01-25 04:05:05 +00:00
App::new("MyApp").arg(
Arg::new("awesome")
.about("turns up the awesome")
.short('a')
2018-01-25 04:05:05 +00:00
.long("awesome")
.setting(ArgSettings::MultipleOccurrences)
2018-01-25 04:05:05 +00:00
.requires("config")
.conflicts_with("output"),
)
}
fn app_example6<'c>() -> App<'c> {
App::new("MyApp")
2018-01-25 04:05:05 +00:00
.arg(
Arg::new("input")
.about("the input file to use")
2018-01-25 04:05:05 +00:00
.index(1)
.requires("config")
.conflicts_with("output")
.setting(ArgSettings::Required),
2018-01-25 04:05:05 +00:00
)
.arg(Arg::new("config").about("the config file to use").index(2))
}
fn app_example7<'c>() -> App<'c> {
App::new("MyApp")
.arg(Arg::new("config"))
.arg(Arg::new("output"))
2018-01-25 04:05:05 +00:00
.arg(
Arg::new("input")
.about("the input file to use")
.settings(&[
ArgSettings::MultipleValues,
ArgSettings::MultipleOccurrences,
ArgSettings::Required,
])
.short('i')
2018-01-25 04:05:05 +00:00
.long("input")
.requires("config")
.conflicts_with("output"),
)
}
fn app_example8<'c>() -> App<'c> {
App::new("MyApp")
.arg(Arg::new("config"))
.arg(Arg::new("output"))
2018-01-25 04:05:05 +00:00
.arg(
Arg::new("input")
.about("the input file to use")
.settings(&[
ArgSettings::MultipleValues,
ArgSettings::MultipleOccurrences,
ArgSettings::Required,
])
.short('i')
2018-01-25 04:05:05 +00:00
.long("input")
.requires("config")
.conflicts_with("output"),
)
}
fn app_example10<'c>() -> App<'c> {
2018-01-25 04:05:05 +00:00
App::new("myapp").about("does awesome things").arg(
Arg::new("CONFIG")
.about("The config file to use (default is \"config.json\")")
.short('c')
.setting(ArgSettings::TakesValue),
2018-01-25 04:05:05 +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)));
}
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)));
}
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)));
}
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)));
}
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)));
}
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)));
}
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)));
}
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)));
}
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)));
}
2020-02-17 18:14:19 +00:00
pub fn example4_template(c: &mut Criterion) {
2018-11-14 03:16:02 +00:00
let mut app = app_example4().help_template("{bin} {version}\n{author}\n{about}\n\nUSAGE:\n {usage}\n\nFLAGS:\n{flags}\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);