Skip to content

Commit 90f3fe9

Browse files
committed
Add usage examples for methods that add option config
Provide example usage for methods that configure accepted options. The examples describe how to do basic setup and how to query parsed Matches instance for builder-like methods. The only exception is the opt() method which provides too much freedom of configuration to write short sensible example.
1 parent f79fc64 commit 90f3fe9

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

src/lib.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ impl Options {
194194
/// * `short_name` - e.g. `"h"` for a `-h` option, or `""` for none
195195
/// * `long_name` - e.g. `"help"` for a `--help` option, or `""` for none
196196
/// * `desc` - Description for usage help
197+
///
198+
/// # Example
199+
///
200+
/// ```
201+
/// # use getopts::Options;
202+
/// let mut opts = Options::new();
203+
/// opts.optflag("h", "help", "help flag");
204+
///
205+
/// let matches = opts.parse(&["-h"]).unwrap();
206+
/// assert!(matches.opt_present("h"));
207+
/// ```
197208
pub fn optflag(&mut self, short_name: &str, long_name: &str, desc: &str) -> &mut Options {
198209
validate_names(short_name, long_name);
199210
self.grps.push(OptGroup {
@@ -213,6 +224,17 @@ impl Options {
213224
/// * `short_name` - e.g. `"h"` for a `-h` option, or `""` for none
214225
/// * `long_name` - e.g. `"help"` for a `--help` option, or `""` for none
215226
/// * `desc` - Description for usage help
227+
///
228+
/// # Example
229+
///
230+
/// ```
231+
/// # use getopts::Options;
232+
/// let mut opts = Options::new();
233+
/// opts.optflagmulti("v", "verbose", "verbosity flag");
234+
///
235+
/// let matches = opts.parse(&["-v", "--verbose"]).unwrap();
236+
/// assert_eq!(2, matches.opt_count("v"));
237+
/// ```
216238
pub fn optflagmulti(&mut self, short_name: &str, long_name: &str, desc: &str) -> &mut Options {
217239
validate_names(short_name, long_name);
218240
self.grps.push(OptGroup {
@@ -233,6 +255,20 @@ impl Options {
233255
/// * `desc` - Description for usage help
234256
/// * `hint` - Hint that is used in place of the argument in the usage help,
235257
/// e.g. `"FILE"` for a `-o FILE` option
258+
///
259+
/// # Example
260+
///
261+
/// ```
262+
/// # use getopts::Options;
263+
/// let mut opts = Options::new();
264+
/// opts.optflagopt("t", "text", "flag with optional argument", "TEXT");
265+
///
266+
/// let matches = opts.parse(&["--text"]).unwrap();
267+
/// assert_eq!(None, matches.opt_str("text"));
268+
///
269+
/// let matches = opts.parse(&["--text=foo"]).unwrap();
270+
/// assert_eq!(Some("foo".to_owned()), matches.opt_str("text"));
271+
/// ```
236272
pub fn optflagopt(
237273
&mut self,
238274
short_name: &str,
@@ -260,6 +296,21 @@ impl Options {
260296
/// * `desc` - Description for usage help
261297
/// * `hint` - Hint that is used in place of the argument in the usage help,
262298
/// e.g. `"FILE"` for a `-o FILE` option
299+
///
300+
/// # Example
301+
///
302+
/// ```
303+
/// # use getopts::Options;
304+
/// let mut opts = Options::new();
305+
/// opts.optmulti("t", "text", "text option", "TEXT");
306+
///
307+
/// let matches = opts.parse(&["-t", "foo", "--text=bar"]).unwrap();
308+
///
309+
/// let values = matches.opt_strs("t");
310+
/// assert_eq!(2, values.len());
311+
/// assert_eq!("foo", values[0]);
312+
/// assert_eq!("bar", values[1]);
313+
/// ```
263314
pub fn optmulti(
264315
&mut self,
265316
short_name: &str,
@@ -286,6 +337,21 @@ impl Options {
286337
/// * `desc` - Description for usage help
287338
/// * `hint` - Hint that is used in place of the argument in the usage help,
288339
/// e.g. `"FILE"` for a `-o FILE` option
340+
///
341+
/// # Example
342+
///
343+
/// ```
344+
/// # use getopts::Options;
345+
/// # use getopts::Fail;
346+
/// let mut opts = Options::new();
347+
/// opts.optopt("o", "optional", "optional text option", "TEXT");
348+
///
349+
/// let matches = opts.parse(&["arg1"]).unwrap();
350+
/// assert_eq!(None, matches.opt_str("optional"));
351+
///
352+
/// let matches = opts.parse(&["--optional", "foo", "arg1"]).unwrap();
353+
/// assert_eq!(Some("foo".to_owned()), matches.opt_str("optional"));
354+
/// ```
289355
pub fn optopt(
290356
&mut self,
291357
short_name: &str,
@@ -312,6 +378,23 @@ impl Options {
312378
/// * `desc` - Description for usage help
313379
/// * `hint` - Hint that is used in place of the argument in the usage help,
314380
/// e.g. `"FILE"` for a `-o FILE` option
381+
///
382+
/// # Example
383+
///
384+
/// ```
385+
/// # use getopts::Options;
386+
/// # use getopts::Fail;
387+
/// let mut opts = Options::new();
388+
/// opts.optopt("o", "optional", "optional text option", "TEXT");
389+
/// opts.reqopt("m", "mandatory", "madatory text option", "TEXT");
390+
///
391+
/// let result = opts.parse(&["--mandatory", "foo"]);
392+
/// assert!(result.is_ok());
393+
///
394+
/// let result = opts.parse(&["--optional", "foo"]);
395+
/// assert!(result.is_err());
396+
/// assert_eq!(Fail::OptionMissing("mandatory".to_owned()), result.unwrap_err());
397+
/// ```
315398
pub fn reqopt(
316399
&mut self,
317400
short_name: &str,

0 commit comments

Comments
 (0)