Skip to content

Commit 42a5b2c

Browse files
authored
doc,comment: added descriptions about Cmd#parse_for (#21)
1 parent 52912db commit 42a5b2c

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

README.md

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This library provides the following functionalities:
99
- This library doesn't support numeric short option.
1010
- This library supports not `-ofoo` but `-o=foo` as an alternative to `-o foo` for short option.
1111
- 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.
1313
- Is able to parse command line arguments including sub commands. *(To be added)*
1414
- Generates help text from option configurations. *(To be added)*
1515

@@ -119,6 +119,10 @@ If you want to prioritize the output of short option name first in the help text
119119
`validator` field is to set a function pointer which validates an option argument.
120120
This crate provides the validator `cliargs::validators::validate_number<T>` which validates whether an option argument is valid format as a number.
121121

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+
122126
```
123127
use cliargs::{Cmd, OptCfg};
124128
use cliargs::OptCfgParam::{names, has_arg, defaults, validator, desc, arg_in_help};
@@ -141,7 +145,7 @@ let opt_cfgs = vec![
141145
]),
142146
];
143147
144-
match cmd.parse_with(&opt_cfgs) {
148+
match cmd.parse_with(opt_cfgs) {
145149
Ok(_) => { /* ... */ },
146150
Err(InvalidOption::OptionContainsInvalidChar { option }) => { /* ... */ },
147151
Err(InvalidOption::UnconfiguredOption { option }) => { /* ... */ },
@@ -153,6 +157,73 @@ match cmd.parse_with(&opt_cfgs) {
153157
}
154158
```
155159

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+
```
156227

157228
## Supporting Rust versions
158229

@@ -163,7 +234,7 @@ This crate supports Rust 1.74.1 or later.
163234
Fetching index
164235
Determining the Minimum Supported Rust Version (MSRV) for toolchain x86_64-apple-darwin
165236
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
167238
```
168239

169240

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//! - This library supports not `-ofoo` but `-o=foo` as an alternative to
1313
//! `-o foo` for short option.
1414
//! - Supports parsing with option configurations.
15+
//! - Supports parsing with option configurations made from struct fields and attributes, and
16+
//! setting the option values to them.
1517
//!
1618
//! [posix]: https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html#Argument-Syntax
1719
//! [gnu]: https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html

tests/parse_for_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ mod tests_of_parse_for {
5050

5151
assert_eq!(cmd.cfgs.len(), 1);
5252
assert_eq!(cmd.cfgs[0].store_key, "foo_bar");
53-
assert_eq!(cmd.cfgs[0].names, ["f".to_string(), "b".to_string(), "foo-bar".to_string()]);
53+
assert_eq!(
54+
cmd.cfgs[0].names,
55+
["f".to_string(), "b".to_string(), "foo-bar".to_string()]
56+
);
5457
assert_eq!(cmd.cfgs[0].has_arg, false);
5558
assert_eq!(cmd.cfgs[0].is_array, false);
5659
assert_eq!(cmd.cfgs[0].defaults, None);

0 commit comments

Comments
 (0)