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
use std::collections::HashMap;

/// Holds all the command-line arguments given by the user.
///
/// Each argument is contained within a [`HashMap`] that can be index by the
/// argument's name.
#[derive(Debug)]
pub struct Args {
  bools: HashMap<String, bool>,
  ints: HashMap<String, i64>,
  uints: HashMap<String, u64>,
  strs: HashMap<String, String>,
  bool_arrays: HashMap<String, Box<[bool]>>,
  int_arrays: HashMap<String, Box<[i64]>>,
  uint_arrays: HashMap<String, Box<[u64]>>,
  str_arrays: HashMap<String, Box<[String]>>,
  free_args: Vec<String>,
}

impl Args {
  pub(crate) fn new(
    bools: HashMap<String, bool>,
    ints: HashMap<String, i64>,
    uints: HashMap<String, u64>,
    strs: HashMap<String, String>,
    bool_arrays: HashMap<String, Box<[bool]>>,
    int_arrays: HashMap<String, Box<[i64]>>,
    uint_arrays: HashMap<String, Box<[u64]>>,
    str_arrays: HashMap<String, Box<[String]>>,
    free_args: Vec<String>,
  ) -> Self {
    Args {
      bools,
      ints,
      uints,
      strs,
      bool_arrays,
      int_arrays,
      uint_arrays,
      str_arrays,
      free_args,
    }
  }

  /// Determines if an argument of a given name was set by the user in the
  /// command-line.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .uinteger("window-height")
  ///     .parse()
  ///     .unwrap();
  /// if args.is_set("window-height") {
  ///     // resize the window height
  /// }
  /// ```
  pub fn is_set(&self, name: impl Into<String>) -> bool {
    let n = name.into();
    self
      .bools
      .keys()
      .chain(self.ints.keys())
      .chain(self.uints.keys())
      .chain(self.strs.keys())
      .chain(self.bool_arrays.keys())
      .chain(self.int_arrays.keys())
      .chain(self.uint_arrays.keys())
      .chain(self.str_arrays.keys())
      .find(|&k| *k == n)
      .is_some()
  }

  /// Returns a reference to the boolean value that corresponds with the
  /// given argument name.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .boolean("fullscreen")
  ///     .parse()
  ///     .unwrap();
  /// if args.boolean("fullscreen") == Some(&true) {
  ///     // go fullscreen
  /// }
  /// ```
  pub fn boolean(&self, name: impl Into<String>) -> Option<&bool> {
    self.bools.get(&name.into())
  }

  /// Returns a reference to the i64 value that corresponds with the given
  /// argument name.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .integer("leaves")
  ///     .parse()
  ///     .unwrap();
  /// let num_leaves_in_pile = *args.integer("leaves").unwrap_or(&0);
  /// ```
  pub fn integer(&self, name: impl Into<String>) -> Option<&i64> {
    self.ints.get(&name.into())
  }

  /// Returns a reference to the u64 value that corresponds with the given
  /// argument name.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .uinteger("size")
  ///     .parse()
  ///     .unwrap();
  /// let size = *args.uinteger("size").unwrap_or(&0);
  /// ```
  pub fn uinteger(&self, name: impl Into<String>) -> Option<&u64> {
    self.uints.get(&name.into())
  }

  /// Returns a reference to the String value that corresponds with the
  /// given argument name.
  ///
  /// # Exmaple
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .string("username")
  ///     .parse()
  ///     .unwrap();
  /// let username = args.string("username").unwrap_or(&"Guest".to_string());
  /// ```
  pub fn string(&self, name: impl Into<String>) -> Option<&String> {
    self.strs.get(&name.into())
  }

  /// Returns a reference to the boolean slice that corresponds with the
  /// given argument name.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .boolean_array(5, "flags")
  ///     .parse()
  ///     .unwrap();
  /// if let Some(flags) = args.boolean_array("flags") {
  ///     // do something with flags
  /// }
  /// ```
  pub fn boolean_array(&self, name: impl Into<String>) -> Option<&[bool]> {
    Some(self.bool_arrays.get(&name.into())?.as_ref())
  }

  /// Returns a reference to the i64 slice that corresponds with the given
  /// argument name.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .integer_array(3, "position")
  ///     .parse()
  ///     .unwrap();
  /// if let Some([x, y, z]) = args.integer_array("position") {
  ///     // do something with the position
  /// }
  /// ```
  pub fn integer_array(&self, name: impl Into<String>) -> Option<&[i64]> {
    Some(self.int_arrays.get(&name.into())?.as_ref())
  }

  /// Returns a reference to the u64 slice that corresponds with the given
  /// argument name.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .uinteger_array(2, "size")
  ///     .parse()
  ///     .unwrap();
  /// if let Some([width, height]) = args.uinteger_array("size") {
  ///     // do something with screen size
  /// }
  /// ```
  pub fn uinteger_array(&self, name: impl Into<String>) -> Option<&[u64]> {
    Some(self.uint_arrays.get(&name.into())?.as_ref())
  }

  /// Returns a reference to the String slice that corresponds with the
  /// given argument name.
  ///
  /// # Exmaple
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .string_array(2, "login-details")
  ///     .parse()
  ///     .unwrap();
  /// if let Some([username, password]) = args.string_array("login-details") {
  ///     // do something with username and password
  /// }
  /// ```
  pub fn string_array(&self, name: impl Into<String>) -> Option<&[String]> {
    Some(self.str_arrays.get(&name.into())?.as_ref())
  }

  /// A Vector of all strings passed as command-line arguments that weren't
  /// arguments of the [`ArgSpec`].
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build().parse().unwrap();
  /// for arg in args.free_args() {
  ///     println!("What the heck is this? '{}'.", arg);
  /// }
  /// ```
  pub fn free_args(&self) -> &Vec<String> {
    &self.free_args
  }
}