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
#[derive(Debug, Clone)]
/// Value of an argument
pub enum ArgVal {
    Bool(bool),
    Str(String),
}

#[derive(Debug, Clone)]
/// Argument struct
pub struct Arg {
    /// Name of the argument
    pub (crate) name: String,
    /// Optional long (string) value of the argument
    pub (crate) long: Option<String>,
    /// Optional short (char) value of the argument
    pub (crate) short: Option<char>,
    /// Help string for the argument
    pub (crate) help: String,
    /// Value of the argument
    pub (crate) val: ArgVal,
}

impl Arg {
    /// Creates a new bool (flag) argument
    pub fn bool(name: &str, val: bool) -> Self {
        Self {
            name: String::from(name),
            long: None,
            short: None,
            help: String::new(),
            val: ArgVal::Bool(val),
        }
    }

    /// Creates a new str (option) argument
    pub fn str(name: &str, val: &str) -> Self {
        Self {
            name: String::from(name),
            long: None,
            short: None,
            help: String::new(),
            val: ArgVal::Str(String::from(val)),
        }
    }

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

    /// Sets the short value of the argument
    pub fn short(&mut self, short: char) -> &mut Self {
        self.short = Some(short);
        self
    }

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