-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.rs
72 lines (67 loc) · 1.89 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use clap::{value_parser, Arg, Command};
#[derive(Debug)]
struct AppArgs {
number: u32,
opt_number: Option<u32>,
width: u32,
input: Vec<std::path::PathBuf>,
}
fn parse_width(s: &str) -> Result<u32, String> {
let w = s.parse().map_err(|_| "not a number")?;
if w != 0 {
Ok(w)
} else {
Err("width must be positive".to_string())
}
}
fn main() {
let matches = Command::new("App")
.arg(
Arg::new("number")
.long("number")
.required(true)
.help("Sets a number")
.value_parser(value_parser!(u32)),
)
.arg(
Arg::new("opt-number")
.long("opt-number")
.help("Sets an optional number")
.value_parser(value_parser!(u32)),
)
.arg(
Arg::new("width")
.long("width")
.default_value("10")
.value_parser(parse_width)
.help("Sets width"),
)
.arg(
Arg::new("INPUT")
.num_args(1..)
.value_parser(value_parser!(std::path::PathBuf)),
)
.get_matches();
let args = AppArgs {
number: *matches.get_one::<u32>("number").unwrap(),
opt_number: matches.get_one::<u32>("opt-number").cloned(),
width: matches.get_one::<u32>("width").cloned().unwrap(),
input: matches
.get_many::<std::path::PathBuf>("INPUT")
.unwrap_or_default()
.cloned()
.collect(),
};
#[cfg(debug_assertions)]
{
println!("{:#?}", args.number);
println!("{:#?}", args.opt_number);
println!("{:#?}", args.width);
if 10 < args.input.len() {
println!("{:#?}", args.input.len());
} else {
println!("{:#?}", args);
}
}
std::hint::black_box(args);
}