simple-cli-parser 0.1.1

A crate for parsing commands and arguemnts passed to the console.
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! A crate for parsing commands and arguemnts passed to the console.
//! 
//! This can parse commands with various arguments.
//! It currently only supports option arguments (or args beginning with a "-"),
//! but an update for regular params and argument rules (like ordering) will be coming soon.
//! 
//! ## Getting Started
//! To start, create a new [`Parser`] struct and add a couple of [`Arg`]s to it using the
//! [`Parser::add_arg()`] or [`Parser::add_args()`] methods.
//! Then, call the [`Parser::parse()`] method with [`std::env::Args`] passed in.
//! 
//! ```
//! use cli_parser::{ Parser, Arg };
//! use std::env;
//! 
//! fn main() {
//!     let parser = Parser::new();
//!     let my_arg = Arg::new().flag("help").short('h');
//!     parser.add_arg(my_arg);
//!     
//!     let mut args = env::args();
//! 
//!     // Don't include the first argument
//!     args.next();
//!     
//!     let hashmap = parser.parse(&mut args).unwrap();
//! 
//!     if hashmap.contains_key("help") {
//!         println!("Help argument called!");
//!     }
//! }
//! ```

use std::cell::RefCell;
use std::collections::HashMap;
use std::error::Error;
use std::fmt::Display;

#[derive(Debug)]
struct InvalidCommandError {
    reason: InvalidCommandReasons
}

impl InvalidCommandError {
    pub fn new(reason: InvalidCommandReasons) -> InvalidCommandError {
        InvalidCommandError { reason }
    }
}

impl Display for InvalidCommandError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match &self.reason {
            InvalidCommandReasons::Unexpected(s) => {
                write!(f, "Invalid command, unexpected token '{}'", s)
            },
            InvalidCommandReasons::Duplicate(s) => {
                write!(f, "Invalid command, duplicate token '{}'", s)
            },
            InvalidCommandReasons::Missing => {
                write!(f, "Invalid command, missing argument")
            }
        }
    }
}

impl Error for InvalidCommandError {
    
}

#[derive(Debug)]
enum InvalidCommandReasons {
    Unexpected(String),
    Missing,
    Duplicate(String),
}

#[derive(Clone, Debug)]
enum ArgTypes {
    Param(bool),
    Input,
    Short(char),
    None
}

/// Represents a single argument which can be passed to a [`Parser`].
/// 
/// # Example
/// ```
/// let arg = Arg::new().param("num");
/// let parser = Parser::new();
/// let mut args = std::env::args();
/// args.next();
/// 
/// parser.add_arg(arg);
/// let output = parser.parse(&mut args).unwrap();
/// ```
#[derive(Clone, Debug)]
pub struct Arg {
    name: String,
    arg_type: ArgTypes,
    expecting: bool,
}

impl Arg {
    /// Create a new arg object, note you must call further methods on this for it to be useful.
    /// 
    /// # Example
    /// ```
    /// let arg = Arg::new();
    /// ```
    pub fn new() -> Arg {
        
        Arg { name: String::new(), arg_type: ArgTypes::None, expecting: false} 
    }

    /// A parameter argument, or one that does not expect any argument to come before it.
    /// Note that the order that these are added to the parser matters.
    /// 
    /// # Example
    /// ```
    /// let arg = Arg::new().param("p1");
    /// ```
    /// `arg` is a required argument and the [`Parser::parse()`] will return an error if it is not present.
    pub fn param(self, name: &str) -> Arg {
        Arg { name: String::from(name), arg_type: ArgTypes::Param(false), expecting: false }
    }

    /// An optional argument that expects a value to follow directly after it.
    /// 
    /// # Example
    /// ```
    /// let arg = Arg::new().input("inp");
    /// ```
    /// Upon parsing, if `--inp` is one of the arguments called, `arg` will be in the output with whatever string comes next in the arguments.
    pub fn input(self, name: &str) -> Arg {
        Arg { name: String::from(name), arg_type: ArgTypes::Input, expecting: true }
    }

