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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::collections::HashSet;
use std::collections::HashMap;
use std::str::FromStr;
use std::any::Any;
#[derive(Default, Debug)]
pub struct ParsingResults {
path: String,
flags: HashSet<String>,
named_params: HashMap<String, Box<Any>>,
unnamed_params: Vec<String>,
}
impl ParsingResults {
pub fn new() -> ParsingResults {
ParsingResults {
path: String::new(),
flags: HashSet::<String>::new(),
named_params: HashMap::<String, Box<Any>>::new(),
unnamed_params: Vec::<String>::new(),
}
}
pub fn path(&self) -> &String {
&self.path
}
pub fn path_mut(&mut self) -> &mut String {
&mut self.path
}
pub fn flags(&self) -> &HashSet<String> {
&self.flags
}
pub fn flags_mut(&mut self) -> &mut HashSet<String> {
&mut self.flags
}
pub fn named_params(&self) -> &HashMap<String, Box<Any>> {
&self.named_params
}
pub fn named_params_mut(&mut self) -> &mut HashMap<String, Box<Any>> {
&mut self.named_params
}
pub fn unnamed_params(&self) -> &Vec<String> {
&self.unnamed_params
}
pub fn unnamed_params_mut(&mut self) -> &mut Vec<String> {
&mut self.unnamed_params
}
pub fn flag(&self, name: &str) -> bool {
self.flags.contains(name)
}
pub fn named_param<T: 'static + FromStr>(&self, name: &str) -> Option<&T> {
match self.named_params.get(name) {
Some(value) => {
if !value.is::<T>() {
panic!("clargs: specified type does not match the specified parameter's associated type");
}
Some(value.downcast_ref::<T>().unwrap())
},
None => None,
}
}
pub fn named_param_mut<T: 'static + FromStr>(&mut self, name: &str) -> Option<&mut T> {
match self.named_params.get_mut(name) {
Some(value) => {
if !value.is::<T>() {
panic!("clargs: specified type does not match the specified parameter's associated type");
}
Some(value.downcast_mut::<T>().unwrap())
},
None => None,
}
}
pub fn num_unnamed_params(&self) -> usize {
self.unnamed_params.len()
}
pub fn unnamed_param(&self, index: usize) -> &String {
if index >= self.unnamed_params.len() {
panic!("clargs: attempted to access a non-existant unnamed parameter");
}
&self.unnamed_params[index]
}
pub fn unnamed_param_mut(&mut self, index: usize) -> &mut String {
if index >= self.unnamed_params.len() {
panic!("clargs: attempted to access a non-existant unnamed parameter");
}
&mut self.unnamed_params[index]
}
}