Skip to content

Commit

Permalink
refactor: Port over to arg!
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 23, 2021
1 parent 263fe30 commit 4c4a2b8
Show file tree
Hide file tree
Showing 43 changed files with 819 additions and 701 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,21 +281,21 @@ The next example shows a far less verbose method, but sacrifices some of the adv
//
// This example demonstrates clap's "usage strings" method of creating arguments
// which is less verbose
use clap::{App, Arg};
use clap::{App, arg};
fn main() {
let matches = App::new("myapp")
.version("1.0")
.author("Kevin K. <[email protected]>")
.about("Does awesome things")
.arg(Arg::from_usage("-c, --config=[FILE] 'Sets a custom config file'"))
.arg(Arg::from_usage("<INPUT> 'Sets the input file to use'"))
.arg(Arg::from_usage("-v... 'Sets the level of verbosity'"))
.arg(arg!(-c --config <FILE> "Sets a custom config file").required(false))
.arg(arg!(<INPUT> "Sets the input file to use"))
.arg(arg!(-v --verbose ... "Sets the level of verbosity"))
.subcommand(App::new("test")
.about("controls testing features")
.version("1.3")
.author("Someone E. <[email protected]>")
.arg(Arg::from_usage("-d, --debug 'Print debug information'")))
.arg(arg!(-d --debug "Print debug information")))
.get_matches();
// Same as previous example...
Expand Down
16 changes: 8 additions & 8 deletions benches/02_simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{App, Arg};
use clap::{arg, App, Arg};
use criterion::{criterion_group, criterion_main, Criterion};

macro_rules! create_app {
Expand All @@ -7,9 +7,9 @@ macro_rules! create_app {
.version("0.1")
.about("tests clap library")
.author("Kevin K. <[email protected]>")
.arg(Arg::from_usage("-f --flag 'tests flags'"))
.arg(Arg::from_usage("-o --option=[opt] 'tests options'"))
.arg(Arg::from_usage("[positional] 'tests positional'"))
.arg(arg!(-f --flag "tests flags"))
.arg(arg!(-o --option <opt> "tests options").required(false))
.arg(arg!([positional] "tests positional"))
}};
}