    /// A flag argument, or one that toggles a setting without expecting another token afterwards.
    /// 
    /// # Example
    /// ```
    /// let arg = Arg::new().flag("optional");
    /// ```
    /// Upon parsing, if `--optional` is one of the arguments called, `arg` will be in the output with the value `true`.
    pub fn flag(self, name: &str) -> Arg {
        Arg { name: String::from(name), arg_type: self.arg_type, expecting: false }
    }

    /// Sets a short option for the argument, allowing it to be called with a char rather than a string.
    /// 
    /// # Example
    /// ```
    /// let arg = Arg::new().flag("help").short('h');
    /// ```
    /// The `arg` variable can be called by `--help` or by `-h`.
    pub fn short(self, ch: char) -> Arg {
        Arg { name: self.name, arg_type: ArgTypes::Short(ch), expecting: self.expecting }
    }

    fn set_used(&mut self, used: bool) {
        self.arg_type = ArgTypes::Param(used);
    }
}

/// A struct that parses the command line for certain [`Arg`]s.
/// 
/// # Example
/// ```
/// let parser = Parser::new();
/// let arg = Arg::new().param("num");
/// let mut args = std::env::args();
/// args.next();
/// 
/// parser.add_arg(arg);
/// let output = parser.parse(&mut args).unwrap();
/// ```
pub struct Parser {
    args: RefCell<Vec<Arg>>,
}

impl Parser {

    /// Creates a new Parser struct.
    /// 
    /// # Example
    /// ```
    /// let parser = Parser::new();
    /// ```
    pub fn new() -> Parser {
        Parser { args: RefCell::new(vec![]) }
    }

    /// Adds an argument to the parser.
    /// 
    /// # Example
    /// ```
    /// let arg = Arg::new().param("num");
    /// let parser = Parser::new();
    /// 
    /// parser.add_arg(arg);
    /// ```
    pub fn add_arg(&self, arg: Arg) {
        self.args.borrow_mut().push(arg);
    }

    /// Adds a vector of arguments to the parser.
    /// 
    /// # Example
    /// ```
    /// let mut args = vec![
    ///     Arg::new().param("num1"),
    ///     Arg::new().param("num2"),
    /// ];
    /// let parser = Parser::new();
    /// 
    /// parser.add_args(args);
    /// ```
    pub fn add_args(&self, mut args: Vec<Arg>) {
        self.args.borrow_mut().append(&mut args);
    }

    /// Returns the arguments associated with this parser as a vector.
    /// 
    /// # Example
    /// ```
    /// let args = parser.args();
    /// ```
    pub fn args(&self) -> Vec<Arg> {
        self.args.borrow().clone()
    }

    /// Returns the number of arguments associated with this parser.
    /// 
    /// # Example
    /// ```
    /// let n = parser.len();
    /// ```
    pub fn len(&self) -> usize {
        self.args.borrow().len()
    }

    fn get_err(&self, reason: InvalidCommandReasons) -> Result<HashMap<String, Option<String>>, Box<dyn Error>> {
        return Err(Box::new(InvalidCommandError::new(reason)))
    }

