Skip to main content

clap_builder/builder/
arg_settings.rs

1#[allow(unused)]
2use crate::Arg;
3
4#[derive(#[automatically_derived]
impl ::core::default::Default for ArgFlags {
    #[inline]
    fn default() -> ArgFlags { ArgFlags(::core::default::Default::default()) }
}Default, #[automatically_derived]
impl ::core::marker::Copy for ArgFlags { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ArgFlags {
    #[inline]
    fn clone(&self) -> ArgFlags {
        let _: ::core::clone::AssertParamIsClone<u32>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ArgFlags {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "ArgFlags",
            &&self.0)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for ArgFlags {
    #[inline]
    fn eq(&self, other: &ArgFlags) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ArgFlags {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<u32>;
    }
}Eq)]
5pub(crate) struct ArgFlags(u32);
6
7impl ArgFlags {
8    pub(crate) fn set(&mut self, setting: ArgSettings) {
9        self.0 |= setting.bit();
10    }
11
12    pub(crate) fn unset(&mut self, setting: ArgSettings) {
13        self.0 &= !setting.bit();
14    }
15
16    pub(crate) fn is_set(&self, setting: ArgSettings) -> bool {
17        self.0 & setting.bit() != 0
18    }
19
20    pub(crate) fn insert(&mut self, other: Self) {
21        self.0 |= other.0;
22    }
23}
24
25impl std::ops::BitOr for ArgFlags {
26    type Output = Self;
27
28    fn bitor(mut self, rhs: Self) -> Self::Output {
29        self.insert(rhs);
30        self
31    }
32}
33
34/// Various settings that apply to arguments and may be set, unset, and checked via getter/setter
35/// methods [`Arg::setting`], [`Arg::unset_setting`], and [`Arg::is_set`]. This is what the
36/// [`Arg`] methods which accept a `bool` use internally.
37///
38/// [`Arg`]: crate::Arg
39/// [`Arg::setting`]: crate::Arg::setting()
40/// [`Arg::unset_setting`]: crate::Arg::unset_setting()
41/// [`Arg::is_set`]: crate::Arg::is_set()
42#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ArgSettings {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                ArgSettings::Required => "Required",
                ArgSettings::Global => "Global",
                ArgSettings::Hidden => "Hidden",
                ArgSettings::NextLineHelp => "NextLineHelp",
                ArgSettings::HidePossibleValues => "HidePossibleValues",
                ArgSettings::AllowHyphenValues => "AllowHyphenValues",
                ArgSettings::AllowNegativeNumbers => "AllowNegativeNumbers",
                ArgSettings::RequireEquals => "RequireEquals",
                ArgSettings::Last => "Last",
                ArgSettings::TrailingVarArg => "TrailingVarArg",
                ArgSettings::HideDefaultValue => "HideDefaultValue",
                ArgSettings::IgnoreCase => "IgnoreCase",
                ArgSettings::HiddenShortHelp => "HiddenShortHelp",
                ArgSettings::HiddenLongHelp => "HiddenLongHelp",
                ArgSettings::Exclusive => "Exclusive",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for ArgSettings {
    #[inline]
    fn eq(&self, other: &ArgSettings) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::marker::Copy for ArgSettings { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ArgSettings {
    #[inline]
    fn clone(&self) -> ArgSettings { *self }
}Clone)]
43#[repr(u8)]
44pub(crate) enum ArgSettings {
45    Required,
46    Global,
47    Hidden,
48    NextLineHelp,
49    HidePossibleValues,
50    AllowHyphenValues,
51    AllowNegativeNumbers,
52    RequireEquals,
53    Last,
54    TrailingVarArg,
55    HideDefaultValue,
56    IgnoreCase,
57    #[cfg(feature = "env")]
58    HideEnv,
59    #[cfg(feature = "env")]
60    HideEnvValues,
61    HiddenShortHelp,
62    HiddenLongHelp,
63    Exclusive,
64}
65
66impl ArgSettings {
67    fn bit(self) -> u32 {
68        1 << (self as u8)
69    }
70}
71
72#[cfg(test)]
73mod test {
74    use super::*;
75    use crate::Arg;
76
77    #[test]
78    fn setting() {
79        let m = Arg::new("setting").setting(ArgSettings::Required);
80        assert!(m.is_required_set());
81    }
82
83    #[test]
84    fn unset_setting() {
85        let m = Arg::new("unset_setting").setting(ArgSettings::Required);
86        assert!(m.is_required_set());
87
88        let m = m.unset_setting(ArgSettings::Required);
89        assert!(!m.is_required_set(), "{m:#?}");
90    }
91}