@@ -9,7 +9,7 @@ This library provides the following functionalities:
9
9
- This library doesn't support numeric short option.
10
10
- This library supports not ` -ofoo ` but ` -o=foo ` as an alternative to ` -o foo ` for short option.
11
11
- Supports parsing with option configurations.
12
- - Supports parsing with an object which stores option values and has annotations of fields. * (To be added) *
12
+ - Supports parsing with option configurations made from struct fields and attributes, and setting the option values to them.
13
13
- Is able to parse command line arguments including sub commands. * (To be added)*
14
14
- Generates help text from option configurations. * (To be added)*
15
15
@@ -119,6 +119,10 @@ If you want to prioritize the output of short option name first in the help text
119
119
` validator ` field is to set a function pointer which validates an option argument.
120
120
This crate provides the validator ` cliargs::validators::validate_number<T> ` which validates whether an option argument is valid format as a number.
121
121
122
+ The ownership of the vector of option configurations which is passed as an argument of this method
123
+ is moved to this method and set to the public field ` cfgs ` of [ Cmd] instance.
124
+ If you want to access the option configurations after parsing, get them from this field.
125
+
122
126
```
123
127
use cliargs::{Cmd, OptCfg};
124
128
use cliargs::OptCfgParam::{names, has_arg, defaults, validator, desc, arg_in_help};
@@ -141,7 +145,7 @@ let opt_cfgs = vec![
141
145
]),
142
146
];
143
147
144
- match cmd.parse_with(& opt_cfgs) {
148
+ match cmd.parse_with(opt_cfgs) {
145
149
Ok(_) => { /* ... */ },
146
150
Err(InvalidOption::OptionContainsInvalidChar { option }) => { /* ... */ },
147
151
Err(InvalidOption::UnconfiguredOption { option }) => { /* ... */ },
@@ -153,6 +157,73 @@ match cmd.parse_with(&opt_cfgs) {
153
157
}
154
158
```
155
159
160
+ ### Parse for a OptStore struct
161
+
162
+ The ` Cmd ` struct has the method ` parse_for ` which parses command line arguments and set their
163
+ option values to the option store which is passed as an argument.
164
+
165
+ This method divides command line arguments to command arguments and options, then sets
166
+ each option value to a curresponding field of the option store.
167
+
168
+ Within this method, a vector of ` OptCfg ` is made from the fields of the option store.
169
+ This [ OptCfg] vector is set to the public field ` cfgs ` of the ` Cmd ` instance.
170
+ If you want to access this option configurations, get them from this field.
171
+
172
+ An option configuration corresponding to each field of an option store is determined by
173
+ its type and ` opt ` field attribute.
174
+ If the type is bool, the option takes no argument.
175
+ If the type is integer, floating point number or string, the option can takes single option
176
+ argument, therefore it can appear once in command line arguments.
177
+ If the type is a vector, the option can takes multiple option arguments, therefore it can
178
+ appear multiple times in command line arguments.
179
+
180
+ A ` opt ` field attribute can have the following pairs of name and value: one is ` cfg ` to
181
+ specify ` names ` and ` defaults ` fields of ` OptCfg ` struct, another is ` desc ` to specify
182
+ ` desc ` field, and yet another is ` arg ` to specify ` arg_in_help ` field.
183
+
184
+ The format of ` cfg ` is like ` cfg="f,foo=123" ` .
185
+ The left side of the equal sign is the option name(s), and the right side is the default
186
+ value(s).
187
+ If there is no equal sign, it is determined that only the option name is specified.
188
+ If you want to specify multiple option names, separate them with commas.
189
+ If you want to specify multiple default values, separate them with commas and round them
190
+ with square brackets, like ` [1,2,3] ` .
191
+ If you want to use your favorite carachter as a separator, you can use it by putting it on
192
+ the left side of the open square bracket, like ` /[1/2/3] ` .
193
+
194
+ NOTE: A default value of empty string array option in a field attribute is ` [] ` , like
195
+ ` #[opt(cfg="=[]")] ` , but it doesn't represent an array which contains only one empty
196
+ string.
197
+ If you want to specify an array which contains only one emtpy string, write nothing after
198
+ ` = ` symbol, like ` #[opt(cfg="=")] ` .
199
+
200
+ ```
201
+ use cliargs::Cmd;
202
+ use cliargs::errors::InvalidOption;
203
+
204
+ #[derive(cliargs::OptStore)]
205
+ struct MyOptions {
206
+ #[opt(cfg = "f,foo-bar", desc="The description of foo_bar.")]
207
+ foo_bar: bool,
208
+ #[opt(cfg = "b,baz", desc="The description of baz.", arg="<s>")]
209
+ baz: String,
210
+ }
211
+ let mut my_options = MyOptions::with_defaults();
212
+
213
+ let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
214
+ match cmd.parse_for(&mut my_options) {
215
+ Ok(_) => { /* ... */ },
216
+ Err(InvalidOption::OptionContainsInvalidChar { option }) => { /* ... */ },
217
+ Err(InvalidOption::UnconfiguredOption { option }) => { /* ... */ },
218
+ Err(InvalidOption::OptionNeedsArg { option, .. }) => { /* ... */ },
219
+ Err(InvalidOption::OptionTakesNoArg { option, .. }) => { /* ... */ },
220
+ Err(InvalidOption::OptionIsNotArray { option, .. }) => { /* ... */ },
221
+ Err(InvalidOption::OptionArgIsInvalid { option, opt_arg, details, .. }) => { /* ... */ },
222
+ Err(err) => panic!("Invalid option: {}", err.option()),
223
+ }
224
+
225
+ let opt_cfgs = &cmd.cfgs;
226
+ ```
156
227
157
228
## Supporting Rust versions
158
229
@@ -163,7 +234,7 @@ This crate supports Rust 1.74.1 or later.
163
234
Fetching index
164
235
Determining the Minimum Supported Rust Version (MSRV) for toolchain x86_64-apple-darwin
165
236
Using check command cargo check
166
- Finished The MSRV is: 1.74.1 █████████████████████████████████████ 00:00:02
237
+ Finished The MSRV is: 1.74.1 █████████████████████████████████████████████████████████ 00:00:04
167
238
```
168
239
169
240
0 commit comments