Skip to content

Commit 1dfa1b0

Browse files
authored
doc,comment: added the descriptions for help printing. (#30)
1 parent 589b0f2 commit 1dfa1b0

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

README.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This library provides the following functionalities:
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.
1313
- Is able to parse command line arguments including sub commands. *(To be added)*
14-
- Generates help text from option configurations. *(To be added)*
14+
- Generates help text from option configurations.
1515

1616
## Install
1717

@@ -123,6 +123,8 @@ The ownership of the vector of option configurations which is passed as an argum
123123
is moved to this method and set to the public field `cfgs` of `Cmd` instance.
124124
If you want to access the option configurations after parsing, get them from this field.
125125

126+
In addition,the help printing for an array of `OptCfg` is generated with `Help`.
127+
126128
```
127129
use cliargs::{Cmd, OptCfg};
128130
use cliargs::OptCfgParam::{names, has_arg, defaults, validator, desc, arg_in_help};
@@ -155,6 +157,18 @@ match cmd.parse_with(opt_cfgs) {
155157
Err(InvalidOption::OptionArgIsInvalid { option, opt_arg, details, .. }) => { /* ... */ },
156158
Err(err) => panic!("Invalid option: {}", err.option()),
157159
}
160+
161+
let opt_cfgs = cmd.opt_cfgs();
162+
163+
let mut help = Help::new();
164+
help.add_text("This is the usage description.".to_string());
165+
help.add_opts_with_margins(opt_cfgs, 2, 0);
166+
help.print();
167+
168+
// (stdout)
169+
// This is the usage description.
170+
// --foo-bar, -f This is description of foo-bar.
171+
// --bar, -z <num> This is description of baz.
158172
```
159173

160174
### Parse for a OptStore struct
@@ -222,7 +236,17 @@ match cmd.parse_for(&mut my_options) {
222236
Err(err) => panic!("Invalid option: {}", err.option()),
223237
}
224238
225-
let opt_cfgs = &cmd.cfgs;
239+
let opt_cfgs = cmd.opt_cfgs();
240+
241+
let mut help = Help::new();
242+
help.add_text("This is the usage description.".to_string());
243+
help.add_opts_with_margins(opt_cfgs, 2, 0);
244+
help.print();
245+
246+
// (stdout)
247+
// This is the usage description.
248+
// -f, --foo-bar This is description of foo_bar.
249+
// -z, --baz <s> This is description of baz.
226250
```
227251

228252
## Supporting Rust versions
@@ -234,7 +258,7 @@ This crate supports Rust 1.74.1 or later.
234258
Fetching index
235259
Determining the Minimum Supported Rust Version (MSRV) for toolchain x86_64-apple-darwin
236260
Using check command cargo check
237-
Finished The MSRV is: 1.74.1 █████████████████████████████████████████████████████████ 00:00:04
261+
Finished The MSRV is: 1.74.1 ████████████████████████████████████████████ 00:00:04
238262
```
239263

240264

src/help/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ impl Help {
7070
/// The margins of this help text block generated by this instance equals the sum of them
7171
/// specified as parameters of this method and them which specified at a constructor.
7272
/// The indent width of this help text block is set to *auto indentation*.
73-
pub fn add_text_with_margins(
74-
&mut self,
75-
text: String,
76-
margin_left: usize,
77-
margin_right: usize,
78-
) {
73+
pub fn add_text_with_margins(&mut self, text: String, margin_left: usize, margin_right: usize) {
7974
self.add_text_with_indent_and_margins(text, 0, margin_left, margin_right)
8075
}
8176

src/lib.rs

+55
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+
//! - Generates help text from option configurations.
1718
//!
1819
//! [posix]: https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html#Argument-Syntax
1920
//! [gnu]: https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html
@@ -145,11 +146,14 @@
145146
//! This crate provides the validator `cliargs::validators::validate_number<T>`
146147
//! which validates whether an option argument is valid format as a number.
147148
//!
149+
//! In addition,the help printing for an array of [OptCfg] is generated with [Help].
150+
//!
148151
//! ```
149152
//! use cliargs::{Cmd, OptCfg};
150153
//! use cliargs::OptCfgParam::{names, has_arg, defaults, validator, desc, arg_in_help};
151154
//! use cliargs::validators::validate_number;
152155
//! use cliargs::errors::InvalidOption;
156+
//! use cliargs::Help;
153157
//!
154158
//! let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
155159
//! let opt_cfgs = vec![
@@ -183,6 +187,18 @@
183187
//! Err(InvalidOption::OptionArgIsInvalid { option, opt_arg, details, .. }) => { /* ... */ },
184188
//! Err(err) => panic!("Invalid option: {}", err.option()),
185189
//! }
190+
//!
191+
//! let opt_cfgs = cmd.opt_cfgs();
192+
//!
193+
//! let mut help = Help::new();
194+
//! help.add_text("This is the usage description.".to_string());
195+
//! help.add_opts_with_margins(opt_cfgs, 2, 0);
196+
//! help.print();
197+
//!
198+
//! // (stdout)
199+
//! // This is the usage description.
200+
//! // --foo-bar, -f This is description of foo-bar.
201+
//! // --bar, -z <num> This is description of baz.
186202
//! ```
187203
//!
188204
//! ## Parse for a OptStore struct
@@ -220,6 +236,45 @@
220236
//! it doesn't represent an array which contains only one empty string.
221237
//! If you want to specify an array which contains only one emtpy string, write nothing after `=` symbol, like
222238
//! `#[opt(cfg="=")]`.
239+
//!
240+
//! ```
241+
//! use cliargs::Cmd;
242+
//! use cliargs::errors::InvalidOption;
243+
//! use cliargs::Help;
244+
//!
245+
//! #[derive(cliargs::OptStore)]
246+
//! struct MyOptions {
247+
//! #[opt(cfg = "f,foo-bar", desc="The description of foo_bar.")]
248+
//! foo_bar: bool,
249+
//! #[opt(cfg = "b,baz", desc="The description of baz.", arg="<s>")]
250+
//! baz: String,
251+
//! }
252+
//! let mut my_options = MyOptions::with_defaults();
253+
//!
254+
//! let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
255+
//! match cmd.parse_for(&mut my_options) {
256+
//! Ok(_) => { /* ... */ },
257+
//! Err(InvalidOption::OptionContainsInvalidChar { option }) => { /* ... */ },
258+
//! Err(InvalidOption::UnconfiguredOption { option }) => { /* ... */ },
259+
//! Err(InvalidOption::OptionNeedsArg { option, .. }) => { /* ... */ },
260+
//! Err(InvalidOption::OptionTakesNoArg { option, .. }) => { /* ... */ },
261+
//! Err(InvalidOption::OptionIsNotArray { option, .. }) => { /* ... */ },
262+
//! Err(InvalidOption::OptionArgIsInvalid { option, opt_arg, details, .. }) => { /* ... */ },
263+
//! Err(err) => panic!("Invalid option: {}", err.option()),
264+
//! }
265+
//!
266+
//! let opt_cfgs = cmd.opt_cfgs();
267+
//!
268+
//! let mut help = Help::new();
269+
//! help.add_text("This is the usage description.".to_string());
270+
//! help.add_opts_with_margins(opt_cfgs, 2, 0);
271+
//! help.print();
272+
//!
273+
//! // (stdout)
274+
//! // This is the usage description.
275+
//! // -f, --foo-bar This is description of foo_bar.
276+
//! // -z, --baz <s> This is description of baz.
277+
//! ```
223278
224279
/// Enums for errors that can occur when parsing command line arguments.
225280
pub mod errors;

0 commit comments

Comments
 (0)