2 use std
::ascii
::AsciiExt
;
8 const MULTIPLE
= 1 << 1;
9 const EMPTY_VALS
= 1 << 2;
10 const GLOBAL
= 1 << 3;
11 const HIDDEN
= 1 << 4;
12 const TAKES_VAL
= 1 << 5;
13 const USE_DELIM
= 1 << 6;
14 const NEXT_LINE_HELP
= 1 << 7;
15 const R_UNLESS_ALL
= 1 << 8;
16 const REQ_DELIM
= 1 << 9;
17 const DELIM_NOT_SET
= 1 << 10;
18 const HIDE_POS_VALS
= 1 << 11;
19 const ALLOW_TAC_VALS
= 1 << 12;
20 const REQUIRE_EQUALS
= 1 << 13;
22 const HIDE_DEFAULT_VAL
= 1 << 15;
27 #[derive(Debug, Clone, Copy)]
28 pub struct ArgFlags(Flags
);
31 pub fn new() -> Self { ArgFlags::default() }
33 impl_settings
!{ArgSettings
,
36 EmptyValues
=> EMPTY_VALS
,
39 TakesValue
=> TAKES_VAL
,
40 UseValueDelimiter
=> USE_DELIM
,
41 NextLineHelp
=> NEXT_LINE_HELP
,
42 RequiredUnlessAll
=> R_UNLESS_ALL
,
43 RequireDelimiter
=> REQ_DELIM
,
44 ValueDelimiterNotSet
=> DELIM_NOT_SET
,
45 HidePossibleValues
=> HIDE_POS_VALS
,
46 AllowLeadingHyphen
=> ALLOW_TAC_VALS
,
47 RequireEquals
=> REQUIRE_EQUALS
,
49 HideDefaultValue
=> HIDE_DEFAULT_VAL
53 impl Default
for ArgFlags
{
54 fn default() -> Self { ArgFlags(EMPTY_VALS | DELIM_NOT_SET) }
57 /// Various settings that apply to arguments and may be set, unset, and checked via getter/setter
58 /// methods [`Arg::set`], [`Arg::unset`], and [`Arg::is_set`]
59 /// [`Arg::set`]: ./struct.Arg.html#method.set
60 /// [`Arg::unset`]: ./struct.Arg.html#method.unset
61 /// [`Arg::is_set`]: ./struct.Arg.html#method.is_set
62 #[derive(Debug, PartialEq, Copy, Clone)]
63 pub enum ArgSettings
{
64 /// The argument must be used
66 /// The argument may be used multiple times such as `--flag --flag`
68 /// The argument allows empty values such as `--option ""`
70 /// The argument should be propagated down through all child [`SubCommands`]
71 /// [`SubCommand`]: ./struct.SubCommand.html
73 /// The argument should **not** be shown in help text
75 /// The argument accepts a value, such as `--option <value>`
77 /// Determines if the argument allows values to be grouped via a delimter
79 /// Prints the help text on the line after the argument
81 /// Requires the use of a value delimiter for all multiple values
83 /// Hides the possible values from the help string
85 /// Allows vals that start with a '-'
87 /// Require options use `--option=val` syntax
89 /// Specifies that the arg is the last positional argument and may be accessed early via `--`
92 /// Hides the default value from the help string
100 impl FromStr
for ArgSettings
{
102 fn from_str(s
: &str) -> Result
<Self, <Self as FromStr
>::Err
> {
103 match &*s
.to_ascii_lowercase() {
104 "required" => Ok(ArgSettings
::Required
),
105 "multiple" => Ok(ArgSettings
::Multiple
),
106 "global" => Ok(ArgSettings
::Global
),
107 "emptyvalues" => Ok(ArgSettings
::EmptyValues
),
108 "hidden" => Ok(ArgSettings
::Hidden
),
109 "takesvalue" => Ok(ArgSettings
::TakesValue
),
110 "usevaluedelimiter" => Ok(ArgSettings
::UseValueDelimiter
),
111 "nextlinehelp" => Ok(ArgSettings
::NextLineHelp
),
112 "requiredunlessall" => Ok(ArgSettings
::RequiredUnlessAll
),
113 "requiredelimiter" => Ok(ArgSettings
::RequireDelimiter
),
114 "valuedelimiternotset" => Ok(ArgSettings
::ValueDelimiterNotSet
),
115 "hidepossiblevalues" => Ok(ArgSettings
::HidePossibleValues
),
116 "allowleadinghyphen" => Ok(ArgSettings
::AllowLeadingHyphen
),
117 "requireequals" => Ok(ArgSettings
::RequireEquals
),
118 "last" => Ok(ArgSettings
::Last
),
119 "hidedefaultvalue" => Ok(ArgSettings
::HideDefaultValue
),
120 _
=> Err("unknown ArgSetting, cannot convert from str".to_owned()),
127 use super::ArgSettings
;
130 fn arg_settings_fromstr() {
131 assert_eq
!("allowleadinghyphen".parse
::<ArgSettings
>().unwrap(),
132 ArgSettings
::AllowLeadingHyphen
);
133 assert_eq
!("emptyvalues".parse
::<ArgSettings
>().unwrap(),
134 ArgSettings
::EmptyValues
);
135 assert_eq
!("global".parse
::<ArgSettings
>().unwrap(),
136 ArgSettings
::Global
);
137 assert_eq
!("hidepossiblevalues".parse
::<ArgSettings
>().unwrap(),
138 ArgSettings
::HidePossibleValues
);
139 assert_eq
!("hidden".parse
::<ArgSettings
>().unwrap(),
140 ArgSettings
::Hidden
);
141 assert_eq
!("multiple".parse
::<ArgSettings
>().unwrap(),
142 ArgSettings
::Multiple
);
143 assert_eq
!("nextlinehelp".parse
::<ArgSettings
>().unwrap(),
144 ArgSettings
::NextLineHelp
);
145 assert_eq
!("requiredunlessall".parse
::<ArgSettings
>().unwrap(),
146 ArgSettings
::RequiredUnlessAll
);
147 assert_eq
!("requiredelimiter".parse
::<ArgSettings
>().unwrap(),
148 ArgSettings
::RequireDelimiter
);
149 assert_eq
!("required".parse
::<ArgSettings
>().unwrap(),
150 ArgSettings
::Required
);
151 assert_eq
!("takesvalue".parse
::<ArgSettings
>().unwrap(),
152 ArgSettings
::TakesValue
);
153 assert_eq
!("usevaluedelimiter".parse
::<ArgSettings
>().unwrap(),
154 ArgSettings
::UseValueDelimiter
);
155 assert_eq
!("valuedelimiternotset".parse
::<ArgSettings
>().unwrap(),
156 ArgSettings
::ValueDelimiterNotSet
);
157 assert_eq
!("requireequals".parse
::<ArgSettings
>().unwrap(),
158 ArgSettings
::RequireEquals
);
159 assert_eq
!("last".parse
::<ArgSettings
>().unwrap(),
161 assert_eq
!("hidedefaultvalue".parse
::<ArgSettings
>().unwrap(),
162 ArgSettings
::HideDefaultValue
);
163 assert
!("hahahaha".parse
::<ArgSettings
>().is_err());