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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
use crate::*;
pub struct NamedSwitchQuery<'a> {
/// The borrowed switchboard instance.
pub switchboard: &'a mut Switchboard,
/// Long switch name.
pub name: String,
/// Short switch name.
pub short: Option<char>,
/// The default value if the switch has been provided with no value.
pub quick: Option<String>,
/// Documentation string.
pub doc: Option<String>,
/// True if this query has already been committed.
pub committed: bool,
}
impl NamedSwitchQuery<'_> {
pub fn doc(mut self, doc: &str) -> Self {
self.doc = Some(doc.to_string());
self
}
pub fn short(mut self, short: char) -> Self {
if !short.is_ascii_alphanumeric() {
fatal!("Invalid short name for argument: {short:?}");
}
self.short = Some(short);
self
}
pub fn quick(mut self, value: &str) -> Self {
self.quick = Some(value.to_string());
self
}
pub fn optional(&mut self) {
let value = self.get_value();
self.insert(value);
}
pub fn default(&mut self, default: &str) {
match self.get_value() {
Some(value) => self.insert(Some(value)),
None => self.insert(Some(Some(default.to_string()))),
};
}
pub fn required(&mut self) {
match self.get_value() {
Some(value) => self.insert(Some(value)),
None => {
let error = SwitchboardError::MissingNamed(self.debug_name());
self.switchboard.errors.push(error);
}
}
}
fn insert(&mut self, value: Option<Option<String>>) {
let queried = QueriedValue {
doc: self.doc.clone(),
name: self.name.clone(),
variant: QueryVariant::Named(self.debug_name()),
value,
};
if let Some(_) = self.switchboard.values.insert(self.name.clone(), queried) {
error!("Duplicate query for name {:?}", self.name.clone());
}
self.committed = true;
}
fn get_value(&mut self) -> Option<Option<String>> {
// Find all matches first.
let mut matches = Vec::new();
for (i, switch) in self.switchboard.switches.iter().enumerate() {
match &switch.0 {
SwitchName::Short(other) => if let Some(short) = self.short {
if short == *other { matches.push(i) }
}
SwitchName::Long(other) => if self.name == *other {
matches.push(i);
},
};
}
// Return a value.
match matches.len() {
0 => None,
1 => {
let found = self.switchboard.switches.remove(matches[0]).1;
match found {
Some(string) => Some(Some(string)),
None => Some(self.quick.clone()),
}
}
_ => {
let error = SwitchboardError::Repeated(self.debug_name());
self.switchboard.errors.push(error);
None
}
}
}
fn debug_name(&self) -> String {
let mut debug_name = format!("--{}", self.name);
if let Some(short) = self.short {
debug_name.push_str(&format!(" (-{})", short));
}
debug_name
}
}
impl Drop for NamedSwitchQuery<'_> {
fn drop(&mut self) {
if !self.committed {
self.optional();
}
}
}
pub struct PositionalSwitchQuery<'a> {
/// The borrowed switchboard instance.
pub switchboard: &'a mut Switchboard,
/// The display name of this argument.
pub name: String,
/// Documentation string.
pub doc: Option<String>,
/// True if this query has already been committed.
pub committed: bool,
}
impl PositionalSwitchQuery<'_> {
pub fn doc(mut self, doc: &str) -> Self {
self.doc = Some(doc.to_string());
self
}
pub fn optional(&mut self) {
let value = self.switchboard.pop();
self.insert(value);
}
pub fn default(&mut self, default: &str) {
match self.switchboard.pop() {
Some(value) => self.insert(Some(value)),
None => self.insert(Some(default.to_string())),
};
}
pub fn required(&mut self) {
match self.switchboard.pop() {
Some(value) => self.insert(Some(value)),
None => {
let error = SwitchboardError::MissingPositional(self.name.clone());
self.switchboard.errors.push(error);
}
}
}
fn insert(&mut self, value: Option<String>) {
let queried = QueriedValue {
doc: self.doc.clone(),
name: self.name.clone(),
variant: QueryVariant::Positional(self.switchboard.i),
value: Some(value),
};
if let Some(_) = self.switchboard.values.insert(self.name.clone(), queried) {
error!("Duplicate query for name {:?}", self.name.clone());
}
self.committed = true;
}
}
impl Drop for PositionalSwitchQuery<'_> {
fn drop(&mut self) {
if !self.committed {
self.optional();
}
}
}
|