midenc_session/flags/
flag.rs

1#[derive(Debug, Clone)]
2pub struct CompileFlag {
3    pub name: &'static str,
4    pub short: Option<char>,
5    pub long: Option<&'static str>,
6    pub help: Option<&'static str>,
7    pub help_heading: Option<&'static str>,
8    pub env: Option<&'static str>,
9    pub action: FlagAction,
10    pub default_missing_value: Option<&'static str>,
11    pub default_value: Option<&'static str>,
12    pub hide: Option<bool>,
13}
14impl CompileFlag {
15    pub const fn new(name: &'static str) -> Self {
16        Self {
17            name,
18            short: None,
19            long: None,
20            help: None,
21            help_heading: None,
22            env: None,
23            action: FlagAction::Set,
24            default_missing_value: None,
25            default_value: None,
26            hide: None,
27        }
28    }
29
30    pub const fn short(mut self, short: char) -> Self {
31        self.short = Some(short);
32        self
33    }
34
35    pub const fn long(mut self, long: &'static str) -> Self {
36        self.long = Some(long);
37        self
38    }
39
40    pub const fn action(mut self, action: FlagAction) -> Self {
41        self.action = action;
42        self
43    }
44
45    pub const fn help(mut self, help: &'static str) -> Self {
46        self.help = Some(help);
47        self
48    }
49
50    pub const fn help_heading(mut self, help_heading: &'static str) -> Self {
51        self.help_heading = Some(help_heading);
52        self
53    }
54
55    pub const fn env(mut self, env: &'static str) -> Self {
56        self.env = Some(env);
57        self
58    }
59
60    pub const fn default_value(mut self, value: &'static str) -> Self {
61        self.default_value = Some(value);
62        self
63    }
64
65    pub const fn default_missing_value(mut self, value: &'static str) -> Self {
66        self.default_missing_value = Some(value);
67        self
68    }
69
70    pub const fn hide(mut self, yes: bool) -> Self {
71        self.hide = Some(yes);
72        self
73    }
74}
75
76#[derive(Debug, Copy, Clone, PartialEq, Eq)]
77pub enum FlagAction {
78    Set,
79    Append,
80    SetTrue,
81    SetFalse,
82    Count,
83}
84impl FlagAction {
85    pub fn is_boolean(&self) -> bool {
86        matches!(self, Self::SetTrue | Self::SetFalse)
87    }
88
89    pub fn as_boolean_value(&self) -> bool {
90        assert!(self.is_boolean());
91        self == &Self::SetTrue
92    }
93}
94
95#[cfg(feature = "std")]
96impl From<FlagAction> for clap::ArgAction {
97    fn from(action: FlagAction) -> Self {
98        match action {
99            FlagAction::Set => Self::Set,
100            FlagAction::Append => Self::Append,
101            FlagAction::SetTrue => Self::SetTrue,
102            FlagAction::SetFalse => Self::SetFalse,
103            FlagAction::Count => Self::Count,
104        }
105    }
106}
107
108inventory::collect!(CompileFlag);