    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

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

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

    // Third party
    use bitflags::bitflags;

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

    impl Default for AppFlags {
        fn default() -> Self {
            AppFlags(Flags::COLOR_AUTO)
        }
    }

    /// Application level settings, which affect how [`Command`] operates
    ///
    /// **NOTE:** When these settings are used, they apply only to current command, and are *not*
    /// propagated down or up through child or parent subcommands
    ///
    /// [`Command`]: crate::Command
    #[derive(Debug, PartialEq, Copy, Clone)]
    #[non_exhaustive]
    pub(crate) enum AppSettings {
        IgnoreErrors,
        AllowHyphenValues,
        AllowNegativeNumbers,
        AllArgsOverrideSelf,
        AllowMissingPositional,
        TrailingVarArg,
        DontDelimitTrailingValues,
        InferLongArgs,
        InferSubcommands,
        SubcommandRequired,
        AllowExternalSubcommands,
        Multicall,
        SubcommandsNegateReqs,
        ArgsNegateSubcommands,
        SubcommandPrecedenceOverArg,
        ArgRequiredElseHelp,
        NextLineHelp,
        DisableColoredHelp,
        DisableHelpFlag,
        DisableHelpSubcommand,
        DisableVersionFlag,
        PropagateVersion,
        Hidden,
        HidePossibleValues,
        HelpExpected,
        NoBinaryName,
        #[allow(dead_code)]
        ColorAuto,
        ColorAlways,
        ColorNever,
        Built,
        BinNameBuilt,
    }

    bitflags! {
        struct Flags: u64 {
            const SC_NEGATE_REQS                 = 1;
            const SC_REQUIRED                    = 1 << 1;
            const ARG_REQUIRED_ELSE_HELP         = 1 << 2;
            const PROPAGATE_VERSION              = 1 << 3;
            const DISABLE_VERSION_FOR_SC         = 1 << 4;
            const WAIT_ON_ERROR                  = 1 << 6;
            const DISABLE_VERSION_FLAG           = 1 << 10;
            const HIDDEN                         = 1 << 11;
            const TRAILING_VARARG                = 1 << 12;
            const NO_BIN_NAME                    = 1 << 13;
            const ALLOW_UNK_SC                   = 1 << 14;
            const LEADING_HYPHEN                 = 1 << 16;
            const NO_POS_VALUES                  = 1 << 17;
            const NEXT_LINE_HELP                 = 1 << 18;
            const DISABLE_COLORED_HELP           = 1 << 20;
            const COLOR_ALWAYS                   = 1 << 21;
            const COLOR_AUTO                     = 1 << 22;
            const COLOR_NEVER                    = 1 << 23;
            const DONT_DELIM_TRAIL               = 1 << 24;
            const ALLOW_NEG_NUMS                 = 1 << 25;
            const DISABLE_HELP_SC                = 1 << 27;
            const ARGS_NEGATE_SCS                = 1 << 29;
            const PROPAGATE_VALS_DOWN            = 1 << 30;
            const ALLOW_MISSING_POS              = 1 << 31;
            const TRAILING_VALUES                = 1 << 32;
            const BUILT                          = 1 << 33;
            const BIN_NAME_BUILT                 = 1 << 34;
            const VALID_ARG_FOUND                = 1 << 35;
            const INFER_SUBCOMMANDS              = 1 << 36;
            const CONTAINS_LAST                  = 1 << 37;
            const ARGS_OVERRIDE_SELF             = 1 << 38;
            const HELP_REQUIRED                  = 1 << 39;
            const SUBCOMMAND_PRECEDENCE_OVER_ARG = 1 << 40;
            const DISABLE_HELP_FLAG              = 1 << 41;
            const INFER_LONG_ARGS                = 1 << 43;
            const IGNORE_ERRORS                  = 1 << 44;
            const MULTICALL                      = 1 << 45;
            const EXPAND_HELP_SUBCOMMAND_TREES   = 1 << 46;
            const NO_OP                          = 0;
        }
    }

    impl_settings! { AppSettings, AppFlags,
        ArgRequiredElseHelp
            => Flags::ARG_REQUIRED_ELSE_HELP,
        SubcommandPrecedenceOverArg
            => Flags::SUBCOMMAND_PRECEDENCE_OVER_ARG,
        ArgsNegateSubcommands
            => Flags::ARGS_NEGATE_SCS,
        AllowExternalSubcommands
            => Flags::ALLOW_UNK_SC,
        AllowHyphenValues
            => Flags::LEADING_HYPHEN,
        AllowNegativeNumbers
            => Flags::ALLOW_NEG_NUMS,
        AllowMissingPositional
            => Flags::ALLOW_MISSING_POS,
        ColorAlways
            => Flags::COLOR_ALWAYS,
        ColorAuto
            => Flags::COLOR_AUTO,
        ColorNever
            => Flags::COLOR_NEVER,
        DontDelimitTrailingValues
            => Flags::DONT_DELIM_TRAIL,
        DisableColoredHelp
            => Flags::DISABLE_COLORED_HELP,
        DisableHelpSubcommand
            => Flags::DISABLE_HELP_SC,
        DisableHelpFlag
            => Flags::DISABLE_HELP_FLAG,
        DisableVersionFlag
            => Flags::DISABLE_VERSION_FLAG,
        PropagateVersion
            => Flags::PROPAGATE_VERSION,
        HidePossibleValues
            => Flags::NO_POS_VALUES,
        HelpExpected
            => Flags::HELP_REQUIRED,
        Hidden
            => Flags::HIDDEN,
        Multicall
            => Flags::MULTICALL,
        NoBinaryName
            => Flags::NO_BIN_NAME,
        SubcommandsNegateReqs
            => Flags::SC_NEGATE_REQS,
        SubcommandRequired
            => Flags::SC_REQUIRED,
        TrailingVarArg
            => Flags::TRAILING_VARARG,
        NextLineHelp
            => Flags::NEXT_LINE_HELP,
        IgnoreErrors
            => Flags::IGNORE_ERRORS,
        Built
            => Flags::BUILT,
        BinNameBuilt
            => Flags::BIN_NAME_BUILT,
        InferSubcommands
            => Flags::INFER_SUBCOMMANDS,
        AllArgsOverrideSelf
            => Flags::ARGS_OVERRIDE_SELF,
        InferLongArgs
            => Flags::INFER_LONG_ARGS
    }