Skip to content

Commit 4c4a2b8

Browse files
committed
refactor: Port over to arg!
1 parent 263fe30 commit 4c4a2b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+819
-701
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,21 @@ The next example shows a far less verbose method, but sacrifices some of the adv
281281
//
282282
// This example demonstrates clap's "usage strings" method of creating arguments
283283
// which is less verbose
284-
use clap::{App, Arg};
284+
use clap::{App, arg};
285285
286286
fn main() {
287287
let matches = App::new("myapp")
288288
.version("1.0")
289289
.author("Kevin K. <kbknapp@gmail.com>")
290290
.about("Does awesome things")
291-
.arg(Arg::from_usage("-c, --config=[FILE] 'Sets a custom config file'"))
292-
.arg(Arg::from_usage("<INPUT> 'Sets the input file to use'"))
293-
.arg(Arg::from_usage("-v... 'Sets the level of verbosity'"))
291+
.arg(arg!(-c --config <FILE> "Sets a custom config file").required(false))
292+
.arg(arg!(<INPUT> "Sets the input file to use"))
293+
.arg(arg!(-v --verbose ... "Sets the level of verbosity"))
294294
.subcommand(App::new("test")
295295
.about("controls testing features")
296296
.version("1.3")
297297
.author("Someone E. <someone_else@other.com>")
298-
.arg(Arg::from_usage("-d, --debug 'Print debug information'")))
298+
.arg(arg!(-d --debug "Print debug information")))
299299
.get_matches();
300300
301301
// Same as previous example...

benches/02_simple.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{App, Arg};
1+
use clap::{arg, App, Arg};
22
use criterion::{criterion_group, criterion_main, Criterion};
33

44
macro_rules! create_app {
@@ -7,9 +7,9 @@ macro_rules! create_app {
77
.version("0.1")
88
.about("tests clap library")
99
.author("Kevin K. <kbknapp@gmail.com>")
10-
.arg(Arg::from_usage("-f --flag 'tests flags'"))
11-
.arg(Arg::from_usage("-o --option=[opt] 'tests options'"))
12-
.arg(Arg::from_usage("[positional] 'tests positional'"))
10+
.arg(arg!(-f --flag "tests flags"))
11+
.arg(arg!(-o --option <opt> "tests options").required(false))
12+
.arg(arg!([positional] "tests positional"))
1313
}};
1414
}
1515

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

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

2626
pub fn build_with_flag_ref(c: &mut Criterion) {
2727
c.bench_function("build_with_flag_ref", |b| {
2828
b.iter(|| {
29-
let arg = Arg::from_usage("-s, --some 'something'");
29+
let arg = arg!(-s --some "something");
3030
App::new("claptests").arg(&arg)
3131
})
3232
});
3333
}
3434

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

