|
14 | 14 | //! - Supports parsing with option configurations.
|
15 | 15 | //! - Supports parsing with option configurations made from struct fields and attributes, and
|
16 | 16 | //! setting the option values to them.
|
| 17 | +//! - Generates help text from option configurations. |
17 | 18 | //!
|
18 | 19 | //! [posix]: https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html#Argument-Syntax
|
19 | 20 | //! [gnu]: https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html
|
|
145 | 146 | //! This crate provides the validator `cliargs::validators::validate_number<T>`
|
146 | 147 | //! which validates whether an option argument is valid format as a number.
|
147 | 148 | //!
|
| 149 | +//! In addition,the help printing for an array of [OptCfg] is generated with [Help]. |
| 150 | +//! |
148 | 151 | //! ```
|
149 | 152 | //! use cliargs::{Cmd, OptCfg};
|
150 | 153 | //! use cliargs::OptCfgParam::{names, has_arg, defaults, validator, desc, arg_in_help};
|
151 | 154 | //! use cliargs::validators::validate_number;
|
152 | 155 | //! use cliargs::errors::InvalidOption;
|
| 156 | +//! use cliargs::Help; |
153 | 157 | //!
|
154 | 158 | //! let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
|
155 | 159 | //! let opt_cfgs = vec![
|
|
183 | 187 | //! Err(InvalidOption::OptionArgIsInvalid { option, opt_arg, details, .. }) => { /* ... */ },
|
184 | 188 | //! Err(err) => panic!("Invalid option: {}", err.option()),
|
185 | 189 | //! }
|
| 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. |
186 | 202 | //! ```
|
187 | 203 | //!
|
188 | 204 | //! ## Parse for a OptStore struct
|
|
220 | 236 | //! it doesn't represent an array which contains only one empty string.
|
221 | 237 | //! If you want to specify an array which contains only one emtpy string, write nothing after `=` symbol, like
|
222 | 238 | //! `#[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 | +//! ``` |
223 | 278 |
|
224 | 279 | /// Enums for errors that can occur when parsing command line arguments.
|
225 | 280 | pub mod errors;
|
|
0 commit comments