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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use super::argument::*;

use std::{env, process};
use std::collections::HashMap;

/// Main parser struct.
pub struct ArgParser {
    /// Name of the program.
    name: String,
    /// Name of the author.
    author: String,
    /// Version of the program.
    version: String,
    /// Copyright (if any)
    copyright: String,
    /// Description/info on the program.
    info: String,
    /// Usage (defaults to "{} [flags] [options]", name)
    usage: String,
    args: HashMap<String, Arg>,
    pub extra: Vec<String>,
    /// Prints help and exits if no args are passed when parsing.
    require_args: bool,
}

impl ArgParser {
    /// Parses std::env::args().
    pub fn parse(&mut self) -> &mut Self {
        let mut args: Vec<_> = env::args().collect();
        args.remove(0);
        self.parse_vec(args);
        self
    }

    /// Parses a given Vec<String>.
    pub fn parse_vec(&mut self, args: Vec<String>) -> &mut Self {
        if args.len() == 0 && self.require_args {
            self.print_help();
            process::exit(1);
        }

        let mut skip_indexes = Vec::new();
        for (idx, arg) in args.iter().enumerate() {
            if let Some(arg) = self.args.get_mut(arg) {
                if let ArgType::Word(w) = arg.clone().typ {
                    match w {
                        WordType::Boolean(boolean) => {arg.word(WordType::Boolean(!boolean));},
                        WordType::String_(_) => {
                            let next = args.get(idx + 1);
                            if let Some(next) = next {
                                if !next.starts_with('-') {
                                    arg.word(WordType::String_(next.clone()));
                                    arg.set();
                                    skip_indexes.push(idx + 1);
                                }
                            }
                        },
                    }
                }
                continue;
            } else if let Some(arg) = arg.strip_prefix("--") {
                if arg == "help" {self.help_exit()}
                else if arg == "version" {self.version_exit()}

                if let Some(arg) = self.args.get_mut(arg) {
                    match arg.typ {
                        ArgType::Flag(boolean) => {arg.flag(!boolean);arg.set();},
                        ArgType::Option_(_) => {
                            if let Some(next) = args.get(idx + 1) {
                                if !next.starts_with('-') {
                                    arg.option(&next);
                                    arg.set();
                                    skip_indexes.push(idx + 1);
                                }
                            }
                        },
                        _ => {},
                    }
                } else {
                    self.unexpected(&format!("--{}", arg));
                }

            } else if let Some(arg) = arg.strip_prefix('-') {
                arg.chars().into_iter().for_each(|ch| {
                    if ch == 'h' {self.help_exit()}
                    else if ch == 'v' {self.version_exit()}

                    for arg in self.args.values_mut() {
                        if arg.short == ch {
                            match arg.typ {
                                ArgType::Flag(boolean) => {arg.flag(!boolean);arg.set();},
                                ArgType::Option_(_) => {
                                    if let Some(next) = args.get(idx + 1) {
                                        if !next.starts_with('-') {
                                            arg.option(&next);
                                            arg.set();
                                            skip_indexes.push(idx + 1);
                                        } else {
                                            println!("{}", arg.name);
                                            // Couldn't do this here because multiple mutable calls
                                            // to self
                                            //self.unexpected(next);

                                            eprintln!("Unexpected argument: \"{}\"", next);
                                            self.print_help();
                                            process::exit(1);
                                        }
                                    }
                                },
                                _ => {},
                            }
                        }
                    }
                });
            } else {
                if !skip_indexes.contains(&idx) {
                    self.extra.push(String::from(arg));
                }
            }
        }

        self.args.iter().for_each(|(_, arg)| {
            if arg.required && !arg.set {
                println!("Didn't find \"{}\"\n", arg.name);
                self.help_exit();
            }
        });

        self
    }

    /// Gets an option argument's output by name.
    pub fn get_option(&self, name: &str) -> Option<String> {
        if let Some(arg) = self.args.get(name) {
            if let ArgType::Option_(string) = arg.clone().typ {
                return Some(string);
            }
        }
        None
    }

    /// Gets a flag argument's output by name.
    pub fn get_flag(&self, name: &str) -> Option<bool> {
        if let Some(arg) = self.args.get(name) {
            if let ArgType::Flag(boolean) = arg.typ {
                return Some(boolean);
            }
        }
        None
    }

