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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::env;

#[derive(Default)]
pub struct App {
    app_path: String,
    args: Vec<Arg>,
    ops: Vec<Ops>,
    about: String,
    conjunction: String,
}

#[derive(Default)]
pub struct Ops {
    short: String,
    long: String,
    takes_value: bool,
}

#[derive(Default)]
pub struct Arg {
    ops: Option<Ops>,
    value: Option<String>,
}

impl App {

    pub fn new() -> App {
        App{
            app_path: String::default(),
            args: vec![],
            ops: vec![],
            about: String::default(),
            conjunction: String::from("=")
        }
    }


///Add necessary options
    pub fn add_ops(&mut self, ops: Ops) {
        self.ops.push(ops);
    }

    pub fn parse(&mut self) {
        let args: Vec<String> = env::args().collect();
        self.app_path = args[0].clone();
        self.args = self.matcher(args[1..].to_vec());
    }

    pub fn has_ops(&self, s: &str) -> bool {
        self.args.iter().any(|arg| 
            match &arg.ops {
                Some(o) => o.short == s || o.long == s,
                None => false,
            }
        )
    }

    pub fn take_args(&self) -> bool {
        self.args.len() >= 1
    }

    pub fn get_value(&self, ops: &str) -> Option<String> {
        match self.find_args(ops.to_string()) {
            Some(a) => a.value.clone(),
            None => None,
        }
    }

    pub fn print(&self) {
        println!("{}", self.about);
        for o in &self.ops {
            print!("\t");
            if !o.short.is_empty() {
                print!("-{} ", o.short);
            }
            if !o.long.is_empty() {
                print!("--{}", o.long);
            }
        }
    }
}

impl App {

    fn matcher(&mut self, cl_args: Vec<String>) -> Vec<Arg> {
        let mut args: Vec<Arg> = vec![];
        let mut current_ops: Vec<Ops> = vec![];
        std::mem::swap(&mut self.ops, &mut current_ops);
        for (_i, arg) in cl_args.iter().enumerate() {
            let (is_ops, name, value) = self.separate_args(arg.clone());
            let _arg: Arg;
            if is_ops {
                if let Some(i) = current_ops.iter().position(|o| o.short == name || o.long == name) {
                    args.push(
                        Arg {
                            ops: Some(current_ops.remove(i)),
                            value
                        }
                    );
                };
            } else {
                args.push(Arg {
                    ops: None,
                    value: None
                });
            };
        }

        args
    }

    fn separate_args(&self, s: String) -> (bool, String, Option<String>) {
        let split_args: Vec<&str> = s.split(&self.conjunction).collect();
        let mut is_ops = false;
        let name: String;
        let front = split_args[0];
        if front.starts_with("-") {
            is_ops = true;
            if front.starts_with("--") {
                name = front[2..].to_string();
            } else {
                name = front[1..].to_string();
            }
            let value = if split_args.len() > 1 {
                Some(split_args[1].to_string())
            } else {
                None
            };
            (is_ops, name, value)
        } else {
            name = s.clone();
            (is_ops, name, None)
        }
    }

    fn find_args(&self, s: String) -> Option<&Arg> {
        self.args.iter().find(|&arg| match &arg.ops {
            Some(o) => o.short == s || o.long == s,
            None => false
        })
    }
}

impl Ops {
    pub fn new() -> Ops {
        Ops{
            short: String::default(),
            long: String::default(),
            takes_value: false
        }
    }

    pub fn short(mut self, s: &str) -> Self {
        self.short = s.to_string();
        self
    }

    pub fn long(mut self, l: &str) -> Self {
        self.long = l.to_string();
        self
    }

    pub fn takes_value(mut self, is_takes: bool) -> Self {
        self.takes_value = is_takes;
        self
    }

}


#[cfg(test)]
mod test {

    use super::*;

    fn setup() -> App {
        let mut app = App::new();
        app.args = vec![
             Arg {
                 ops: Some(Ops::new().short("v").long("--version")),
                 value: None
             },
             Arg {
                 ops: Some(Ops::new().short("Xms").takes_value(true)),
                 value: Some("4096".to_string())
             },
             Arg {
                 ops: None,
                 value: None
             }
        ];
        app
    }

    #[test]
    fn has_ops() {
        let app = setup();
        assert!(app.has_ops("v"));
    }


    #[test]
    fn get_value() {
        let app = setup();
        assert_eq!(app.get_value("Xms"), Some("4096".to_string()));
    }

    #[test]
    fn ver() {
        let mut app = App::new();
        app.parse();
       assert!(!app.take_args());
    }

}