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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use super::Level;
use clap::Clap;
use std::str::FromStr;
#[derive(Default, Clap, Debug)]
#[clap(version = "0.1.1")]
pub struct Args {
#[clap(long = "name")]
quiz_name: Option<String>,
#[clap(long = "id")]
quiz_id: Option<u64>,
#[clap(short, long)]
random: bool,
#[clap(short)]
interact: bool,
#[clap(short, long)]
level: Option<String>,
#[clap(short, long = "code")]
code_snippet: Option<String>,
#[clap(long = "temp-str")]
temp_str: Option<String>,
}
impl Args {
pub fn name(&self) -> &Option<String> {
&self.quiz_name
}
pub fn if_random(&self) -> bool {
self.random
}
pub fn if_interact(&self) -> bool {
self.interact
}
pub fn quiz_id(&self) -> &Option<u64> {
&self.quiz_id
}
pub fn level(&self) -> Option<Level> {
match self.level {
Some(ref s) => Level::from_str(s).map_or_else(
|e| {
println!("{:?}", e.to_string());
None
},
|a| Some(a),
),
None => None,
}
}
pub fn if_show_code_snippet(&self) -> &Option<String> {
&self.code_snippet
}
pub fn template(&self) -> &Option<String> {
&self.temp_str
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_level_parse() {
let a = Args::parse_from(["a", "-l", "e"]);
dbg!(a);
}
}