clars 0.2.0

Command line arguments resolution system
Documentation
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/// CLI option.
#[derive(Debug, Default, Clone)]
pub struct ClarOption {
  /// Option name.
  name: String,
  /// Short label.
  short_label: Option<char>,
  /// Long label.
  long_label: Option<String>,
  /// Indicates whether option must be used without other options, arguments, or commands.
  standalone: bool,
  /// Default value used when the option is not specified.
  default_value: Option<String>,
  /// Default value used when option is provided without value.
  default_missing_value: Option<String>,
  /// List of possible values for option.
  possible_values: Vec<String>,
  /// Indicates whether option takes a value.
  takes_value: Option<String>,
  /// Help content.
  help: Option<String>,
  /// Long help content.
  help_long: Option<String>,
  /// Path in definition tree.
  path: String,
}

impl ClarOption {
  pub fn new(name: impl AsRef<str>, short: char, long: impl AsRef<str>) -> Self {
    Self {
      name: name.as_ref().to_string(),
      short_label: Some(short),
      long_label: Some(long.as_ref().to_string()),
      ..Default::default()
    }
  }

  pub fn new_short(name: impl AsRef<str>, label: char) -> Self {
    Self {
      name: name.as_ref().to_string(),
      short_label: Some(label),
      ..Default::default()
    }
  }

  pub fn new_long(name: impl AsRef<str>, label: impl AsRef<str>) -> Self {
    Self {
      name: name.as_ref().to_string(),
      long_label: Some(label.as_ref().to_string()),
      ..Default::default()
    }
  }

  pub fn short(mut self, label: char) -> Self {
    self.short_label = Some(label);
    self
  }

  pub fn long(mut self, label: impl AsRef<str>) -> Self {
    self.long_label = Some(label.as_ref().to_string());
    self
  }

  pub fn standalone(mut self) -> Self {
    self.standalone = true;
    self
  }

  pub fn default_value(mut self, value: impl AsRef<str>) -> Self {
    self.default_value = Some(value.as_ref().to_string());
    self
  }

  pub fn default_missing_value(mut self, value: impl AsRef<str>) -> Self {
    self.default_missing_value = Some(value.as_ref().to_string());
    self
  }

  pub fn possible_values<I, S>(mut self, values: I) -> Self
  where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
  {
    self.possible_values = values.into_iter().map(|value| value.as_ref().to_owned()).collect();
    self
  }

  pub fn takes_value(mut self, caption: impl AsRef<str>) -> Self {
    self.takes_value = Some(caption.as_ref().to_string());
    self
  }

  pub fn help(mut self, content: impl AsRef<str>) -> Self {
    self.help = Some(content.as_ref().to_string());
    self.help_long = Some(content.as_ref().to_string());
    self
  }

  pub fn help_long(mut self, content: impl AsRef<str>) -> Self {
    self.help_long = Some(content.as_ref().to_string());
    self
  }

  pub fn get_name(&self) -> &str {
    &self.name
  }

  pub fn get_short_label(&self) -> &Option<char> {
    &self.short_label
  }

  pub fn get_long_label(&self) -> &Option<String> {
    &self.long_label
  }

  pub fn is_standalone(&self) -> bool {
    self.standalone
  }

  pub fn get_default_value(&self) -> &Option<String> {
    &self.default_value
  }

  pub fn get_default_missing_value(&self) -> &Option<String> {
    &self.default_missing_value
  }

  pub fn get_takes_value(&self) -> &Option<String> {
    &self.takes_value
  }

  pub fn get_possible_values(&self) -> &Vec<String> {
    &self.possible_values
  }

  pub fn get_help(&self) -> &Option<String> {
    &self.help
  }

  pub fn get_help_long(&self) -> &Option<String> {
    &self.help_long
  }

  fn set_path(&mut self, parent_path: impl AsRef<str>) {
    self.path = make_path(parent_path.as_ref(), &self.name);
  }

  pub(crate) fn get_path(&self) -> &str {
    &self.path
  }
}

/// CLI argument.
#[derive(Debug, Default, Clone)]
pub struct ClarArgument {
  /// Argument name.
  name: String,
  /// Caption displayed in usage instead of the name.
  caption: String,
  /// Default value used when argument is not specified.
  default_value: Option<String>,
  /// Indicates whether argument is required.
  required: bool,
  /// Help content.
  help: Option<String>,
  /// Long help content.
  help_long: Option<String>,
  /// Path in definition tree.
  path: String,
}

impl ClarArgument {
  pub fn new(name: impl AsRef<str>) -> Self {
    Self {
      name: name.as_ref().to_string(),
      caption: name.as_ref().to_uppercase(),
      ..Default::default()
    }
  }

  pub fn caption(mut self, caption: impl AsRef<str>) -> Self {
    self.caption = caption.as_ref().to_string();
    self
  }

