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
/// Contains value for commands and parameters
#[derive(Debug, Clone)]
pub enum ArgValue {
    Bool(bool),
    Int(i64),
    Float(f64),
    String(String)
}

/// Set value type for commands and parameters
#[derive(Debug, Clone, PartialEq)]
pub enum ArgType {
    Bool,
    Int,
    Float,
    String
}

impl Default for ArgType {
    fn default() -> Self {
        ArgType::Bool
    }
}

/// Command parameter
#[derive(Debug)]
pub struct Parameter {
    pub(super) name: String,
    pub(super) value_type: ArgType,
    pub(super) description: String
}

/// Buildr for command parameter
#[derive(Default)]
pub struct ParameterBuilder<'a> {
    pub(super) name: String,
    pub(super) aliases: Vec<String>,
    pub(super) description: Option<&'a str>,
    pub(super) value_type: ArgType
}

impl Parameter {
    /// Create parameter builder with name
    pub fn with_name(name: &str) -> ParameterBuilder {
        ParameterBuilder {
            name: name.to_string(),
            ..
            Default::default()
        }
    }
}

impl<'a> ParameterBuilder<'a> {
    /// Set parameters value type
    pub fn value_type(mut self, t: ArgType) -> Self {
        self.value_type = t;
        self
    }

    /// Add alias for parameter.
    /// If alias consist from single charceter than this parameter will be used as short
    pub fn alias(mut self, alias: &'a str) -> Self {
        self.aliases.push(alias.into());
        self
    }

    /// Add description that shown in help
    pub fn description(mut self, text: &'a str) -> Self {
        self.description = Some(text);
        self
    }
}