12
12
13
13
//! Simple getopt alternative.
14
14
//!
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.
22
23
//!
23
24
//! Single-character options are expected to appear on the command line with a
24
25
//! single preceding dash; multiple-character options are expected to be
25
26
//! proceeded by two dashes. Options that expect an argument accept their
26
27
//! 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.
28
30
//!
29
31
//! # Usage
30
32
//!
@@ -193,6 +195,17 @@ impl Options {
193
195
/// * `short_name` - e.g. `"h"` for a `-h` option, or `""` for none
194
196
/// * `long_name` - e.g. `"help"` for a `--help` option, or `""` for none
195
197
/// * `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
+ /// ```
196
209
pub fn optflag ( & mut self , short_name : & str , long_name : & str , desc : & str ) -> & mut Options {
197
210
validate_names ( short_name, long_name) ;
198
211
self . grps . push ( OptGroup {
@@ -212,6 +225,17 @@ impl Options {
212
225
/// * `short_name` - e.g. `"h"` for a `-h` option, or `""` for none
213
226
/// * `long_name` - e.g. `"help"` for a `--help` option, or `""` for none
214
227
/// * `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
+ /// ```
215
239
pub fn optflagmulti ( & mut self , short_name : & str , long_name : & str , desc : & str ) -> & mut Options {
216
240
validate_names ( short_name, long_name) ;
217
241
self . grps . push ( OptGroup {
@@ -232,6 +256,20 @@ impl Options {
232
256
/// * `desc` - Description for usage help
233
257
/// * `hint` - Hint that is used in place of the argument in the usage help,
234
258
/// 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
+ /// ```
235
273
pub fn optflagopt (
236
274
& mut self ,
237
275
short_name : & str ,
@@ -259,6 +297,21 @@ impl Options {
259
297
/// * `desc` - Description for usage help
260
298
/// * `hint` - Hint that is used in place of the argument in the usage help,
261
299
/// 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
+ /// ```
262
315
pub fn optmulti (
263
316
& mut self ,
264
317
short_name : & str ,
@@ -285,6 +338,21 @@ impl Options {
285
338
/// * `desc` - Description for usage help
286
339
/// * `hint` - Hint that is used in place of the argument in the usage help,
287
340
/// 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
+ /// ```
288
356
pub fn optopt (
289
357
& mut self ,
290
358
short_name : & str ,
@@ -311,6 +379,23 @@ impl Options {
311
379
/// * `desc` - Description for usage help
312
380
/// * `hint` - Hint that is used in place of the argument in the usage help,
313
381
/// 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
+ /// ```
314
399
pub fn reqopt (
315
400
& mut self ,
316
401
short_name : & str ,
0 commit comments