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
use {Arguments, Options};
pub struct Parser {
_private: (),
}
impl Parser {
#[inline]
pub fn new() -> Parser {
Parser { _private: () }
}
pub fn parse<I: Iterator<Item=String>>(&self, mut stream: I) -> Result<Arguments, String> {
let mut arguments = Arguments {
program: match stream.next() {
Some(program) => String::from(program),
_ => raise!("expected at least the name of the executed program"),
},
options: Options::new(),
orphans: Vec::new(),
};
let mut previous: Option<String> = None;
for chunk in stream {
if chunk.starts_with("--") {
if let Some(name) = previous {
arguments.options.set(&name, true);
}
if chunk.len() == 2 {
raise!("expected a name right after “--”");
}
previous = Some(String::from(&chunk[2..]));
} else if let Some(name) = previous {
arguments.options.set(&name, String::from(chunk));
previous = None;
} else {
arguments.orphans.push(chunk);
}
}
if let Some(name) = previous {
arguments.options.set(&name, true);
}
Ok(arguments)
}
}
#[cfg(test)]
mod tests {
use Arguments;
use super::Parser;
macro_rules! strings(
($($str:expr),*) => (
[$($str),*].iter().map(|s| s.to_string()).collect::<Vec<_>>()
);
);
#[test]
fn orphans() {
let args = strings!["a", "b", "--c", "d", "e", "--f"];
let args = args.iter().map(|a| a.to_string());
let Arguments { orphans, .. } = Parser::new().parse(args).unwrap();
assert_eq!(orphans, strings!["b", "e"]);
}
}