4141
pub fn build_with_opt_ref(c: &mut Criterion) {
4242
c.bench_function("build_with_opt_ref", |b| {
4343
b.iter(|| {
44-
let arg = Arg::from_usage("-s, --some <FILE> 'something'");
44+
let arg = arg!(-s --some <FILE> "something");
4545
App::new("claptests").arg(&arg)
4646
})
4747
});

benches/03_complex.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
1-
use clap::{App, AppSettings, Arg, ArgSettings};
1+
use clap::{arg, App, AppSettings, Arg, ArgSettings};
22
use criterion::{criterion_group, criterion_main, Criterion};
33

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

77
macro_rules! create_app {
8+
() => {{
9+
App::new("claptests")
10+
.version("0.1")
11+
.about("tests clap library")
12+
.author("Kevin K. <kbknapp@gmail.com>")
13+
.arg(arg!(-o --option <opt> ... "tests options").required(false))
14+
.arg(arg!([positional] "tests positionals"))
15+
.arg(arg!(-f --flag ... "tests flags").global(true))
16+
.args(&[
17+
arg!(flag2: -F "tests flags with exclusions")
18+
.conflicts_with("flag")
19+
.requires("option2"),
20+
arg!(option2: --"long-option-2" <option2> "tests long options with exclusions")
21+
.required(false)
22+
.conflicts_with("option")
23+
.requires("positional2"),
24+
arg!([positional2] "tests positionals with exclusions"),
25+
arg!(-O --Option <option3> "tests options with specific value sets")
26+
.required(false)
27+
.possible_values(OPT3_VALS),
28+
arg!([positional3] ... "tests positionals with specific values")
29+
.possible_values(POS3_VALS),
30+
arg!(--multvals "Tests multiple values not mult occs").required(false).value_names(&["one", "two"]),
31+
arg!(
32+
--multvalsmo "Tests multiple values, not mult occs"
33+
).multiple_values(true).required(false).value_names(&["one", "two"]),
34+
arg!(--minvals2 <minvals> ... "Tests 2 min vals").min_values(2).multiple_values(true).required(false),
35+
arg!(--maxvals3 <maxvals> ... "Tests 3 max vals").max_values(3).multiple_values(true).required(false),
36+
])
37+
.subcommand(
38+
App::new("subcmd")
39+
.about("tests subcommands")
40+
.version("0.1")
41+
.author("Kevin K. <kbknapp@gmail.com>")
42+
.arg(arg!(-o --option <scoption> ... "tests options").required(false))
43+
.arg(arg!([scpositional] "tests positionals"))
44+
)
45+
}};
46+
}
47+
48+
macro_rules! create_app_from_usage {
849
() => {{
950
App::new("claptests")
1051
.version("0.1")
@@ -46,7 +87,7 @@ macro_rules! create_app {
4687
}
4788

4889
pub fn build_from_usage(c: &mut Criterion) {
49-
c.bench_function("build_from_usage", |b| b.iter(|| create_app!()));
90+
c.bench_function("build_from_usage", |b| b.iter(|| create_app_from_usage!()));
5091
}
5192

5293
pub fn build_from_builder(c: &mut Criterion) {

benches/04_new_help.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::App;
2-
use clap::{Arg, ArgSettings};
2+
use clap::{arg, Arg, ArgSettings};
33
use criterion::{criterion_group, criterion_main, Criterion};
44
use std::io::Cursor;
55

@@ -15,15 +15,18 @@ fn app_example1<'c>() -> App<'c> {
1515
.version("1.0")
1616
.author("Kevin K. <kbknapp@gmail.com>")
1717
.about("Does awesome things")
18-
.arg(Arg::from_usage(
19-
"-c, --config=[FILE] 'Sets a custom config file'",
20-
))
21-
.arg(Arg::from_usage("<output> 'Sets an optional output file'"))
22-
.arg(Arg::from_usage("-d... 'Turn debugging information on'"))
18+
.arg(
19+
arg!(
20+
-c --config <FILE> "Sets a custom config file"
21+
)
22+
.required(false),
23+
)
24+
.arg(arg!(<output> "Sets an optional output file"))
25+
.arg(arg!(d: -d ... "Turn debugging information on"))
2326
.subcommand(
2427
App::new("test")
2528
.about("does testing things")
26-
.arg(Arg::from_usage("-l, --list 'lists test values'")),
29+
.arg(arg!(-l --list "lists test values")),
2730
)
2831
}
2932

@@ -51,11 +54,14 @@ fn app_example3<'c>() -> App<'c> {
5154
.help("the input file to use")
5255
.setting(ArgSettings::Required),
5356
])
54-
.arg(Arg::from_usage("--license 'display the license file'"))
55-
.arg(Arg::from_usage("[output] 'Supply an output file to use'"))
56-
.arg(Arg::from_usage(
57-
"-i, --int=[IFACE] 'Set an interface to use'",
58-
))
57+
.arg(arg!(--license "display the license file"))
58+
.arg(arg!([output] "Supply an output file to use"))
59+
.arg(
60+
arg!(
61+
-i --int <IFACE> "Set an interface to use"
62+
)
63+
.required(false),
64+
)
5965
}
6066

6167
fn app_example4<'c>() -> App<'c> {

examples/01a_quick_example.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{App, Arg};
1+
use clap::{arg, App};
22

33
fn main() {
44
// This example shows how to create an application with several arguments using usage strings, which can be
@@ -33,17 +33,20 @@ fn main() {
3333
.version("1.0")
3434
.author("Kevin K. <kbknapp@gmail.com>")
3535
.about("Does awesome things")
36-
.arg(Arg::from_usage(
37-
"-c, --config=[FILE] 'Sets a custom config file'",
38-
))
39-
.arg(Arg::from_usage("[output] 'Sets an optional output file'"))
40-
.arg(Arg::from_usage(
41-
"-d..., --debug... 'Turn debugging information on'",
36+
.arg(
37+
arg!(
38+
-c --config <FILE> "Sets a custom config file"
39+
)
40+
.required(false),
41+
)
42+
.arg(arg!([output] "Sets an optional output file"))
43+
.arg(arg!(
44+
-d --debug ... "Turn debugging information on"
4245
))
4346
.subcommand(
4447
App::new("test")
4548
.about("does testing things")
46-
.arg(Arg::from_usage("-l, --list 'lists test values'")),
49+
.arg(arg!(-l --list "lists test values")),
4750
)
4851
.get_matches();
4952

examples/02_apps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ fn main() {
99
// another option, usage(), which is an exception to the rule. This should only be used when
1010
// the default usage string automatically generated by clap doesn't suffice.
1111
//
12-
// You also set all the valid arguments your App should accept via the arg(), args(), arg()
13-
// and args_from_usage() (as well as subcommands via the subcommand() and subcommands() methods) which
12+
// You also set all the valid arguments your App should accept via the arg() and args()
13+
// (as well as subcommands via the subcommand() and subcommands() methods) which
1414
// will be covered later.
1515
//
1616
// Once all options have been set, call one of the .get_matches* family of methods in order to

examples/03_args.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{App, Arg};
1+
use clap::{arg, App, Arg};
22

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

6163
// Here are some examples of using the arguments defined above. Keep in mind that this is only

examples/05_flag_args.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{App, Arg};
1+
use clap::{arg, App, Arg};
22

33
fn main() {
44
// Of the three argument types, flags are the most simple. Flags are simple switches which can
@@ -29,10 +29,13 @@ fn main() {
2929
// also has a conflicts_with_all(Vec<&str>)
3030
// and an exclusive(true)
3131
)
32-
.arg(Arg::from_usage(
33-
"-c, --config=[FILE] 'sets a custom config file'",
34-
))
35-
.arg(Arg::from_usage("[output] 'sets an output file'"))
32+
.arg(
33+
arg!(
34+
-c --config <FILE> "sets a custom config file"
35+
)
36+
.required(false),
37+
)
38+
.arg(arg!([output] "sets an output file"))
3639
.get_matches();
3740

3841
// We can find out whether or not awesome was used

examples/07_option_args.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{App, Arg};
1+
use clap::{arg, App, Arg};
22

33
fn main() {
44
// Option arguments are those that take an additional value, such as "-c value". In clap they
@@ -33,10 +33,13 @@ fn main() {
3333
// also has a conflicts_with_all(Vec<&str>)
3434
// and an exclusive(true)
3535
)
36-
.arg(Arg::from_usage(
37-
"-c, --config=[FILE] 'the config file to use'",
38-
))
39-
.arg(Arg::from_usage("[output] 'the output file to use'"))
36+
.arg(
37+
arg!(
38+
-c --config <FILE> "the config file to use"
39+
)
40+
.required(false),
41+
)
42+
.arg(arg!([output] "the output file to use"))
4043
.get_matches();
4144

4245
// We can find out whether or not "input" was used

examples/12_typed_values.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{App, Arg};
1+
use clap::{arg, App};
22

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

3336
// This code loops through all the values provided to "seq" and adds 2

0 commit comments

Comments
 (0)