sea_shell/
arg_parser.rs

1use crate::re_exports::*;
2
3pub struct Arg<'a> {
4  pub is_required: bool,
5  pub is_flag: bool,
6  pub flag_type: FlagType,
7  pub name: &'a str,
8}
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum FlagType {
12  Number,
13  String,
14  Boolean,
15}
16
17impl Arg<'_> {
18  pub const fn default() -> Self {
19    Self {
20      is_required: false,
21      is_flag: false,
22      flag_type: FlagType::Boolean,
23      name: "",
24    }
25  }
26}
27
28impl Display for Arg<'_> {
29  fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
30    let mut out = String::new();
31
32    if self.is_flag {
33      out.push_str("--");
34    }
35
36    out.push_str(self.name);
37
38    if self.flag_type != FlagType::Boolean {
39      out.push_str("=");
40
41      if self.flag_type == FlagType::Number {
42        out.push_str("<number>");
43      } else {
44        out.push_str("<string>");
45      }
46    }
47
48    if self.is_required {
49      out = format!("[{}]", out);
50    } else {
51      out = format!("{{{}}}", out);
52    }
53
54    write!(f, "{}", out)
55  }
56}