cain_ls/config/
mod.rs

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
//! Args Parser Module

use std::env::Args;

pub struct Config {
    pub all_flag: bool,
    pub show_detail_flag: bool,
}

impl Config {
    pub fn new(mut args: Args) -> Result<Config, String> {
        args.next();

        let mut config = Config {
            all_flag: false,
            show_detail_flag: false,
        };

        for arg in args {
            if arg.starts_with("-") {
                for c in arg.chars().skip(1) {
                    match c {
                        'a' => config.all_flag = true,
                        'l' => config.show_detail_flag = true,
                        _ => return Err(format!("Invalid option: {}", c))
                    }
                }
            } else {
                return Err(format!("Invalid argument: {}", arg))
            }
        }

        Ok(config)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn config_new() {
    }
}