-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclap.rs
47 lines (41 loc) · 1.41 KB
/
clap.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#[macro_use]
extern crate clap;
use clap::{AppSettings, Arg, SubCommand};
fn main() {
let matches = clap::app_from_crate!()
.global_setting(AppSettings::GlobalVersion)
.arg(
Arg::with_name("environment")
.short("e")
.long("env")
.global(true)
.takes_value(true)
.value_name("STRING")
.help("Sets an environment value, defaults to \"dev\""),
)
.subcommand(
SubCommand::with_name("foo")
.about("Shows foo")
.author(clap::crate_authors!())
.arg(
Arg::with_name("debug")
.short("d")
.help("Prints debug information verbosely"),
),
)
.subcommand(
SubCommand::with_name("bar")
.about("Shows bar")
.author(clap::crate_authors!()),
)
.get_matches();
let env = matches.value_of("environment").unwrap_or("dev");
match matches.subcommand() {
("foo", Some(matches)) => {
let debug = clap::value_t!(matches, "debug", bool).unwrap_or_default();
println!("Running foo, env = {}, debug = {}", env, debug);
}
("bar", Some(_matches)) => println!("Running bar, env = {}", env),
_ => println!("No subcommand matched"),
}
}