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
87
88
89
90
91
92
93
94
95
96
97
98
use std::option::Option as Optional;
use std::string::String;
#[derive(Clone,Debug)]
pub struct Option {
pub name: String,
pub help: String,
pub short_name: Optional<char>,
pub default_value: Optional<String>,
pub is_optional: bool,
pub is_flag: bool,
}
impl Option {
pub fn required(name: &str,
help: &str,
short_name: Optional<char>,
default_value: Optional<&str>)
-> Option {
Option {
name: name.to_string(),
help: help.to_string(),
short_name: short_name,
default_value: default_value.map(|dv| dv.to_string()),
is_optional: false,
is_flag: false,
}
}
pub fn optional(name: &str, help: &str, short_name: Optional<char>) -> Option {
Option {
name: name.to_string(),
help: help.to_string(),
short_name: short_name,
default_value: None,
is_optional: true,
is_flag: false,
}
}
pub fn flag(name: &str, help: &str, short_name: Optional<char>) -> Option {
Option {
name: name.to_string(),
help: help.to_string(),
short_name: short_name,
default_value: None,
is_optional: false,
is_flag: true,
}
}
}
pub fn find_option<'a, I>(options: I, name: &str) -> Optional<&'a Option>
where I: Iterator<Item = &'a Option>
{
let mut result: Optional<&'a Option> = None;
for o in options {
if o.name == name {
result = Some(o);
break;
} else if let Some(sn) = o.short_name {
if name.starts_with(sn) {
result = result.or_else(|| Some(o));
}
}
}
result
}