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
use {Arguments, Error, Options, Result};

/// A parser for command-line arguments.
pub struct Parser {
    _private: (),
}

impl Parser {
    /// Create a new parser.
    #[inline]
    pub fn new() -> Parser {
        Parser { _private: () }
    }

    /// Parse command-line arguments.
    pub fn parse<I: Iterator<Item=String>>(&self, mut stream: I) -> Result<Arguments> {
        let mut arguments = Arguments {
            program: match stream.next() {
                Some(ref program) if !program.starts_with("--") => String::from(&program[..]),
                _ => raise!("expected a name as the first argument"),
            },
            options: Options::new(),
            orphans: Vec::new(),
        };

        let mut previous: Option<String> = None;

        macro_rules! set_boolean_if_any(
            () => (
                if let Some(ref name) = previous {
                    if name.starts_with("no-") {
                        if name.len() == 3 {
                            raise!("expected a name right after “--no-”");
                        }
                        arguments.options.set(&name[3..], "false".to_string());
                    } else {
                        arguments.options.set(name, "true".to_string());
                    }
                }
            );
        );

        for chunk in stream {
            if chunk.starts_with("--") {
                set_boolean_if_any!();
                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);
            }
        }
        set_boolean_if_any!();

        Ok(arguments)
    }
}

#[cfg(test)]
mod tests {
    use Arguments;
    use super::Parser;

    macro_rules! strings(
        ($slices:expr) => ($slices.iter().map(|s| s.to_string()));
    );

    #[test]
    fn program() {
        let arguments = vec!["--a", "--b"];
        match Parser::new().parse(strings!(arguments)) {
            Ok(_) => unreachable!(),
            Err(_) => {},
        }
    }

    #[test]
    fn booleans() {
        let arguments = vec!["a", "--b", "--no-c", "--d"];
        let arguments = Parser::new().parse(strings!(arguments)).unwrap();
        assert_eq!(arguments.get::<bool>("b").unwrap(), true);
        assert_eq!(arguments.get::<bool>("c").unwrap(), false);
        assert_eq!(arguments.get::<bool>("d").unwrap(), true);
    }

    #[test]
    fn orphans() {
        let arguments = vec!["a", "b", "--c", "d", "e", "--f"];
        let Arguments { orphans, .. } = Parser::new().parse(strings!(arguments)).unwrap();
        assert_eq!(&orphans, &["b", "e"]);
    }
}