1212
1313//! Simple getopt alternative.
1414//!
15- //! Construct a vector of options, either by using `reqopt`, `optopt`, and
16- //! `optflag` or by building them from components yourself, and pass them to
17- //! `getopts`, along with a vector of actual arguments (not including
18- //! `argv[0]`). You'll either get a failure code back, or a match. You'll have
19- //! to verify whether the amount of 'free' arguments in the match is what you
20- //! expect. Use `opt_*` accessors to get argument values out of the matches
21- //! object.
15+ //! Construct instance of `Options` and configure it by using `reqopt()`,
16+ //! `optopt()` and other methods that add option configuration. Then call
17+ //! `parse()` method and pass into it a vector of actual arguments (not
18+ //! including `argv[0]`).
19+ //!
20+ //! You'll either get a failure code back, or a match. You'll have to verify
21+ //! whether the amount of 'free' arguments in the match is what you expect. Use
22+ //! `opt_*` accessors to get argument values out of the matches object.
2223//!
2324//! Single-character options are expected to appear on the command line with a
2425//! single preceding dash; multiple-character options are expected to be
2526//! proceeded by two dashes. Options that expect an argument accept their
2627//! argument following either a space or an equals sign. Single-character
27- //! options don't require the space.
28+ //! options don't require the space. Everything after double-dash "--" argument
29+ //! is considered to be a 'free' argument, even if it starts with dash.
2830//!
2931//! # Usage
3032//!
@@ -193,6 +195,17 @@ impl Options {
193195 /// * `short_name` - e.g. `"h"` for a `-h` option, or `""` for none
194196 /// * `long_name` - e.g. `"help"` for a `--help` option, or `""` for none
195197 /// * `desc` - Description for usage help
198+ ///
199+ /// # Example
200+ ///
201+ /// ```
202+ /// # use getopts::Options;
203+ /// let mut opts = Options::new();
204+ /// opts.optflag("h", "help", "help flag");
205+ ///
206+ /// let matches = opts.parse(&["-h"]).unwrap();
207+ /// assert!(matches.opt_present("h"));
208+ /// ```
196209 pub fn optflag ( & mut self , short_name : & str , long_name : & str , desc : & str ) -> & mut Options {
197210 validate_names ( short_name, long_name) ;
198211 self . grps . push ( OptGroup {
@@ -212,6 +225,17 @@ impl Options {
212225 /// * `short_name` - e.g. `"h"` for a `-h` option, or `""` for none
213226 /// * `long_name` - e.g. `"help"` for a `--help` option, or `""` for none
214227 /// * `desc` - Description for usage help
228+ ///
229+ /// # Example
230+ ///
231+ /// ```
232+ /// # use getopts::Options;
233+ /// let mut opts = Options::new();
234+ /// opts.optflagmulti("v", "verbose", "verbosity flag");
235+ ///
236+ /// let matches = opts.parse(&["-v", "--verbose"]).unwrap();
237+ /// assert_eq!(2, matches.opt_count("v"));
238+ /// ```
215239 pub fn optflagmulti ( & mut self , short_name : & str , long_name : & str , desc : & str ) -> & mut Options {
216240 validate_names ( short_name, long_name) ;
217241 self . grps . push ( OptGroup {
@@ -232,6 +256,20 @@ impl Options {
232256 /// * `desc` - Description for usage help
233257 /// * `hint` - Hint that is used in place of the argument in the usage help,
234258 /// e.g. `"FILE"` for a `-o FILE` option
259+ ///
260+ /// # Example
261+ ///
262+ /// ```
263+ /// # use getopts::Options;
264+ /// let mut opts = Options::new();
265+ /// opts.optflagopt("t", "text", "flag with optional argument", "TEXT");
266+ ///
267+ /// let matches = opts.parse(&["--text"]).unwrap();
268+ /// assert_eq!(None, matches.opt_str("text"));
269+ ///
270+ /// let matches = opts.parse(&["--text=foo"]).unwrap();
271+ /// assert_eq!(Some("foo".to_owned()), matches.opt_str("text"));
272+ /// ```
235273 pub fn optflagopt (
236274 & mut self ,
237275 short_name : & str ,
@@ -259,6 +297,21 @@ impl Options {
259297 /// * `desc` - Description for usage help
260298 /// * `hint` - Hint that is used in place of the argument in the usage help,
261299 /// e.g. `"FILE"` for a `-o FILE` option
300+ ///
301+ /// # Example
302+ ///
303+ /// ```
304+ /// # use getopts::Options;
305+ /// let mut opts = Options::new();
306+ /// opts.optmulti("t", "text", "text option", "TEXT");
307+ ///
308+ /// let matches = opts.parse(&["-t", "foo", "--text=bar"]).unwrap();
309+ ///
310+ /// let values = matches.opt_strs("t");
311+ /// assert_eq!(2, values.len());
312+ /// assert_eq!("foo", values[0]);
313+ /// assert_eq!("bar", values[1]);
314+ /// ```
262315 pub fn optmulti (
263316 & mut self ,
264317 short_name : & str ,
@@ -285,6 +338,21 @@ impl Options {
285338 /// * `desc` - Description for usage help
286339 /// * `hint` - Hint that is used in place of the argument in the usage help,
287340 /// e.g. `"FILE"` for a `-o FILE` option
341+ ///
342+ /// # Example
343+ ///
344+ /// ```
345+ /// # use getopts::Options;
346+ /// # use getopts::Fail;
347+ /// let mut opts = Options::new();
348+ /// opts.optopt("o", "optional", "optional text option", "TEXT");
349+ ///
350+ /// let matches = opts.parse(&["arg1"]).unwrap();
351+ /// assert_eq!(None, matches.opt_str("optional"));
352+ ///
353+ /// let matches = opts.parse(&["--optional", "foo", "arg1"]).unwrap();
354+ /// assert_eq!(Some("foo".to_owned()), matches.opt_str("optional"));
355+ /// ```
288356 pub fn optopt (
289357 & mut self ,
290358 short_name : & str ,
@@ -311,6 +379,23 @@ impl Options {
311379 /// * `desc` - Description for usage help
312380 /// * `hint` - Hint that is used in place of the argument in the usage help,
313381 /// e.g. `"FILE"` for a `-o FILE` option
382+ ///
383+ /// # Example
384+ ///
385+ /// ```
386+ /// # use getopts::Options;
387+ /// # use getopts::Fail;
388+ /// let mut opts = Options::new();
389+ /// opts.optopt("o", "optional", "optional text option", "TEXT");
390+ /// opts.reqopt("m", "mandatory", "madatory text option", "TEXT");
391+ ///
392+ /// let result = opts.parse(&["--mandatory", "foo"]);
393+ /// assert!(result.is_ok());
394+ ///
395+ /// let result = opts.parse(&["--optional", "foo"]);
396+ /// assert!(result.is_err());
397+ /// assert_eq!(Fail::OptionMissing("mandatory".to_owned()), result.unwrap_err());
398+ /// ```
314399 pub fn reqopt (
315400 & mut self ,
316401 short_name : & str ,
0 commit comments