    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

    // Std
    use std::ops::BitOr;

    // Third party
    use bitflags::bitflags;

    #[allow(unused)]
    use crate::Arg;

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(crate) struct ArgFlags(Flags);

    impl Default for ArgFlags {
        fn default() -> Self {
            Self::empty()
        }
    }

    /// Various settings that apply to arguments and may be set, unset, and checked via getter/setter
    /// methods [`Arg::setting`], [`Arg::unset_setting`], and [`Arg::is_set`]. This is what the
    /// [`Arg`] methods which accept a `bool` use internally.
    ///
    /// [`Arg`]: crate::Arg
    /// [`Arg::setting`]: crate::Arg::setting()
    /// [`Arg::unset_setting`]: crate::Arg::unset_setting()
    /// [`Arg::is_set`]: crate::Arg::is_set()
    #[derive(Debug, PartialEq, Copy, Clone)]
    #[non_exhaustive]
    pub(crate) enum ArgSettings {
        Required,
        Global,
        Hidden,
        NextLineHelp,
        HidePossibleValues,
        AllowHyphenValues,
        AllowNegativeNumbers,
        RequireEquals,
        Last,
        TrailingVarArg,
        HideDefaultValue,
        IgnoreCase,
        #[cfg(feature = "env")]
        HideEnv,
        #[cfg(feature = "env")]
        HideEnvValues,
        HiddenShortHelp,
        HiddenLongHelp,
        Exclusive,
    }

    bitflags! {
        struct Flags: u32 {
            const REQUIRED         = 1;
            const GLOBAL           = 1 << 3;
            const HIDDEN           = 1 << 4;
            const TRAILING_VARARG  = 1 << 5;
            const ALLOW_NEG_NUMS   = 1 << 6;
            const NEXT_LINE_HELP   = 1 << 7;
            const DELIM_NOT_SET    = 1 << 10;
            const HIDE_POS_VALS    = 1 << 11;
            const ALLOW_TAC_VALS   = 1 << 12;
            const REQUIRE_EQUALS   = 1 << 13;
            const LAST             = 1 << 14;
            const HIDE_DEFAULT_VAL = 1 << 15;
            const CASE_INSENSITIVE = 1 << 16;
            #[cfg(feature = "env")]
            const HIDE_ENV_VALS    = 1 << 17;
            const HIDDEN_SHORT_H   = 1 << 18;
            const HIDDEN_LONG_H    = 1 << 19;
            #[cfg(feature = "env")]
            const HIDE_ENV         = 1 << 21;
            const EXCLUSIVE        = 1 << 23;
            const NO_OP            = 0;
        }
    }

    impl_settings! { ArgSettings, ArgFlags,
        Required => Flags::REQUIRED,
        Global => Flags::GLOBAL,
        Hidden => Flags::HIDDEN,
        NextLineHelp => Flags::NEXT_LINE_HELP,
        HidePossibleValues => Flags::HIDE_POS_VALS,
        AllowHyphenValues => Flags::ALLOW_TAC_VALS,
        AllowNegativeNumbers => Flags::ALLOW_NEG_NUMS,
        RequireEquals => Flags::REQUIRE_EQUALS,
        Last => Flags::LAST,
        TrailingVarArg => Flags::TRAILING_VARARG,
        IgnoreCase => Flags::CASE_INSENSITIVE,
        #[cfg(feature = "env")]
        HideEnv => Flags::HIDE_ENV,
        #[cfg(feature = "env")]
        HideEnvValues => Flags::HIDE_ENV_VALS,
        HideDefaultValue => Flags::HIDE_DEFAULT_VAL,
        HiddenShortHelp => Flags::HIDDEN_SHORT_H,
        HiddenLongHelp => Flags::HIDDEN_LONG_H,
        Exclusive => Flags::EXCLUSIVE
    }

    #[cfg(test)]
    mod test {
        use super::*;
        use crate::Arg;

        #[test]
        fn setting() {
            let m = Arg::new("setting").setting(ArgSettings::Required);
            assert!(m.is_required_set());
        }

        #[test]
        fn unset_setting() {
            let m = Arg::new("unset_setting").setting(ArgSettings::Required);
            assert!(m.is_required_set());

            let m = m.unset_setting(ArgSettings::Required);
            assert!(!m.is_required_set(), "{:#?}", m);
        }

        #[test]
        fn setting_bitor() {
            let m = Arg::new("setting_bitor")
                .setting(ArgSettings::Required | ArgSettings::Hidden | ArgSettings::Last);

            assert!(m.is_required_set());
            assert!(m.is_hide_set());
            assert!(m.is_last_set());
        }

        #[test]
        fn unset_setting_bitor() {
            let m = Arg::new("unset_setting_bitor")
                .setting(ArgSettings::Required)
                .setting(ArgSettings::Hidden)
                .setting(ArgSettings::Last);

            assert!(m.is_required_set());
            assert!(m.is_hide_set());
            assert!(m.is_last_set());

            let m = m.unset_setting(ArgSettings::Required | ArgSettings::Hidden | ArgSettings::Last);
            assert!(!m.is_required_set(), "{:#?}", m);
            assert!(!m.is_hide_set(), "{:#?}", m);
            assert!(!m.is_last_set(), "{:#?}", m);
        }
    }