Expand All @@ -19,29 +19,29 @@ pub fn build_simple(c: &mut Criterion) {

pub fn build_with_flag(c: &mut Criterion) {
c.bench_function("build_with_flag", |b| {
b.iter(|| App::new("claptests").arg(Arg::from_usage("-s, --some 'something'")))
b.iter(|| App::new("claptests").arg(arg!(-s --some "something")))
});
}

pub fn build_with_flag_ref(c: &mut Criterion) {
c.bench_function("build_with_flag_ref", |b| {
b.iter(|| {
let arg = Arg::from_usage("-s, --some 'something'");
let arg = arg!(-s --some "something");
App::new("claptests").arg(&arg)
})
});
}

pub fn build_with_opt(c: &mut Criterion) {
c.bench_function("build_with_opt", |b| {
b.iter(|| App::new("claptests").arg(Arg::from_usage("-s, --some <FILE> 'something'")))
b.iter(|| App::new("claptests").arg(arg!(-s --some <FILE> "something")))
});
}

pub fn build_with_opt_ref(c: &mut Criterion) {
c.bench_function("build_with_opt_ref", |b| {
b.iter(|| {
let arg = Arg::from_usage("-s, --some <FILE> 'something'");
let arg = arg!(-s --some <FILE> "something");
App::new("claptests").arg(&arg)
})
});
Expand Down
45 changes: 43 additions & 2 deletions benches/03_complex.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
use clap::{App, AppSettings, Arg, ArgSettings};
use clap::{arg, App, AppSettings, Arg, ArgSettings};
use criterion::{criterion_group, criterion_main, Criterion};

static OPT3_VALS: [&str; 2] = ["fast", "slow"];
static POS3_VALS: [&str; 2] = ["vi", "emacs"];

macro_rules! create_app {
() => {{
App::new("claptests")
.version("0.1")
.about("tests clap library")
.author("Kevin K. <[email protected]>")
.arg(arg!(-o --option <opt> ... "tests options").required(false))
.arg(arg!([positional] "tests positionals"))
.arg(arg!(-f --flag ... "tests flags").global(true))
.args(&[
arg!(flag2: -F "tests flags with exclusions")
.conflicts_with("flag")
.requires("option2"),
arg!(option2: --"long-option-2" <option2> "tests long options with exclusions")
.required(false)
.conflicts_with("option")
.requires("positional2"),
arg!([positional2] "tests positionals with exclusions"),
arg!(-O --Option <option3> "tests options with specific value sets")
.required(false)
.possible_values(OPT3_VALS),
arg!([positional3] ... "tests positionals with specific values")
.possible_values(POS3_VALS),
arg!(--multvals "Tests multiple values not mult occs").required(false).value_names(&["one", "two"]),
arg!(
--multvalsmo "Tests multiple values, not mult occs"
).multiple_values(true).required(false).value_names(&["one", "two"]),
arg!(--minvals2 <minvals> ... "Tests 2 min vals").min_values(2).multiple_values(true).required(false),
arg!(--maxvals3 <maxvals> ... "Tests 3 max vals").max_values(3).multiple_values(true).required(false),
])
.subcommand(
App::new("subcmd")
.about("tests subcommands")
.version("0.1")
.author("Kevin K. <[email protected]>")
.arg(arg!(-o --option <scoption> ... "tests options").required(false))
.arg(arg!([scpositional] "tests positionals"))
)
}};
}

macro_rules! create_app_from_usage {
() => {{
App::new("claptests")
.version("0.1")
Expand Down Expand Up @@ -46,7 +87,7 @@ macro_rules! create_app {
}

pub fn build_from_usage(c: &mut Criterion) {
c.bench_function("build_from_usage", |b| b.iter(|| create_app!()));
c.bench_function("build_from_usage", |b| b.iter(|| create_app_from_usage!()));
}

pub fn build_from_builder(c: &mut Criterion) {
Expand Down
30 changes: 18 additions & 12 deletions benches/04_new_help.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::App;
use clap::{Arg, ArgSettings};
use clap::{arg, Arg, ArgSettings};
use criterion::{criterion_group, criterion_main, Criterion};
use std::io::Cursor;

Expand All @@ -15,15 +15,18 @@ fn app_example1<'c>() -> App<'c> {
.version("1.0")
.author("Kevin K. <[email protected]>")
.about("Does awesome things")
.arg(Arg::from_usage(
"-c, --config=[FILE] 'Sets a custom config file'",
))
.arg(Arg::from_usage("<output> 'Sets an optional output file'"))
.arg(Arg::from_usage("-d... 'Turn debugging information on'"))
.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"))
.subcommand(
App::new("test")
.about("does testing things")
.arg(Arg::from_usage("-l, --list 'lists test values'")),
.arg(arg!(-l --list "lists test values")),
)
}

Expand Down Expand Up @@ -51,11 +54,14 @@ fn app_example3<'c>() -> App<'c> {
.help("the input file to use")
.setting(ArgSettings::Required),
])
.arg(Arg::from_usage("--license 'display the license file'"))
.arg(Arg::from_usage("[output] 'Supply an output file to use'"))
.arg(Arg::from_usage(
"-i, --int=[IFACE] 'Set an interface to use'",
))
.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),
)
}

fn app_example4<'c>() -> App<'c> {
Expand Down
19 changes: 11 additions & 8 deletions examples/01a_quick_example.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{App, Arg};
use clap::{arg, App};

fn main() {
// This example shows how to create an application with several arguments using usage strings, which can be
Expand Down Expand Up @@ -33,17 +33,20 @@ fn main() {
.version("1.0")
.author("Kevin K. <[email protected]>")
.about("Does awesome things")
.arg(Arg::from_usage(
"-c, --config=[FILE] 'Sets a custom config file'",
))
.arg(Arg::from_usage("[output] 'Sets an optional output file'"))
.arg(Arg::from_usage(
"-d..., --debug... 'Turn debugging information on'",
.arg(
arg!(
-c --config <FILE> "Sets a custom config file"
)
.required(false),
)
.arg(arg!([output] "Sets an optional output file"))
.arg(arg!(
-d --debug ... "Turn debugging information on"
))
.subcommand(
App::new("test")
.about("does testing things")
.arg(Arg::from_usage("-l, --list 'lists test values'")),
.arg(arg!(-l --list "lists test values")),
)
.get_matches();

Expand Down
4 changes: 2 additions & 2 deletions examples/02_apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ fn main() {
// another option, usage(), which is an exception to the rule. This should only be used when
// the default usage string automatically generated by clap doesn't suffice.
//
// You also set all the valid arguments your App should accept via the arg(), args(), arg()
// and args_from_usage() (as well as subcommands via the subcommand() and subcommands() methods) which
// You also set all the valid arguments your App should accept via the arg() and args()
// (as well as subcommands via the subcommand() and subcommands() methods) which
// will be covered later.
//
// Once all options have been set, call one of the .get_matches* family of methods in order to
Expand Down
22 changes: 12 additions & 10 deletions examples/03_args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{App, Arg};
use clap::{arg, App, Arg};

fn main() {
// Args describe a possible valid argument which may be supplied by the user at runtime. There
Expand All @@ -9,9 +9,8 @@ fn main() {
// methods describing various settings for the individual arguments. Or by supplying a "usage"
// string. Both methods have their pros and cons.
//
// Arguments can be added to applications in two manners, one at a time with the arg(), and
// arg() method, or multiple arguments at once via a Vec<Arg> inside the args() method,
// or a single &str describing multiple Args (one per line) supplied to args_from_usage().
// Arguments can be added to applications in two manners, one at a time with the arg()
// method, or multiple arguments at once via a `&[Arg]` inside the args() method.
//
// There are various options which can be set for a given argument, some apply to any of the
// three types of arguments, some only apply one or two of the types. *NOTE* if you set
Expand Down Expand Up @@ -50,12 +49,15 @@ fn main() {
//
//
// One "Flag" using a usage string
.arg(Arg::from_usage("--license 'display the license file'"))
// Two args, one "Positional", and one "Option" using a usage string
.arg(Arg::from_usage("[output] 'Supply an output file to use'"))
.arg(Arg::from_usage(
"-i, --int=[IFACE] 'Set an interface to use'",
))
.arg(arg!(--license "display the license file"))
// Two args one Positional and one Option using a usage string
.arg(arg!([output] "Supply an output file to use"))
.arg(
arg!(
-i --int <IFACE> "Set an interface to use"
)
.required(false),
)
.get_matches();

// Here are some examples of using the arguments defined above. Keep in mind that this is only
Expand Down
13 changes: 8 additions & 5 deletions examples/05_flag_args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{App, Arg};
use clap::{arg, App, Arg};

fn main() {
// Of the three argument types, flags are the most simple. Flags are simple switches which can
Expand Down Expand Up @@ -29,10 +29,13 @@ fn main() {
// also has a conflicts_with_all(Vec<&str>)
// and an exclusive(true)
)
.arg(Arg::from_usage(
"-c, --config=[FILE] 'sets a custom config file'",
))
.arg(Arg::from_usage("[output] 'sets an output file'"))
.arg(
arg!(
-c --config <FILE> "sets a custom config file"
)
.required(false),
)
.arg(arg!([output] "sets an output file"))
.get_matches();

// We can find out whether or not awesome was used
Expand Down
13 changes: 8 additions & 5 deletions examples/07_option_args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{App, Arg};
use clap::{arg, App, Arg};

fn main() {
// Option arguments are those that take an additional value, such as "-c value". In clap they
Expand Down Expand Up @@ -33,10 +33,13 @@ fn main() {
// also has a conflicts_with_all(Vec<&str>)
// and an exclusive(true)
)
.arg(Arg::from_usage(
"-c, --config=[FILE] 'the config file to use'",
))
.arg(Arg::from_usage("[output] 'the output file to use'"))
.arg(
arg!(
-c --config <FILE> "the config file to use"
)
.required(false),
)
.arg(arg!([output] "the output file to use"))
.get_matches();

// We can find out whether or not "input" was used
Expand Down
15 changes: 9 additions & 6 deletions examples/12_typed_values.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{App, Arg};
use clap::{arg, App};

fn main() {
// You can use some convenience methods provided by clap to get typed values, so long as the
Expand All @@ -22,12 +22,15 @@ fn main() {
let matches = App::new("myapp")
// Create two arguments, a required positional which accepts multiple values
// and an optional '-l value'
.arg(Arg::from_usage(
"<seq>... 'A sequence of whole positive numbers, i.e. 20 25 30'",
))
.arg(Arg::from_usage(
"-l [len] 'A length to use, defaults to 10 when omitted'",
.arg(arg!(
<seq> ... "A sequence of whole positive numbers, i.e. 20 25 30"
))
.arg(
arg!(
l: -l <len> "A length to use, defaults to 10 when omitted"
)
.required(false),
)
.get_matches();

// This code loops through all the values provided to "seq" and adds 2
Expand Down
4 changes: 2 additions & 2 deletions examples/13_enum_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::str::FromStr;

// Add clap like normal
use clap::{App, Arg};
use clap::{arg, App};

// Define your enum
enum Vals {
Expand Down Expand Up @@ -35,7 +35,7 @@ fn main() {
let m = App::new("myapp")
// Use a single positional argument that is required
.arg(
Arg::from_usage("<type> 'The type to use'")
arg!(<type> "The type to use")
// Define the list of possible values
.possible_values(["Foo", "Bar", "Baz", "Qux"]),
)
Expand Down
Loading

0 comments on commit 4c4a2b8

Please sign in to comment.