1 | #[allow (unused)] |
2 | use crate::Arg; |
3 | |
4 | #[derive (Default, Copy, Clone, Debug, PartialEq, Eq)] |
5 | pub(crate) struct ArgFlags(u32); |
6 | |
7 | impl 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 | |
25 | impl 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 (Debug, PartialEq, Copy, Clone)] |
43 | #[repr (u8)] |
44 | pub(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 | |
66 | impl ArgSettings { |
67 | fn bit(self) -> u32 { |
68 | 1 << (self as u8) |
69 | } |
70 | } |
71 | |
72 | #[cfg (test)] |
73 | mod 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 | } |
92 | |