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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use crate::args::*;
use crate::errors::{Result, *};
use std::collections::HashMap;

/// Specifies the valid arguments of the program and is used to parse
/// the command-line arguments into an [`Arg`].
pub struct ArgSpec {
  args: HashMap<String, ArgType>,
}

type ArgIter = std::iter::Peekable<std::iter::Skip<std::env::Args>>;

impl ArgSpec {
  /// Creates an [`ArgSpecBuilder`] that can be used to build the [`ArgSpec`].
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let spec = ArgSpec::build()
  ///     .boolean("arg1")
  ///     .done()
  ///     .unwrap();
  /// ```
  pub fn build() -> ArgSpecBuilder {
    ArgSpecBuilder {
      args: HashMap::new(),
      err: None,
    }
  }

  /// Determines if an argument of a given name and [`ArgType`] exists
  /// within the [`ArgSpec`].
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  /// let spec = ArgSpec::build()
  ///     .string("username")
  ///     .done()
  ///     .unwrap();
  /// if spec.has_arg("username", ArgType::String) {
  ///     let args = spec.parse().unwrap();
  ///     if let Some(username) = args.string("username") {
  ///         // do something with username
  ///     }
  /// }
  /// ```
  pub fn has_arg(&self, name: impl Into<String>, ty: ArgType) -> bool {
    if let Some(_t) = self.args.get(&name.into()) {
      matches!(ty, _t)
    } else {
      false
    }
  }

  /// Parses the command-line arguments and Returns [`Ok(Args)`] if there
  /// were no parse errors and [`Err(Error)`] if otherwise.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let spec = ArgSpec::build()
  ///     .boolean("vsync")
  ///     .done()
  ///     .unwrap();
  /// match spec.parse() {
  ///     Ok(args) => {
  ///         // do stuff with the arguments
  ///     }
  ///     Err(err) => eprintln!("{:?}", err),
  /// }
  /// ```
  pub fn parse(&self) -> Result<Args> {
    let mut bools: HashMap<String, bool> = HashMap::new();
    let mut ints: HashMap<String, i64> = HashMap::new();
    let mut uints: HashMap<String, u64> = HashMap::new();
    let mut strs: HashMap<String, String> = HashMap::new();
    let mut bool_arrays: HashMap<String, Box<[bool]>> = HashMap::new();
    let mut int_arrays: HashMap<String, Box<[i64]>> = HashMap::new();
    let mut uint_arrays: HashMap<String, Box<[u64]>> = HashMap::new();
    let mut str_arrays: HashMap<String, Box<[String]>> = HashMap::new();
    let mut free_args: Vec<String> = Vec::new();
    let mut args = std::env::args().skip(1).peekable();

    while let Some(arg) = args.next() {
      let mut chars = arg.chars().peekable();
      if chars.peek() == Some(&'-') {
        chars.next();
        if chars.peek() == Some(&'-') {
          chars.next();
        }
        let arg_name: String = chars.collect();
        let arg_type = *self
          .args
          .get(&arg_name)
          .ok_or(Error::UnknownArgument(arg_name.clone()))?;
        match arg_type {
          ArgType::Boolean => parse_bool(arg_name, &mut args, &mut bools)?,
          ArgType::Integer => parse_arg(arg_name, arg_type, &mut args, &mut ints)?,
          ArgType::UInteger => parse_arg(arg_name, arg_type, &mut args, &mut uints)?,
          ArgType::String => parse_arg(arg_name, arg_type, &mut args, &mut strs)?,
          ArgType::BooleanArray(n) => {
            parse_array_arg(arg_name, arg_type, n, &mut args, &mut bool_arrays)?
          }
          ArgType::IntegerArray(n) => {
            parse_array_arg(arg_name, arg_type, n, &mut args, &mut int_arrays)?
          }
          ArgType::UIntegerArray(n) => {
            parse_array_arg(arg_name, arg_type, n, &mut args, &mut uint_arrays)?
          }
          ArgType::StringArray(n) => {
            parse_array_arg(arg_name, arg_type, n, &mut args, &mut str_arrays)?
          }
        }
      } else {
        free_args.push(chars.collect());
      }
    }

    Ok(Args::new(
      bools,
      ints,
      uints,
      strs,
      bool_arrays,
      int_arrays,
      uint_arrays,
      str_arrays,
      free_args,
    ))
  }
}

