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
use std::collections::HashSet;
use std::collections::HashMap;
use std::str::FromStr;
use std::any::Any;

/// Returned after successfully parsing command-line arguments.
///
/// Contains all the parsed results of the arguments with methods to retrieve those results.
#[derive(Default, Debug)]
pub struct ParsingResults {
    path: String,
    flags: HashSet<String>,
    named_params: HashMap<String, Box<Any>>,
    unnamed_params: Vec<String>,
}

impl ParsingResults {
    /// Constructs and returns a new `ParsingResults` object.
    pub fn new() -> ParsingResults {
        ParsingResults {
            path: String::new(),
            flags: HashSet::<String>::new(),
            named_params: HashMap::<String, Box<Any>>::new(),
            unnamed_params: Vec::<String>::new(),
        }
    }


    /// Returns a reference to the stored path.
    pub fn path(&self) -> &String {
        &self.path
    }

    /// Returns a mutable reference to the stored path.
    pub fn path_mut(&mut self) -> &mut String {
        &mut self.path
    }

    /// Returns a reference to the stored set of flags.
    pub fn flags(&self) -> &HashSet<String> {
        &self.flags
    }

    /// Returns a mutable reference to the stored set of flags.
    pub fn flags_mut(&mut self) -> &mut HashSet<String> {
        &mut self.flags
    }

    /// Returns a reference to the stored map of named parameters to their respective values.
    pub fn named_params(&self) -> &HashMap<String, Box<Any>> {
        &self.named_params
    }

    /// Returns a mutable reference to the stored map of named parameters to their respective values.
    pub fn named_params_mut(&mut self) -> &mut HashMap<String, Box<Any>> {
        &mut self.named_params
    }

    /// Returns a reference to the stored vector of unnamed parameters.
    pub fn unnamed_params(&self) -> &Vec<String> {
        &self.unnamed_params
    }

    /// Returns a mutable reference to the stored vector of unnamed parameters.
    pub fn unnamed_params_mut(&mut self) -> &mut Vec<String> {
        &mut self.unnamed_params
    }


    /// Returns `true` if a flag with the specified `name` was set in the parsed arguments.
    pub fn flag(&self, name: &str) -> bool {
        self.flags.contains(name)
    }

    /// Returns a reference to the specified named parameter's value.
    /// If no such parameter was set in the parsed arguments, returns `None`.
    ///
    /// # Panics
    ///
    /// Panics if the specified type does not match the specified parameter's associated type.
    pub fn named_param<T: 'static + FromStr>(&self, name: &str) -> Option<&T> {
        match self.named_params.get(name) {
            Some(value) => {
                if !value.is::<T>() {
                    panic!("clargs: specified type does not match the specified parameter's associated type");
                }
                Some(value.downcast_ref::<T>().unwrap())
            },
            None => None,
        }
    }

    /// Returns a mutable reference to the specified named parameter's value.
    /// If no such parameter was set in the parsed arguments, returns `None`.
    ///
    /// # Panics
    ///
    /// Panics if the specified type does not match the specified parameter's associated type.
    pub fn named_param_mut<T: 'static + FromStr>(&mut self, name: &str) -> Option<&mut T> {
        match self.named_params.get_mut(name) {
            Some(value) => {
                if !value.is::<T>() {
                    panic!("clargs: specified type does not match the specified parameter's associated type");
                }
                Some(value.downcast_mut::<T>().unwrap())
            },
            None => None,
        }
    }

    /// Returns the number of stored unnamed parameters.
    pub fn num_unnamed_params(&self) -> usize {
        self.unnamed_params.len()
    }

    /// Returns a reference to the unnamed parameter at the specified zero-based `index`.
    ///
    /// # Panics
    ///
    /// Panics if the `index` is higher than or equal to the number of stored unnamed parameters.
    pub fn unnamed_param(&self, index: usize) -> &String {
        if index >= self.unnamed_params.len() {
            panic!("clargs: attempted to access a non-existant unnamed parameter");
        }
        &self.unnamed_params[index]
    }

    /// Returns a mutable reference to the unnamed parameter at the specified zero-based `index`.
    ///
    /// # Panics
    ///
    /// Panics if the `index` is higher than or equal to the number of stored unnamed parameters.
    pub fn unnamed_param_mut(&mut self, index: usize) -> &mut String {
        if index >= self.unnamed_params.len() {
            panic!("clargs: attempted to access a non-existant unnamed parameter");
        }
        &mut self.unnamed_params[index]
    }
}