trivial_argument_parser/argument/
mod.rs

1pub mod builder;
2/// Legacy API argument module. Should not be use since parsable_argument is now the preffered way of defining arguments.
3/// Most likely will be removed in future.
4pub mod legacy_argument;
5pub mod parsable_argument;
6
7/// Defines how arguments can be identified.
8#[derive(Debug)]
9pub enum ArgumentIdentification {
10    Short(char),
11    Long(String),
12    Both(char, String),
13}
14
15impl ArgumentIdentification {
16    // Check if this identification can be identified by specified single character.
17    pub fn is_by_short(&self, name: char) -> bool {
18        if let ArgumentIdentification::Short(c) = self {
19            return c == &name;
20        }
21        if let ArgumentIdentification::Both(c, _) = self {
22            return c == &name;
23        }
24        false
25    }
26
27    // Check if this identification can be identified by specified string value.
28    pub fn is_by_long(&self, name: &str) -> bool {
29        if let ArgumentIdentification::Long(s) = &self {
30            return s == name;
31        }
32        if let ArgumentIdentification::Both(_, s) = &self {
33            return s == name;
34        }
35        false
36    }
37}
38
39#[cfg(test)]
40mod test {
41    use super::ArgumentIdentification;
42
43    #[test]
44    fn is_by_short_works() {
45        let short_id = ArgumentIdentification::Short('x');
46        assert!(short_id.is_by_short('x'));
47        assert!(!short_id.is_by_short('c'));
48        let both_id = ArgumentIdentification::Both('z', String::from("directory"));
49        assert!(both_id.is_by_short('z'));
50        assert!(!both_id.is_by_short('c'));
51    }
52
53    #[test]
54    fn is_by_long_works() {
55        let short_id = ArgumentIdentification::Long(String::from("path"));
56        assert!(short_id.is_by_long("path"));
57        assert!(!short_id.is_by_long("name"));
58        let both_id = ArgumentIdentification::Both('z', String::from("file"));
59        assert!(both_id.is_by_long("file"));
60        assert!(!both_id.is_by_long("bar"));
61    }
62}