-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsubcommands.rs
More file actions
41 lines (39 loc) · 1.11 KB
/
subcommands.rs
File metadata and controls
41 lines (39 loc) · 1.11 KB
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
use args::{
Argument,
ArgumentParser,
ArgumentType,
};
fn main() {
// root
let root =
ArgumentParser::new("tool")
.with_description("Demo with subcommands: serve/build")
.with_subcommand(ArgumentParser::new("serve").with_argument(
Argument::new("--port").with_type(ArgumentType::Integer),
))
.with_subcommand(ArgumentParser::new("build").with_argument(
Argument::new("--release").with_type(ArgumentType::Boolean),
));
let args: Vec<String> = std::env::args().collect();
let usage = root.usage();
match root.parse(&args) {
Ok(parsed) => {
if let Some((name, sub)) = parsed.subcommand() {
match name {
"serve" => {
let port = sub.get_i64("--port").unwrap_or(8080);
println!("serving on :{}", port);
}
"build" => {
let rel = sub.get_bool("--release").unwrap_or(false);
println!("building (release={})", rel);
}
_ => {}
}
} else {
eprintln!("No subcommand provided.\n{}", usage);
}
}
Err(e) => eprintln!("{}", e),
}
}