2015-08-30 04:03:11 +00:00
|
|
|
#![feature(test)]
|
|
|
|
|
2015-09-10 00:52:04 +00:00
|
|
|
#[macro_use]
|
2015-08-30 04:03:11 +00:00
|
|
|
extern crate clap;
|
|
|
|
extern crate test;
|
|
|
|
|
2018-01-31 20:14:43 +00:00
|
|
|
use clap::{App, AppSettings, Arg, SubCommand, ArgSettings};
|
2015-08-30 04:03:11 +00:00
|
|
|
|
|
|
|
use test::Bencher;
|
|
|
|
|
2015-08-30 18:14:42 +00:00
|
|
|
static ARGS: &'static str = "-o --option=[opt]... 'tests options'
|
|
|
|
[positional] 'tests positionals'";
|
|
|
|
static OPT3_VALS: [&'static str; 2] = ["fast", "slow"];
|
|
|
|
static POS3_VALS: [&'static str; 2] = ["vi", "emacs"];
|
|
|
|
|
|
|
|
macro_rules! create_app {
|
|
|
|
() => ({
|
|
|
|
App::new("claptests")
|
|
|
|
.version("0.1")
|
|
|
|
.about("tests clap library")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.args_from_usage(ARGS)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f --flag... 'tests flags'")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::Global))
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("option2"),
|
|
|
|
Arg::from("--long-option-2 [option2] 'tests long options with exclusions'").conflicts_with("option").requires("positional2"),
|
|
|
|
Arg::from("[positional2] 'tests positionals with exclusions'"),
|
|
|
|
Arg::from("-O --Option [option3] 'tests options with specific value sets'").possible_values(&OPT3_VALS),
|
|
|
|
Arg::from("[positional3]... 'tests positionals with specific values'").possible_values(&POS3_VALS),
|
|
|
|
Arg::from("--multvals [one] [two] 'Tests mutliple values, not mult occs'"),
|
|
|
|
Arg::from("--multvalsmo... [one] [two] 'Tests mutliple values, not mult occs'"),
|
|
|
|
Arg::from("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
|
|
|
|
Arg::from("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3)
|
2015-08-30 18:14:42 +00:00
|
|
|
])
|
|
|
|
.subcommand(SubCommand::with_name("subcmd")
|
|
|
|
.about("tests subcommands")
|
|
|
|
.version("0.1")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.arg_from_usage("-o --option [scoption]... 'tests options'")
|
|
|
|
.arg_from_usage("[scpositional] 'tests positionals'"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-30 04:03:11 +00:00
|
|
|
#[bench]
|
2018-01-25 04:05:05 +00:00
|
|
|
fn create_app_from_usage(b: &mut Bencher) { b.iter(|| create_app!()); }
|
2015-08-30 04:03:11 +00:00
|
|
|
|
2015-09-10 00:52:04 +00:00
|
|
|
#[bench]
|
|
|
|
fn create_app_builder(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
App::new("claptests")
|
2018-01-25 04:05:05 +00:00
|
|
|
.version("0.1")
|
|
|
|
.about("tests clap library")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("opt")
|
2015-09-10 00:52:04 +00:00
|
|
|
.help("tests options")
|
|
|
|
.short("o")
|
|
|
|
.long("option")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("positional")
|
2015-09-10 00:52:04 +00:00
|
|
|
.help("tests positionals")
|
2018-01-25 04:05:05 +00:00
|
|
|
.index(1),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("flag")
|
|
|
|
.short("f")
|
|
|
|
.help("tests flags")
|
|
|
|
.long("flag")
|
2018-01-31 20:14:43 +00:00
|
|
|
.settings(&[ArgSettings::MultipleOccurrences, ArgSettings::Global]),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("flag2")
|
2015-09-10 00:52:04 +00:00
|
|
|
.short("F")
|
|
|
|
.help("tests flags with exclusions")
|
|
|
|
.conflicts_with("flag")
|
2018-01-25 04:05:05 +00:00
|
|
|
.requires("option2"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("option2")
|
2015-09-10 00:52:04 +00:00
|
|
|
.help("tests long options with exclusions")
|
|
|
|
.conflicts_with("option")
|
|
|
|
.requires("positional2")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("long-option-2"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("positional2")
|
2015-09-10 00:52:04 +00:00
|
|
|
.index(3)
|
2018-01-25 04:05:05 +00:00
|
|
|
.help("tests positionals with exclusions"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("option3")
|
2015-09-10 00:52:04 +00:00
|
|
|
.short("O")
|
|
|
|
.long("Option")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2015-09-10 00:52:04 +00:00
|
|
|
.help("tests options with specific value sets")
|
2018-01-25 04:05:05 +00:00
|
|
|
.possible_values(&OPT3_VALS),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("positional3")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2015-09-10 00:52:04 +00:00
|
|
|
.help("tests positionals with specific values")
|
|
|
|
.index(4)
|
2018-01-25 04:05:05 +00:00
|
|
|
.possible_values(&POS3_VALS),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("multvals")
|
2015-09-10 00:52:04 +00:00
|
|
|
.long("multvals")
|
|
|
|
.help("Tests mutliple values, not mult occs")
|
2018-01-25 04:05:05 +00:00
|
|
|
.value_names(&["one", "two"]),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("multvalsmo")
|
2015-09-10 00:52:04 +00:00
|
|
|
.long("multvalsmo")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2015-09-10 00:52:04 +00:00
|
|
|
.help("Tests mutliple values, not mult occs")
|
2018-01-25 04:05:05 +00:00
|
|
|
.value_names(&["one", "two"]),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("minvals")
|
2015-09-10 00:52:04 +00:00
|
|
|
.long("minvals2")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2015-09-10 00:52:04 +00:00
|
|
|
.help("Tests 2 min vals")
|
2018-01-25 04:05:05 +00:00
|
|
|
.min_values(2),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("maxvals")
|
2015-09-10 00:52:04 +00:00
|
|
|
.long("maxvals3")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2015-09-10 00:52:04 +00:00
|
|
|
.help("Tests 3 max vals")
|
2018-01-25 04:05:05 +00:00
|
|
|
.max_values(3),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("subcmd")
|
2015-09-10 00:52:04 +00:00
|
|
|
.about("tests subcommands")
|
|
|
|
.version("0.1")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("scoption")
|
|
|
|
.short("o")
|
|
|
|
.long("option")
|
2018-01-31 20:14:43 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2018-01-25 04:05:05 +00:00
|
|
|
.help("tests options"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("scpositional")
|
|
|
|
.index(1)
|
|
|
|
.help("tests positionals"),
|
|
|
|
),
|
|
|
|
);
|
2015-09-10 00:52:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-15 21:37:59 +00:00
|
|
|
#[bench]
|
2015-09-10 12:23:58 +00:00
|
|
|
fn create_app_macros(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg opt: -o --option +takes_value ... "tests options")
|
|
|
|
(@arg positional: index(1) "tests positionals")
|
|
|
|
(@arg flag: -f --flag ... +global "tests flags")
|
|
|
|
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
|
|
"tests flags with exclusions")
|
|
|
|
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
|
|
|
|
"tests long options with exclusions")
|
|
|
|
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
|
|
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
|
|
"tests options with specific value sets")
|
|
|
|
(@arg positional3: index(3) ... possible_value[vi emacs]
|
|
|
|
"tests positionals with specific values")
|
|
|
|
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
|
|
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
|
|
(@subcommand subcmd =>
|
|
|
|
(about: "tests subcommands")
|
|
|
|
(version: "0.1")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
|
|
(@arg scpositional: index(1) "tests positionals"))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2015-09-10 00:52:04 +00:00
|
|
|
|
2015-08-30 04:03:11 +00:00
|
|
|
#[bench]
|
2018-01-25 04:05:05 +00:00
|
|
|
fn parse_clean(b: &mut Bencher) { b.iter(|| create_app!().get_matches_from(vec![""])); }
|
2015-08-30 04:03:11 +00:00
|
|
|
|
2015-08-30 18:46:53 +00:00
|
|
|
#[bench]
|
2018-01-25 04:05:05 +00:00
|
|
|
fn parse_flag(b: &mut Bencher) { b.iter(|| create_app!().get_matches_from(vec!["myprog", "-f"])); }
|
2015-08-30 18:46:53 +00:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_option(b: &mut Bencher) {
|
2016-01-21 05:18:53 +00:00
|
|
|
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1"]));
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_positional(b: &mut Bencher) {
|
2016-01-21 05:18:53 +00:00
|
|
|
b.iter(|| create_app!().get_matches_from(vec!["myprog", "arg1"]));
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_sc_clean(b: &mut Bencher) {
|
2016-01-21 05:18:53 +00:00
|
|
|
b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd"]));
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_sc_flag(b: &mut Bencher) {
|
2016-01-21 05:18:53 +00:00
|
|
|
b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "-f"]));
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_sc_option(b: &mut Bencher) {
|
2016-01-21 05:18:53 +00:00
|
|
|
b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "-o", "option1"]));
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_sc_positional(b: &mut Bencher) {
|
2016-01-21 05:18:53 +00:00
|
|
|
b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "arg1"]));
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_complex1(b: &mut Bencher) {
|
2018-01-25 04:05:05 +00:00
|
|
|
b.iter(|| {
|
|
|
|
create_app!().get_matches_from(vec![
|
|
|
|
"myprog",
|
|
|
|
"-ff",
|
|
|
|
"-o",
|
|
|
|
"option1",
|
|
|
|
"arg1",
|
|
|
|
"-O",
|
|
|
|
"fast",
|
|
|
|
"arg2",
|
|
|
|
"--multvals",
|
|
|
|
"one",
|
|
|
|
"two",
|
|
|
|
"emacs",
|
|
|
|
])
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_complex2(b: &mut Bencher) {
|
2018-01-25 04:05:05 +00:00
|
|
|
b.iter(|| {
|
|
|
|
create_app!().get_matches_from(vec![
|
|
|
|
"myprog",
|
|
|
|
"arg1",
|
|
|
|
"-f",
|
|
|
|
"arg2",
|
|
|
|
"--long-option-2",
|
|
|
|
"some",
|
|
|
|
"-O",
|
|
|
|
"slow",
|
|
|
|
"--multvalsmo",
|
|
|
|
"one",
|
|
|
|
"two",
|
|
|
|
"--minvals2",
|
|
|
|
"3",
|
|
|
|
"2",
|
|
|
|
"1",
|
|
|
|
])
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 19:38:17 +00:00
|
|
|
#[bench]
|
|
|
|
fn parse_complex2_with_args_negate_scs(b: &mut Bencher) {
|
2018-01-25 04:05:05 +00:00
|
|
|
b.iter(|| {
|
|
|
|
create_app!()
|
|
|
|
.setting(AppSettings::ArgsNegateSubcommands)
|
|
|
|
.get_matches_from(vec![
|
|
|
|
"myprog",
|
|
|
|
"arg1",
|
|
|
|
"-f",
|
|
|
|
"arg2",
|
|
|
|
"--long-option-2",
|
|
|
|
"some",
|
|
|
|
"-O",
|
|
|
|
"slow",
|
|
|
|
"--multvalsmo",
|
|
|
|
"one",
|
|
|
|
"two",
|
|
|
|
"--minvals2",
|
|
|
|
"3",
|
|
|
|
"2",
|
|
|
|
"1",
|
|
|
|
])
|
|
|
|
});
|
2017-01-02 19:38:17 +00:00
|
|
|
}
|
2015-08-30 18:46:53 +00:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parse_sc_complex(b: &mut Bencher) {
|
2018-01-25 04:05:05 +00:00
|
|
|
b.iter(|| {
|
|
|
|
create_app!().get_matches_from(vec!["myprog", "subcmd", "-f", "-o", "option1", "arg1"])
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|