    /// Parses through the remaining arguments and returns a hashmap of arguments passed and their relevant values.
    /// 
    /// # Example
    /// ```
    /// let parser = Parser::new();
    /// let mut args = vec![
    ///     Arg::new().param("p1"),
    ///     Arg::new().param("p2"),
    ///     Arg::new().flag("help").short('h');
    /// ];
    /// parser.add_args(args);
    /// 
    /// let mut input_args = std::env::args();
    /// input_args.next();
    /// 
    /// let hashmap = parser.parse(input_args).unwrap();
    /// println!("p1: {}, p2: {}", hashmap.get("p1"), hashmap.get("p2"));
    /// if hashmap.contains_key("help") {
    ///     println!("Help requested!");
    /// }
    /// ```
    pub fn parse(&self, args: &mut impl Iterator<Item = String>) -> Result<HashMap<String, Option<String>>, Box<dyn Error>> {
        let mut hashmap: HashMap<String, Option<String>> = HashMap::new();
        let mut prev_arg: Option<Box<Arg>> = None;
        let mut args = args.peekable();
        let mut parser_args = self.args.clone().take();

        while let Some(c_arg) = args.next() {
            if c_arg.starts_with("-") {
                // Return error if calling a new argument without providing a follow up argument to the previous one
                if prev_arg.is_some() {
                    return self.get_err(InvalidCommandReasons::Unexpected(c_arg));
                }

                if c_arg.starts_with("--") {
                    // Full arg
                    let mut found = false;
                    for arg in &parser_args {
                        if c_arg.ends_with(&arg.name) && c_arg.len() == arg.name.len() + 2 {
                            found = true;
                            if arg.expecting {
                                prev_arg = Some(Box::new(arg.clone()));
                            } else {
                                match hashmap.insert(arg.name.clone(), None) {
                                    Some(_) => return self.get_err(InvalidCommandReasons::Duplicate(c_arg)),
                                    None => {},
                                };
                                prev_arg = None;
                            }
                        }
                    }
                    if !found {
                        return self.get_err(InvalidCommandReasons::Unexpected(c_arg));
                    }
                } else {
                    // Short arg
                    let mut found = false;
                    for arg in &parser_args {
                        if let ArgTypes::Short(c) = arg.arg_type {
                            if c_arg.ends_with(c) && c_arg.len() == 2 {
                                found = true;
                                if arg.expecting {
                                    prev_arg = Some(Box::new(arg.clone()));
                                } else {
                                    match hashmap.insert(arg.name.clone(), None) {
                                        Some(_) => return self.get_err(InvalidCommandReasons::Duplicate(c_arg)),
                                        None => {},
                                    };
                                    prev_arg = None;
                                }
                            }
                        }
                    }
                    if !found {
                        return self.get_err(InvalidCommandReasons::Unexpected(c_arg));
                    }
                }
            } else {
                // non-argument token
                if prev_arg.is_none() {
                    // params
                    let mut found = false;
                    for arg in &mut parser_args {
                        if let ArgTypes::Param(used) = arg.arg_type {
                            if used {
                                continue;
                            }
                            
                            match hashmap.insert(arg.name.clone(), Some(c_arg.clone())) {
                                Some (_) => return self.get_err(InvalidCommandReasons::Duplicate(c_arg)),
                                None => {}
                            };
                            arg.set_used(true);
                            prev_arg = None;
                            found = true;
                            break;
                        }
                    }

                    if !found {
                        return self.get_err(InvalidCommandReasons::Unexpected(c_arg));
                    }
                } else {
                    match hashmap.insert(prev_arg.unwrap().name, Some(c_arg.clone())) {
                        Some (_) => return self.get_err(InvalidCommandReasons::Duplicate(c_arg)),
                        None => {}
                    }
                    prev_arg = None;
                }
            };

            if args.peek().is_none() && prev_arg.is_some() {
                return self.get_err(InvalidCommandReasons::Missing);
            }
        }

        for arg in parser_args {
            if let ArgTypes::Param(false) = arg.arg_type {
                return self.get_err(InvalidCommandReasons::Missing);
            }
        }

        Ok(hashmap)
    }
}



