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
#[derive(Debug, Clone)]
pub struct CompileFlag {
    pub name: &'static str,
    pub short: Option<char>,
    pub long: Option<&'static str>,
    pub help: Option<&'static str>,
    pub help_heading: Option<&'static str>,
    pub env: Option<&'static str>,
    pub action: FlagAction,
    pub default_missing_value: Option<&'static str>,
    pub default_value: Option<&'static str>,
    pub hide: Option<bool>,
}
impl CompileFlag {
    pub const fn new(name: &'static str) -> Self {
        Self {
            name,
            short: None,
            long: None,
            help: None,
            help_heading: None,
            env: None,
            action: FlagAction::Set,
            default_missing_value: None,
            default_value: None,
            hide: None,
        }
    }

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

    pub const fn long(mut self, long: &'static str) -> Self {
        self.long = Some(long);
        self
    }

    pub const fn action(mut self, action: FlagAction) -> Self {
        self.action = action;
        self
    }

    pub const fn help(mut self, help: &'static str) -> Self {
        self.help = Some(help);
        self
    }

    pub const fn help_heading(mut self, help_heading: &'static str) -> Self {
        self.help_heading = Some(help_heading);
        self
    }

    pub const fn env(mut self, env: &'static str) -> Self {
        self.env = Some(env);
        self
    }

    pub const fn default_value(mut self, value: &'static str) -> Self {
        self.default_value = Some(value);
        self
    }

    pub const fn default_missing_value(mut self, value: &'static str) -> Self {
        self.default_missing_value = Some(value);
        self
    }

    pub const fn hide(mut self, yes: bool) -> Self {
        self.hide = Some(yes);
        self
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FlagAction {
    Set,
    Append,
    SetTrue,
    SetFalse,
    Count,
}
impl FlagAction {
    pub fn is_boolean(&self) -> bool {
        matches!(self, Self::SetTrue | Self::SetFalse)
    }

    pub fn as_boolean_value(&self) -> bool {
        assert!(self.is_boolean());
        self == &Self::SetTrue
    }
}

#[cfg(feature = "std")]
impl From<FlagAction> for clap::ArgAction {
    fn from(action: FlagAction) -> Self {
        match action {
            FlagAction::Set => Self::Set,
            FlagAction::Append => Self::Append,
            FlagAction::SetTrue => Self::SetTrue,
            FlagAction::SetFalse => Self::SetFalse,
            FlagAction::Count => Self::Count,
        }
    }
}

inventory::collect!(CompileFlag);