Skip to content

Commit 18edacf

Browse files
authored
doc,comment: added descriptioins about supporting sub command (#37)
1 parent 85aa49a commit 18edacf

File tree

2 files changed

+81
-16
lines changed

2 files changed

+81
-16
lines changed

README.md

+41-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This library provides the following functionalities:
1010
- This library supports not `-ofoo` but `-o=foo` as an alternative to `-o foo` for short option.
1111
- Supports parsing with option configurations.
1212
- Supports parsing with option configurations made from struct fields and attributes, and setting the option values to them.
13-
- Is able to parse command line arguments including sub commands. *(To be added)*
13+
- Supports parsing command line arguments including sub commands.
1414
- Generates help text from option configurations.
1515

1616
## Install
@@ -32,7 +32,7 @@ The usage of this `Cmd` struct is as follows:
3232
The `Cmd::new` function creates a `Cmd` instance with original command line arguments.
3333
This function uses `std::env::args_os` and `OsString#into_string` to read command line arguments in order to avoid `panic!` call that the user cannot control.
3434

35-
```
35+
```rust
3636
use cliargs::Cmd;
3737
use cliargs::errors::InvalidOsArg;
3838

@@ -48,7 +48,7 @@ let cmd = match Cmd::new() {
4848

4949
The `Cmd::with_strings` function creates a `Cmd` instance with the specified `String` array.
5050

51-
```
51+
```rust
5252
use cliargs::Cmd;
5353
use std::env;
5454

@@ -59,7 +59,7 @@ let cmd = Cmd::with_strings(env::args());
5959

6060
The `Cmd::with_os_strings` function creates a `Cmd` instance with the specified `OsString` array.
6161

62-
```
62+
```rust
6363
use cliargs::Cmd;
6464
use cliargs::errors::InvalidOsArg;
6565
use std::env;
@@ -82,7 +82,7 @@ If you want to specify a value to an option, follows `"="` and the value after t
8282

8383
All command line arguments after `--` are command arguments, even they starts with `-` or `--`.
8484

85-
```
85+
```rust
8686
use cliargs::Cmd;
8787
use cliargs::errors::InvalidOption;
8888

@@ -108,7 +108,7 @@ If this field is not specified, the first element of `names` field is used inste
108108

109109
`names` field is a string array and specified the option names, that are both long options and short options.
110110
The order of elements in this field is used in a help text.
111-
If you want to prioritize the output of short option name first in the help text, like `-f, --foo-bar`, but use the long option name as the key in the option map, write `store_key` and `names` fields as follows: `OptCfg::with(&[store_key("foo-bar"), names(&["f", "foo-bar"])])`.
111+
If you want to prioritize the output of short option name first in the help text, like `-f, --foo-bar`, but use the long option name as the key in the option map, write `store_key` and `names` fields as follows: `OptCfg::with([store_key("foo-bar"), names(&["f", "foo-bar"])])`.
112112

113113
`has_arg` field indicates the option requires one or more values.
114114
`is_array` field indicates the option can have multiple values.
@@ -125,19 +125,19 @@ If you want to access the option configurations after parsing, get them from thi
125125

126126
In addition,the help printing for an array of `OptCfg` is generated with `Help`.
127127

128-
```
128+
```rust
129129
use cliargs::{Cmd, OptCfg};
130130
use cliargs::OptCfgParam::{names, has_arg, defaults, validator, desc, arg_in_help};
131131
use cliargs::validators::validate_number;
132132
use cliargs::errors::InvalidOption;
133133

134134
let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
135135
let opt_cfgs = vec![
136-
OptCfg::with(&[
136+
OptCfg::with([
137137
names(&["foo-bar"]),
138138
desc("This is description of foo-bar."),
139139
]),
140-
OptCfg::with(&[
140+
OptCfg::with([
141141
names(&["baz", "z"]),
142142
has_arg(true),
143143
defaults(&["1"]),
@@ -211,7 +211,7 @@ string.
211211
If you want to specify an array which contains only one emtpy string, write nothing after
212212
`=` symbol, like `#[opt(cfg="=")]`.
213213

214-
```
214+
```rust
215215
use cliargs::Cmd;
216216
use cliargs::errors::InvalidOption;
217217

@@ -249,11 +249,41 @@ help.print();
249249
// -z, --baz <s> This is description of baz.
250250
```
251251

252+
### Supports parsing command line arguments including sub commands
253+
254+
This crate provides methods `Cmd#parse_until_sub_cmd`, `Cmd#parse_until_sub_cmd_with`, and `Cmd#parse_until_sub_cmd_for` for parsing command line arguments including sub commands.
255+
256+
These methods correspond to `Cmd#parse`, `Cmd#parse_with`, and `Cmd#parse_for`, respectively, and behave the same except that they stop parsing before the first command argument (= sub command) and return a `Cmd` instance containing the arguments starting from the the sub command.
257+
258+
The folowing is an example code using `Cmd#parse_until_sub_cmd`:
259+
260+
```rust
261+
use cliargs::Cmd;
262+
use cliargs::errors::InvalidOption;
263+
264+
let mut cmd = Cmd::with_strings([ /* ... */ ]);
265+
266+
match cmd.parse_until_sub_cmd() {
267+
Ok(Some(mut sub_cmd)) => {
268+
let sub_cmd_name = sub_cmd.name();
269+
match sub_cmd.parse() {
270+
Ok(_) => { /* ... */ },
271+
Err(err) => panic!("Invalid option: {}", err.option()),
272+
}
273+
},
274+
Ok(None) => { /* ... */ },
275+
Err(InvalidOption::OptionContainsInvalidChar { option }) => {
276+
panic!("Option contains invalid character: {option}");
277+
},
278+
Err(err) => panic!("Invalid option: {}", err.option()),
279+
}
280+
```
281+
252282
## Supporting Rust versions
253283

254284
This crate supports Rust 1.74.1 or later.
255285

256-
```
286+
```sh
257287
% cargo msrv --no-check-feedback
258288
Fetching index
259289
Determining the Minimum Supported Rust Version (MSRV) for toolchain x86_64-apple-darwin

src/lib.rs

+40-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! - Supports parsing with option configurations.
1515
//! - Supports parsing with option configurations made from struct fields and attributes, and
1616
//! setting the option values to them.
17+
//! - Supports parsing command line arguments including sub commands.
1718
//! - Generates help text from option configurations.
1819
//!
1920
//! [posix]: https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html#Argument-Syntax
@@ -99,7 +100,7 @@
99100
//! use cliargs::Cmd;
100101
//! use cliargs::errors::InvalidOption;
101102
//!
102-
//! let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
103+
//! let mut cmd = Cmd::with_strings([ /* ... */ ]);
103104
//! match cmd.parse() {
104105
//! Ok(_) => { /* ... */ },
105106
//! Err(InvalidOption::OptionContainsInvalidChar { option }) => {
@@ -148,14 +149,14 @@
148149
//!
149150
//! In addition,the help printing for an array of [OptCfg] is generated with [Help].
150151
//!
151-
//! ```
152+
//! ```rust
152153
//! use cliargs::{Cmd, OptCfg};
153154
//! use cliargs::OptCfgParam::{names, has_arg, defaults, validator, desc, arg_in_help};
154155
//! use cliargs::validators::validate_number;
155156
//! use cliargs::errors::InvalidOption;
156157
//! use cliargs::Help;
157158
//!
158-
//! let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
159+
//! let mut cmd = Cmd::with_strings([ /* ... */ ]);
159160
//! let opt_cfgs = vec![
160161
//! OptCfg {
161162
//! store_key: "foo_bar".to_string(),
@@ -237,7 +238,7 @@
237238
//! If you want to specify an array which contains only one emtpy string, write nothing after `=` symbol, like
238239
//! `#[opt(cfg="=")]`.
239240
//!
240-
//! ```
241+
//! ```rust
241242
//! use cliargs::Cmd;
242243
//! use cliargs::errors::InvalidOption;
243244
//! use cliargs::Help;
@@ -251,7 +252,7 @@
251252
//! }
252253
//! let mut my_options = MyOptions::with_defaults();
253254
//!
254-
//! let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
255+
//! let mut cmd = Cmd::with_strings([ /* ... */ ]);
255256
//! match cmd.parse_for(&mut my_options) {
256257
//! Ok(_) => { /* ... */ },
257258
//! Err(InvalidOption::OptionContainsInvalidChar { option }) => { /* ... */ },
@@ -275,6 +276,40 @@
275276
//! // -f, --foo-bar This is description of foo_bar.
276277
//! // -z, --baz <s> This is description of baz.
277278
//! ```
279+
//!
280+
//! ## Parse command line arguments including sub command
281+
//!
282+
//! This crate provides methods [Cmd::parse_until_sub_cmd], [Cmd::parse_until_sub_cmd_with], and
283+
//! [Cmd::parse_until_sub_cmd_for] for parsing command line arguments including sub commands.
284+
//!
285+
//! These methods correspond to [Cmd::parse], [Cmd::parse_with], and [Cmd::parse_for],
286+
//! respectively, and behave the same except that they stop parsing before the first command
287+
//! argument (= sub command) and
288+
//! return a [Cmd] instance containing the arguments starting from the the sub command.
289+
//!
290+
//! The folowing is an example code using [Cmd::parse_until_sub_cmd]:
291+
//!
292+
//! ```rust
293+
//! use cliargs::Cmd;
294+
//! use cliargs::errors::InvalidOption;
295+
//!
296+
//! let mut cmd = Cmd::with_strings([ /* ... */ ]);
297+
//!
298+
//! match cmd.parse_until_sub_cmd() {
299+
//! Ok(Some(mut sub_cmd)) => {
300+
//! let sub_cmd_name = sub_cmd.name();
301+
//! match sub_cmd.parse() {
302+
//! Ok(_) => { /* ... */ },
303+
//! Err(err) => panic!("Invalid option: {}", err.option()),
304+
//! }
305+
//! },
306+
//! Ok(None) => { /* ... */ },
307+
//! Err(InvalidOption::OptionContainsInvalidChar { option }) => {
308+
//! panic!("Option contains invalid character: {option}");
309+
//! },
310+
//! Err(err) => panic!("Invalid option: {}", err.option()),
311+
//! }
312+
//! ```
278313
279314
/// Enums for errors that can occur when parsing command line arguments.
280315
pub mod errors;

0 commit comments

Comments
 (0)