-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathcrate_clap.rs
48 lines (42 loc) · 1.37 KB
/
crate_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
48
use clap::{Arg, Command};
/// HOW TO RUN
/// It adds crate_clap to the [[test]] section in Cargo.toml with harness = false option.
/// Therefore, you can run the main function with the --test option.
///
/// $ cargo test --test crate_clap -- --userid 1 --productid asdfasdf
/// Finished `test` profile [unoptimized + debuginfo] target(s) in 0.02s
/// Running tests/crate_clap.rs (target/debug/deps/crate_clap-2038d8ca9fcd5e3a)
/// Hello, world!
/// User ID: 1
///
fn main() {
println!("Hello, world!");
let command = Command::new("serial")
.version("0.1.0")
.about("Serial number generator")
.arg(
Arg::new("userid")
.long("userid")
.help("Sets the user ID")
.required(false),
)
.arg(
Arg::new("productid")
.long("productid")
.help("Sets the product ID")
.required(false),
);
let command = command.arg(
Arg::new("expiredate")
.long("expiredate")
.help("Sets the expire date"),
);
let matches = command.get_matches();
// add expiredate
if let Some(userid) = matches.get_one::<String>("userid") {
println!("User ID: {}", userid);
}
if let Some(expiredate) = matches.get_one::<String>("expiredate") {
println!("Expire date: {}", expiredate);
}
}