    /// Gets a word argument's output by name.
    pub fn get_word(&self, name: &str) -> Option<WordType> {
        if let Some(arg) = self.args.get(name) {
            if let ArgType::Word(wt) = arg.clone().typ {
                return Some(wt);
            }
        }
        None
    }

    /// Creates a new ArgParser with `name` &str.
    pub fn new(name: &str) -> Self {
        let mut s = Self {
            name: String::from(name),
            author: String::new(),
            version: String::new(),
            copyright: String::new(),
            info: String::new(),
            usage: format!("{} [flags] [options]", name),
            args: HashMap::new(),
            extra: Vec::new(),
            require_args: false,
        };

        s.args(vec!(
            Arg::new("help")
                .short('h')
                .help("Prints the help dialog")
                .flag(false),
            Arg::new("version")
                .short('v')
                .help("Prints the version")
                .flag(false),
        ));
        s
    }

    /// Prints the help dialog.
    pub fn print_help(&self) {
        println!("{} {}\n{}\n{}\n{}", self.name, self.version, self.author, self.info, self.copyright);
        println!("\nUsage:\n\t{}", self.usage);

        let flags: Vec<&Arg> = self.args
            .iter()
            .filter(|(_, arg)| if let ArgType::Flag(_) = arg.typ {true} else {false})
            .map(|(_, arg)| arg)
            .collect();

        let options: Vec<&Arg> = self.args
            .iter()
            .filter(|(_, arg)| if let ArgType::Option_(_) = arg.typ {true} else {false})
            .map(|(_, arg)| arg)
            .collect();

        let words: Vec<&Arg> = self.args
            .iter()
            .filter(|(_, arg)| if let ArgType::Word(_) = arg.typ {true} else {false})
            .map(|(_, arg)| arg)
            .collect();

        if !flags.is_empty() {
            println!("\nFlags:");
            flags.iter().for_each(|flag| {
                println!("\t-{}, --{}\t{}", flag.short, flag.name, flag.help);
            });
        }

        if !options.is_empty() {
            println!("\nOptions:");
            options.iter().for_each(|opt| {
                println!("\t-{}, --{}\t{}", opt.short, opt.name, opt.help);
            });
        }

        if !words.is_empty() {
            println!("\nWords:");
            words.iter().for_each(|opt| {
                println!("\t{}\t{}", opt.name, opt.help);
            });
        }
    }

    /// Sets the name of the program.
    pub fn name(&mut self, name: &str) -> &mut Self {
        self.name = String::from(name);
        self
    }

    /// Sets the name of the author of the program.
    pub fn author(&mut self, author: &str) -> &mut Self {
        self.author = String::from(author);
        self
    }

    /// Sets the version of the program.
    pub fn version(&mut self, version: &str) -> &mut Self {
        self.version = String::from(version);
        self
    }

    /// Sets the copyright (if any) of the program.
    pub fn copyright(&mut self, copyright: &str) -> &mut Self {
        self.copyright = String::from(copyright);
        self
    }

    /// Sets the info of the program.
    pub fn info(&mut self, info: &str) -> &mut Self {
        self.info = String::from(info);
        self
    }

    /// Sets the usage of the program.
    pub fn usage(&mut self, usage: &str) -> &mut Self {
        self.usage = String::from(usage);
        self
    }

    /// Gives the parser `args` Vec<&mut Arg>.
    pub fn args(&mut self, args: Vec<&mut Arg>) -> &mut Self {
        for arg in args {
            match arg.typ {
                ArgType::Unknown => panic!("No Args can have type Unknown!"),
                _ => {self.args.insert(arg.name.clone(), arg.clone());}
            }

        }
        self
    }

    /// Sets whether or not the program should exit when no arguments are passed.
    pub fn require_args(&mut self, require: bool) -> &mut Self {
        self.require_args = require;
        self
    }

    fn help_exit(&self) {
        self.print_help();
        process::exit(1);
    }

    fn version_exit(&self) {
        println!("{} {}", self.name, self.version);
        process::exit(1);
    }

    fn unexpected(&self, arg: &str) {
        eprintln!("Unexpected argument: \"{}\"", arg);
        self.help_exit();
    }
}