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
use lexing::errors::LexicalError;
use parsing::Argument;

pub mod lexing;
pub mod parsing;
pub mod tests;

pub struct CommandArguments {
    arguments: Vec<parsing::Argument>,
}

impl CommandArguments {
    pub fn new(arguments: Vec<parsing::Argument>) -> CommandArguments {
        CommandArguments { arguments }
    }

    pub fn from_input(input: &str) -> Result<CommandArguments, LexicalError> {
        let tokens = lexing::tokenise(String::from(input));
        let arguments = parsing::parse(tokens?);
        Ok(CommandArguments::new(arguments))
    }

    pub fn default_new() -> Result<CommandArguments, LexicalError> {
        let raw_arguments = std::env::args().collect::<Vec<String>>().join(" ");
        let tokens = lexing::tokenise(raw_arguments);
        let arguments = parsing::parse(tokens?);
        Ok(CommandArguments::new(arguments))
    }

    pub fn get_named(&self, name: &str) -> Option<&str> {
        self.arguments
            .iter()
            .find(
                |it|
                    it.get_name().is_some() &&
                    it.get_name().unwrap() == name &&
                    it.get_value().is_some()
            )
            .map(|it| it.get_value().unwrap())
    }

    pub fn get_positional(&self, position: usize) -> Option<&str> {
        self.arguments
            .iter()
            .find(|it| it.get_position().is_some() && it.get_position().unwrap() == position)
            .map(|it| it.get_value().unwrap())
    }

    pub fn get_positional_count(&self) -> usize {
        self.arguments
            .iter()
            .filter(|it| it.get_position().is_some())
            .count()
    }

    pub fn get_positional_values(&self) -> Vec<&str> {
        self.arguments
            .iter()
            .filter(|it| it.get_position().is_some())
            .map(|it| it.get_value().unwrap())
            .collect()
    }

    pub fn get_named_count(&self) -> usize {
        self.arguments
            .iter()
            .filter(|it| it.get_name().is_some())
            .count()
    }

    pub fn get_named_values(&self) -> Vec<&str> {
        self.arguments
            .iter()
            .filter(|it| it.get_name().is_some())
            .map(|it| it.get_value().unwrap())
            .collect()
    }

    pub fn get_all(&self) -> Vec<Argument> {
        self.arguments.to_vec()
    }

    pub fn get_flag_names(&self) -> Vec<&str> {
        self.arguments
            .iter()
            .filter(|it| it.get_name().is_some() && it.get_value().is_none())
            .map(|it| it.get_name().unwrap())
            .collect()
    }

    pub fn get_flags(&self) -> Vec<Argument> {
        self.arguments
            .iter()
            .filter(|it| it.get_name().is_some() && it.get_value().is_none())
            .cloned()
            .collect()
    }

    pub fn has_flag(&self, name: &str) -> bool {
        self.arguments
            .iter()
            .any(
                |it|
                    it.get_name().is_some() &&
                    it.get_name().unwrap() == name &&
                    it.get_value().is_none()
            )
    }

    pub fn has_named(&self, name: &str) -> bool {
        self.arguments
            .iter()
            .any(
                |it|
                    it.get_name().is_some() &&
                    it.get_name().unwrap() == name &&
                    it.get_value().is_some()
            )
    }

    pub fn has_positional(&self, position: usize) -> bool {
        self.arguments
            .iter()
            .any(|it| it.get_position().is_some() && it.get_position().unwrap() == position)
    }
}