  pub fn default_value(mut self, default_value: impl AsRef<str>) -> Self {
    self.default_value = Some(default_value.as_ref().to_string());
    self
  }

  pub fn required(mut self) -> Self {
    self.required = true;
    self
  }

  pub fn help(mut self, help: impl AsRef<str>) -> Self {
    self.help = Some(help.as_ref().to_string());
    self
  }

  pub fn help_long(mut self, help_long: impl AsRef<str>) -> Self {
    self.help_long = Some(help_long.as_ref().to_string());
    self
  }

  pub fn get_name(&self) -> &str {
    &self.name
  }

  pub fn get_caption(&self) -> &str {
    &self.caption
  }

  pub fn get_default_value(&self) -> &Option<String> {
    &self.default_value
  }

  pub fn is_required(&self) -> bool {
    self.required
  }

  pub fn get_help(&self) -> &Option<String> {
    &self.help
  }

  pub fn get_help_long(&self) -> &Option<String> {
    if self.help_long.is_some() {
      &self.help_long
    } else {
      self.get_help()
    }
  }

  fn set_path(&mut self, parent_path: impl AsRef<str>) {
    self.path = make_path(parent_path.as_ref(), &self.name);
  }

  pub(crate) fn get_path(&self) -> &str {
    &self.path
  }
}

/// CLI command.
#[derive(Debug, Default, Clone)]
pub struct ClarCommand {
  /// Command name.
  name: String,
  /// Child definition items.
  items: Vec<ClarItem>,
  /// Help content.
  help: Option<String>,
  /// Long help content.
  help_long: Option<String>,
  /// Path definition tree.
  path: String,
}

impl ClarCommand {
  /// Creates a new command with the given name.
  pub fn new(name: impl AsRef<str>) -> Self {
    Self {
      name: name.as_ref().to_string(),
      ..Default::default()
    }
  }

  pub fn help(mut self, help: impl AsRef<str>) -> Self {
    self.help = Some(help.as_ref().to_string());
    self
  }

  pub fn help_long(mut self, help_long: impl AsRef<str>) -> Self {
    self.help_long = Some(help_long.as_ref().to_string());
    self
  }

  /// Returns command resolver that recognizes an **option terminator**.
  pub fn terminator(&mut self, terminator: ClarTerminator) {
    self.items.clear();
    self.items.push(ClarItem::Terminator(terminator));
  }

  /// Returns command resolver that recognizes **options**.
  pub fn options<O>(mut self, options: O) -> Self
  where
    O: IntoIterator<Item = ClarOption>,
  {
    self.items.clear();
    self.items.push(ClarItem::Options(options.into_iter().collect()));
    self
  }

  /// Returns command resolver that recognizes **options** followed by **option terminator**.
  pub fn options_t<O>(&mut self, options: O, t: ClarTerminator)
  where
    O: IntoIterator<Item = ClarOption>,
  {
    self.items.clear();
    self.items.push(ClarItem::Options(options.into_iter().collect()));
    self.items.push(ClarItem::Terminator(t));
  }

  /// Returns command resolver that recognizes **arguments**.
  pub fn arguments<A>(mut self, a: A) -> Self
  where
    A: IntoIterator<Item = ClarArgument>,
  {
    self.items.clear();
    self.items.push(ClarItem::Arguments(a.into_iter().collect()));
    self
  }

  /// Returns command resolver that recognizes **arguments** followed by **option terminator**.
  pub fn arguments_t<A>(mut self, a: A, t: ClarTerminator) -> Self
  where
    A: IntoIterator<Item = ClarArgument>,
  {
    self.items.clear();
    self.items.push(ClarItem::Arguments(a.into_iter().collect()));
    self.items.push(ClarItem::Terminator(t));
    self
  }

  /// Returns command resolver that recognizes **options** followed by **arguments**.
  pub fn options_arguments<O, A>(mut self, o: O, a: A) -> Self
  where
    O: IntoIterator<Item = ClarOption>,
    A: IntoIterator<Item = ClarArgument>,
  {
    self.items.clear();
    self.items.push(ClarItem::Options(o.into_iter().collect()));
    self.items.push(ClarItem::Arguments(a.into_iter().collect()));
    self
  }

  /// Returns command resolver that recognizes **options** followed by **arguments** and **option terminator**.
  pub fn options_arguments_t<O, A>(mut self, o: O, a: A, t: ClarTerminator) -> Self
  where
    O: IntoIterator<Item = ClarOption>,
    A: IntoIterator<Item = ClarArgument>,
  {
    self.items.clear();
    self.items.push(ClarItem::Options(o.into_iter().collect()));
    self.items.push(ClarItem::Arguments(a.into_iter().collect()));
    self.items.push(ClarItem::Terminator(t));
    self
  }