// Tests
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn create_arg() {
        let default = Arg::new().input("default");
        assert_eq!(default.name, "default");
        assert_eq!(default.expecting, true);
        
        let short = Arg::new().input("short").short('s');
        assert_eq!(short.name, "short");
        if let ArgTypes::Short(c) = short.arg_type {
            assert_eq!(c, 's');
        } else {
            assert!(false);
        }
        assert_eq!(short.expecting, true);

        let flag = Arg::new().flag("flag").short( 'f');
        assert_eq!(flag.expecting, false);
        if let ArgTypes::Short(c) = flag.arg_type {
            assert_eq!(c, 'f');
        } else {
            assert!(false);
        }
        assert_eq!(flag.name, "flag");

        let mut param = Arg::new().param("param");
        assert_eq!(param.expecting, false);
        if let ArgTypes::Param(used) = param.arg_type {
            assert!(!used);
        } else {
            assert!(false);
        }
        assert_eq!(param.name, "param");

        param.set_used(true);
        if let ArgTypes::Param(used) = param.arg_type {
            assert!(used);
        } else {
            assert!(false);
        }
    }

    #[test]
    fn append_args() {
        let default = Arg::new().input("default");
        let short = Arg::new().input("short").short('s');
        let flag = Arg::new().flag("flag").short( 'f');

        let parser = Parser::new();
        parser.add_arg(default);
        assert_eq!(parser.len(), 1);
        parser.add_args(vec![short, flag]);
        assert_eq!(parser.len(), 3);
        assert_eq!(parser.args()[2].name, "flag");
    }

    #[test]
    fn test_parse_err() {
        let default = Arg::new().input("default");
        let short = Arg::new().input("short").short('s');
        let flag = Arg::new().flag("flag").short( 'f');
        let param1 = Arg::new().param("p1");
        let param2 = Arg::new().param("p2");

        let parser = Parser::new();
        parser.add_args(vec![default, short, flag, param1, param2]);

        // Tests err on duplicate value
        let mut cmd = "--short short_inp -s s_inp p1 p2"
            .split_whitespace()
            .map(|s| { String::from(s) });

        let res = parser.parse(&mut cmd);
        assert!(res.is_err());
        
        // Tests err on invalid command order
        let mut cmd = "--default -f p1 p2"
            .split_whitespace()
            .map(|s| { String::from(s) });

        let res = parser.parse(&mut cmd);
        assert!(res.is_err());

        // Tests err on unexpected token
        let mut cmd = "p1 p2 -f dsjfhkjuh"
        .split_whitespace()
        .map(|s| { String::from(s) });

        let res = parser.parse(&mut cmd);
        assert!(res.is_err());

        // Tests err on missing token
        let mut cmd = "p1 p2 --default"
            .split_whitespace()
            .map(|s| { String::from(s) });

        let res = parser.parse(&mut cmd);
        assert!(res.is_err());

        // Tests err on missing param
        let mut cmd = "p1 -f"
            .split_whitespace()
            .map(|s| { String::from(s) });

        let res = parser.parse(&mut cmd);
        assert!(res.is_err());

        // Tests err on unrecognized arg
        let mut cmd = "p1 p2 --doesntexist"
            .split_whitespace()
            .map(|s| { String::from(s) });

        let res = parser.parse(&mut cmd);
        assert!(res.is_err());

    }

    #[test]
    pub fn test_parse_success() {
        let default = Arg::new().input("default");
        let short = Arg::new().input("short").short('s');
        let flag = Arg::new().flag("flag").short( 'f');
        let param1 = Arg::new().param("file");
        let param2 = Arg::new().param("path");

        let parser = Parser::new();
        parser.add_args(vec![default, short, flag, param1, param2]);

        let mut cmd = "--default def_arg filename -s s_arg -f pathname"
            .split_whitespace()
            .map(|s| { String::from(s) });

        let res = parser.parse(&mut cmd);
        assert!(res.is_ok());

        let res = res.unwrap();
        assert_eq!(res.len(), 5);
        assert!(res.contains_key("default"));
        assert_eq!(res.get("default").unwrap(), &Some(String::from("def_arg")));
        assert!(res.contains_key("short"));
        assert_eq!(res.get("short").unwrap(), &Some(String::from("s_arg")));
        assert!(res.contains_key("flag"));
        assert_eq!(res.get("flag").unwrap(), &None);
        assert!(res.contains_key("file"));
        assert_eq!(res.get("file").unwrap(), &Some(String::from("filename")));
        assert!(res.contains_key("path"));
        assert_eq!(res.get("path").unwrap(), &Some(String::from("pathname")));
    }
}