2016-04-01 06:00:11 +00:00
|
|
|
#![feature(test)]
|
|
|
|
|
|
|
|
extern crate clap;
|
|
|
|
extern crate test;
|
|
|
|
|
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use clap::App;
|
2018-11-14 17:05:06 +00:00
|
|
|
use clap::{Arg, ArgSettings};
|
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")
|
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(
|
2018-10-19 20:42:13 +00:00
|
|
|
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
|
|
|
)
|
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(
|
|
|
|
Arg::with_name("debug")
|
|
|
|
.help("turn on debugging information")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('d'),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
|
|
|
.args(&[
|
|
|
|
Arg::with_name("config")
|
|
|
|
.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"),
|
|
|
|
Arg::with_name("input")
|
|
|
|
.help("the input file to use")
|
|
|
|
.index(1)
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::Required),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +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'")
|
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(
|
|
|
|
Arg::with_name("debug")
|
|
|
|
.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(
|
|
|
|
Arg::with_name("config")
|
|
|
|
.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(
|
|
|
|
Arg::with_name("input")
|
|
|
|
.help("the input file to use")
|
|
|
|
.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(
|
|
|
|
Arg::with_name("awesome")
|
|
|
|
.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")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2018-01-25 04:05:05 +00:00
|
|
|
.requires("config")
|
|
|
|
.conflicts_with("output"),
|
|
|
|
)
|
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(
|
|
|
|
Arg::with_name("input")
|
|
|
|
.help("the input file to use")
|
|
|
|
.index(1)
|
|
|
|
.requires("config")
|
|
|
|
.conflicts_with("output")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::Required),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("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")
|
2017-02-25 17:34:42 +00:00
|
|
|
.arg(Arg::with_name("config"))
|
|
|
|
.arg(Arg::with_name("output"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("input")
|
|
|
|
.help("the input file to use")
|
2018-01-31 20:14:43 +00:00
|
|
|
.settings(&[
|
|
|
|
ArgSettings::MultipleValues,
|
|
|
|
ArgSettings::MultipleOccurrences,
|
|
|
|
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")
|
2017-02-25 17:34:42 +00:00
|
|
|
.arg(Arg::with_name("config"))
|
|
|
|
.arg(Arg::with_name("output"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("input")
|
|
|
|
.help("the input file to use")
|
2018-01-31 20:14:43 +00:00
|
|
|
.settings(&[
|
|
|
|
ArgSettings::MultipleValues,
|
|
|
|
ArgSettings::MultipleOccurrences,
|
|
|
|
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(
|
|
|
|
Arg::with_name("CONFIG")
|
2017-02-25 17:34:42 +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
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example1(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example1();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example2(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example2();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example3(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example3();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example4(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example4();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example5(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example5();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example6(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example6();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example7(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example7();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example8(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example8();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-04-10 00:35:24 +00:00
|
|
|
fn example10(b: &mut Bencher) {
|
2018-01-26 03:57:18 +00:00
|
|
|
let mut app = app_example10();
|
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-04-03 04:20:55 +00:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn example4_template(b: &mut Bencher) {
|
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");
|
2018-01-26 03:57:18 +00:00
|
|
|
b.iter(|| build_help(&mut app));
|
2016-04-03 04:20:55 +00:00
|
|
|
}
|