  /// Returns command resolver that recognizes **commands**.
  pub fn commands<S>(mut self, s: S) -> Self
  where
    S: IntoIterator<Item = ClarCommand>,
  {
    self.items.clear();
    self.items.push(ClarItem::Commands(s.into_iter().collect()));
    self
  }

  /// Builds subcommand resolver that recognizes **options** followed by **subcommands**.
  pub fn options_subcommands<O, S>(mut self, o: O, s: S) -> Self
  where
    O: IntoIterator<Item = ClarOption>,
    S: IntoIterator<Item = ClarCommand>,
  {
    self.items.clear();
    self.items.push(ClarItem::Options(o.into_iter().collect()));
    self.items.push(ClarItem::Commands(s.into_iter().collect()));
    self
  }

  pub fn get_name(&self) -> &str {
    &self.name
  }

  pub(crate) fn get_items(&self) -> &Vec<ClarItem> {
    &self.items
  }

  pub fn get_help(&self) -> &Option<String> {
    &self.help
  }

  pub fn get_help_long(&self) -> &Option<String> {
    if self.help_long.is_some() {
      &self.help_long
    } else {
      self.get_help()
    }
  }

  fn set_path(&mut self, parent_path: impl AsRef<str>) {
    self.path = make_path(parent_path.as_ref(), &self.name);
  }

  pub(crate) fn get_path(&self) -> &str {
    &self.path
  }
}

/// Command line option terminator (`--`).
///
/// When present in the argument list, everything after it is treated
/// as a positional argument, even if it looks like an option.
#[derive(Debug, Default, Clone)]
pub struct ClarTerminator {
  /// Terminator name.
  pub name: String,
  /// Indicated whether terminator is required.
  required: bool,
  /// Path definition tree.
  path: String,
}

impl ClarTerminator {
  /// Creates a new terminator with the given name.
  pub fn new(name: impl AsRef<str>) -> Self {
    Self {
      name: name.as_ref().to_string(),
      ..Default::default()
    }
  }

  /// Marks this option terminator as required.
  ///
  /// If the option terminator is not present in the argument list, resolving will fail.
  pub fn required(mut self) -> Self {
    self.required = true;
    self
  }

  /// Returns the option terminator name.
  pub fn get_name(&self) -> &str {
    &self.name
  }

  pub fn is_required(&self) -> bool {
    self.required
  }

  fn set_path(&mut self, parent_path: impl AsRef<str>) {
    self.path = make_path(parent_path.as_ref(), &self.name);
  }
  pub(crate) fn get_path(&self) -> &str {
    &self.path
  }
}

/// Command line item enumeration.
#[derive(Debug, Clone)]
pub enum ClarItem {
  Options(Vec<ClarOption>),
  Commands(Vec<ClarCommand>),
  Arguments(Vec<ClarArgument>),
  Terminator(ClarTerminator),
}

pub fn update_paths(items: &mut Vec<ClarItem>, segments: &mut Vec<String>) {
  let parent_path = segments.join("/");
  for item in items {
    match item {
      ClarItem::Commands(subcommands) => {
        for subcommand in subcommands {
          subcommand.set_path(&parent_path);
          segments.push(subcommand.get_name().to_string());
          update_paths(&mut subcommand.items, segments);
          segments.pop();
        }
      }
      ClarItem::Options(options) => options.iter_mut().for_each(|o| o.set_path(&parent_path)),
      ClarItem::Arguments(arguments) => arguments.iter_mut().for_each(|a| a.set_path(&parent_path)),
      ClarItem::Terminator(option_terminator) => option_terminator.set_path(&parent_path),
    }
  }
}

pub fn display_tree(items: &[ClarItem]) {
  for item in items {
    match item {
      ClarItem::Commands(subcommands) => {
        for subcommand in subcommands {
          println!("{}", subcommand.get_path());
          display_tree(&subcommand.items);
        }
      }
      ClarItem::Options(options) => options.iter().for_each(|o| println!("{}", o.get_path())),
      ClarItem::Arguments(arguments) => arguments.iter().for_each(|a| println!("{}", a.get_path())),
      ClarItem::Terminator(option_terminator) => println!("{}", option_terminator.get_path()),
    }
  }
}

pub fn find_command<'a>(command_names: &'a [&str], items: &'a [ClarItem]) -> Option<&'a ClarCommand> {
  let searched_name = command_names.first()?;
  let command = items
    .iter()
    .filter_map(|item| {
      if let ClarItem::Commands(commands) = item {
        Some(commands)
      } else {
        None
      }
    })
    .flatten()
    .find(|command| command.get_name() == *searched_name)?;
  let remaining_names = &command_names[1..];
  if remaining_names.is_empty() {
    Some(command)
  } else {
    find_command(remaining_names, command.get_items())
  }
}

fn make_path(parent_path: &str, name: &str) -> String {
  if parent_path.is_empty() {
    name.to_string()
  } else {
    format!("{}/{}", parent_path, name)
  }
}