trait HashMapExt<V> {
  fn insert_arg(&mut self, name: String, v: V) -> Result<()>;
}

impl<V> HashMapExt<V> for HashMap<String, V> {
  fn insert_arg(&mut self, name: String, v: V) -> Result<()> {
    if self.contains_key(&name) {
      Err(Error::RepeatedArgument(name))
    } else {
      self.insert(name, v);
      Ok(())
    }
  }
}

trait StringExt {
  fn is_valid_arg_name(&self) -> bool;
}

impl StringExt for String {
  fn is_valid_arg_name(&self) -> bool {
    !(self.starts_with('-')
      || self.contains(|c: char| c.is_whitespace())
      || self.contains(|c: char| c.is_control()))
  }
}

fn parse_arg<T: std::str::FromStr>(
  arg_name: String,
  arg_type: ArgType,
  args: &mut ArgIter,
  dict: &mut HashMap<String, T>,
) -> Result<()> {
  let arg_str = args
    .next()
    .ok_or(Error::MissingParameter(arg_name.clone(), arg_type))?;
  dict.insert_arg(
    arg_name.clone(),
    arg_str.parse::<T>().or(Err(Error::InvalidParameter(
      arg_name.clone(),
      arg_type,
      arg_str,
    )))?,
  )?;
  Ok(())
}

fn parse_bool(
  arg_name: String,
  args: &mut ArgIter,
  bools: &mut HashMap<String, bool>,
) -> Result<()> {
  let value = if args.peek() == Some(&"false".to_string()) {
    args.next();
    false
  } else if args.peek() == Some(&"true".to_string()) {
    args.next();
    true
  } else {
    true
  };
  bools.insert_arg(arg_name, value)
}

fn parse_array_arg<T: std::str::FromStr>(
  arg_name: String,
  arg_type: ArgType,
  array_size: usize,
  args: &mut ArgIter,
  dict: &mut HashMap<String, Box<[T]>>,
) -> Result<()> {
  let mut params: Vec<T> = Vec::with_capacity(array_size);
  for _ in 0..array_size {
    let arg_str = args
      .next()
      .ok_or(Error::MissingParameter(arg_name.clone(), arg_type))?;
    params.push(arg_str.parse::<T>().or(Err(Error::InvalidParameter(
      arg_name.clone(),
      arg_type,
      arg_str,
    )))?);
  }
  dict.insert_arg(arg_name, params.into_boxed_slice())
}

impl std::fmt::Debug for ArgSpec {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_map().entries(self.args.iter()).finish()
  }
}

/// Enumerates all data types that are handled by [`ArgSpec::parse()`].
#[derive(Copy, Clone, Debug)]
pub enum ArgType {
  Boolean,
  BooleanArray(usize),
  Integer,
  IntegerArray(usize),
  UInteger,
  UIntegerArray(usize),
  String,
  StringArray(usize),
}

/// Builder type for [`ArgSpec`].
pub struct ArgSpecBuilder {
  args: HashMap<String, ArgType>,
  err: Option<Error>,
}

impl ArgSpecBuilder {
  /// Completes building the [`ArgSpec`]. Returns [`Ok(ArgSpec)`] if no build
  /// errors and [`Err(Error)`] if otherwise.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let spec = ArgSpec::build()
  ///     .uinteger("chickens")
  ///     .done().
  ///     unwrap();
  /// ```
  pub fn done(self) -> Result<ArgSpec> {
    if let Some(err) = self.err {
      Err(err)
    } else {
      Ok(ArgSpec { args: self.args })
    }
  }

  /// Little Wrapper function that will complete building the [`ArgSpec`]
  /// and immediately call [`parse()`].
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::ArgSpec;
  ///
  /// let args = ArgSpec::build()
  ///     .boolean("fullscreen")
  ///     .boolean("vsync")
  ///     .string("username")
  ///     .parse()
  ///     .unwrap();
  /// ```
  pub fn parse(self) -> Result<Args> {
    self.done()?.parse()
  }

  /// Adds an argument the the [`ArgSpec`] with a given name and type.
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  /// let spec = ArgSpec::build()
  ///     .arg("flag", ArgType::Boolean)
  ///     .done()
  ///     .unwrap();
  /// ```
  pub fn arg(mut self, name: impl Into<String>, ty: ArgType) -> ArgSpecBuilder {
    let name_str = name.into();
    if self.err.is_none() && !self.args.contains_key(&name_str) {
      if name_str.is_valid_arg_name() {
        self.args.insert(name_str, ty);
      } else {
        self.err = Some(Error::InvalidArgumentName(name_str));
      }
    } else {
      self.err = Some(Error::RedeclaredArgument(name_str));
    }
    self
  }

