clap/benches/04_new_help.rs

224 lines
6.2 KiB
Rust
Raw Normal View History

2022-02-12 03:48:29 +00:00
use clap::Command;
use clap::{arg, Arg};
2020-02-17 18:14:19 +00:00
use criterion::{criterion_group, criterion_main, Criterion};
use std::io::Cursor;
2022-02-14 21:47:20 +00:00
fn build_help(cmd: &mut Command) -> String {
let mut buf = Cursor::new(Vec::with_capacity(50));
2022-02-14 21:47:20 +00:00
cmd.write_help(&mut buf).unwrap();
let content = buf.into_inner();
String::from_utf8(content).unwrap()
}
2022-02-12 03:48:29 +00:00
fn app_example1<'c>() -> Command<'c> {
Command::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(
2022-02-12 03:48:29 +00:00
Command::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
)
}
2022-02-12 03:48:29 +00:00
fn app_example2<'c>() -> Command<'c> {
Command::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
}
2022-02-12 03:48:29 +00:00
fn app_example3<'c>() -> Command<'c> {
Command::new("MyApp")
2018-01-25 04:05:05 +00:00
.arg(
Arg::new("debug")
.help("turn on debugging information")
.short('d'),
2018-01-25 04:05:05 +00:00
)
.args(&[
Arg::new("config")
.help("sets the config file to use")
.takes_value(true)
.short('c')
2018-01-25 04:05:05 +00:00
.long("config"),
Arg::new("input")
.help("the input file to use")
.required(true),
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),
)
}
2022-02-12 03:48:29 +00:00
fn app_example4<'c>() -> Command<'c> {
Command::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")
.help("turn on debugging information")
.short('d')
2018-01-25 04:05:05 +00:00
.long("debug"),
)
.arg(
Arg::new("config")
.help("sets the config file to use")
.short('c')
2018-01-25 04:05:05 +00:00
.long("config"),
)
.arg(
Arg::new("input")
.help("the input file to use")
2018-01-25 04:05:05 +00:00
.index(1)
.required(true),
2018-01-25 04:05:05 +00:00
)
}
2022-02-12 03:48:29 +00:00
fn app_example5<'c>() -> Command<'c> {
Command::new("MyApp").arg(
Arg::new("awesome")
.help("turns up the awesome")
.short('a')
2018-01-25 04:05:05 +00:00
.long("awesome")
.multiple_occurrences(true),
2018-01-25 04:05:05 +00:00
)
}
2022-02-12 03:48:29 +00:00
fn app_example6<'c>() -> Command<'c> {
Command::new("MyApp")
2018-01-25 04:05:05 +00:00
.arg(
Arg::new("input")
.help("the input file to use")
2018-01-25 04:05:05 +00:00
.index(1)
.requires("config")
.required(true),
2018-01-25 04:05:05 +00:00
)
.arg(Arg::new("config").help("the config file to use").index(2))
}
2022-02-12 03:48:29 +00:00
fn app_example7<'c>() -> Command<'c> {
Command::new("MyApp")
.arg(Arg::new("config"))
.arg(Arg::new("output"))
2018-01-25 04:05:05 +00:00
.arg(
Arg::new("input")
.help("the input file to use")
.takes_value(true)
.multiple_values(true)
.multiple_occurrences(true)
.required(true)
.short('i')
2018-01-25 04:05:05 +00:00
.long("input")
.requires("config")
.conflicts_with("output"),
)
}
2022-02-12 03:48:29 +00:00
fn app_example8<'c>() -> Command<'c> {
Command::new("MyApp")
.arg(Arg::new("config"))
.arg(Arg::new("output"))
2018-01-25 04:05:05 +00:00
.arg(
Arg::new("input")
.help("the input file to use")
.takes_value(true)
.multiple_values(true)
.multiple_occurrences(true)
.required(true)
.short('i')
2018-01-25 04:05:05 +00:00
.long("input")
.requires("config")
.conflicts_with("output"),
)
}
2022-02-12 03:48:29 +00:00
fn app_example10<'c>() -> Command<'c> {
Command::new("myapp").about("does awesome things").arg(
Arg::new("CONFIG")
.help("The config file to use (default is \"config.json\")")
.short('c')
.takes_value(true),
2018-01-25 04:05:05 +00:00
)
}
2020-02-17 18:14:19 +00:00
pub fn example1(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example1();
c.bench_function("example1", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example2(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example2();
c.bench_function("example2", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example3(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example3();
c.bench_function("example3", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example4(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example4();
c.bench_function("example4", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example5(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example5();
c.bench_function("example5", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example6(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example6();
c.bench_function("example6", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example7(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example7();
c.bench_function("example7", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example8(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example8();
c.bench_function("example8", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example10(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example10();
c.bench_function("example10", |b| b.iter(|| build_help(&mut cmd)));
}
2020-02-17 18:14:19 +00:00
pub fn example4_template(c: &mut Criterion) {
2022-02-14 21:47:20 +00:00
let mut cmd = app_example4().help_template("{bin} {version}\n{author}\n{about}\n\nUSAGE:\n {usage}\n\nOPTIONS:\n{options}\n\nARGS:\n{args}\n");
c.bench_function("example4_template", |b| b.iter(|| build_help(&mut cmd)));
2020-02-17 18:14:19 +00:00
}
criterion_group!(
benches,
example1,
example2,
example3,
example4,
example5,
example6,
example7,
example8,
example10,
example4_template
);
criterion_main!(benches);