  /// Adds a boolean argument to the [`ArgSpec`].
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  ///  let spec = ArgSpec::build()
  ///     .boolean("vsync")
  ///     .done()
  ///     .unwrap();
  ///  assert_eq!(spec.has_arg("vsync", ArgType::Boolean), true);
  /// ```
  pub fn boolean(self, name: impl Into<String>) -> ArgSpecBuilder {
    self.arg(name, ArgType::Boolean)
  }

  /// Adds an i64 argument to the [`ArgSpec`].
  ///
  /// # Exmaple
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  /// let spec = ArgSpec::build()
  ///     .integer("num-bananas")
  ///     .done()
  ///     .unwrap();
  /// assert_eq!(spec.has_arg("num-bananas", ArgType::Integer), true);
  /// ```
  pub fn integer(self, name: impl Into<String>) -> ArgSpecBuilder {
    self.arg(name, ArgType::Integer)
  }

  /// Adds a u64 argument to the [`ArgSpec`].
  ///
  /// # Exmaple
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  /// let spec = ArgSpec::build()
  ///     .uinteger("screen-width")
  ///     .done()
  ///     .unwrap();
  /// assert_eq!(spec.has_arg("screen-width", ArgType::UInteger), true);
  /// ```
  pub fn uinteger(self, name: impl Into<String>) -> ArgSpecBuilder {
    self.arg(name, ArgType::UInteger)
  }

  /// Adds a String argument to the [`ArgSpec`].
  ///
  /// # Exmaple
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  /// let spec = ArgSpec::build()
  ///     .string("MOTD")
  ///     .done()
  ///     .unwrap();
  /// assert_eq!(spec.has_arg("MOTD", ArgType::String), true);
  /// ```
  pub fn string(self, name: impl Into<String>) -> ArgSpecBuilder {
    self.arg(name, ArgType::String)
  }

  /// Adds a boolean array argument to the [`ArgSpec`].
  ///
  /// # Example
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  ///  let spec = ArgSpec::build()
  ///     .boolean_array(2, "bools")
  ///     .done()
  ///     .unwrap();
  ///  assert_eq!(spec.has_arg("bools", ArgType::BooleanArray(2)), true);
  /// ```
  pub fn boolean_array(self, size: usize, name: impl Into<String>) -> ArgSpecBuilder {
    self.arg(name, ArgType::BooleanArray(size))
  }

  /// Adds an i64 array argument to the [`ArgSpec`].
  ///
  /// # Exmaple
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  /// let spec = ArgSpec::build()
  ///     .integer_array(3, "position")
  ///     .done()
  ///     .unwrap();
  /// assert_eq!(spec.has_arg("position", ArgType::IntegerArray(3)), true);
  /// ```
  pub fn integer_array(self, size: usize, name: impl Into<String>) -> ArgSpecBuilder {
    self.arg(name, ArgType::IntegerArray(size))
  }

  /// Adds a u64 array argument to the [`ArgSpec`].
  ///
  /// # Exmaple
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  /// let spec = ArgSpec::build()
  ///     .uinteger_array(2, "size")
  ///     .done()
  ///     .unwrap();
  /// assert_eq!(spec.has_arg("size", ArgType::UIntegerArray(2)), true);
  /// ```
  pub fn uinteger_array(self, size: usize, name: impl Into<String>) -> ArgSpecBuilder {
    self.arg(name, ArgType::UIntegerArray(size))
  }

  /// Adds a String array argument to the [`ArgSpec`].
  ///
  /// # Exmaple
  ///
  /// ```
  /// use easy_args::spec::{ArgSpec, ArgType};
  ///
  /// let spec = ArgSpec::build()
  ///     .string_array(2, "name")
  ///     .done()
  ///     .unwrap();
  /// assert_eq!(spec.has_arg("name", ArgType::StringArray(2)), true);
  /// ```
  pub fn string_array(self, size: usize, name: impl Into<String>) -> ArgSpecBuilder {
    self.arg(name, ArgType::StringArray(size))
  }
}