@@ -194,6 +194,17 @@ impl Options {
194
194
/// * `short_name` - e.g. `"h"` for a `-h` option, or `""` for none
195
195
/// * `long_name` - e.g. `"help"` for a `--help` option, or `""` for none
196
196
/// * `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
+ /// ```
197
208
pub fn optflag ( & mut self , short_name : & str , long_name : & str , desc : & str ) -> & mut Options {
198
209
validate_names ( short_name, long_name) ;
199
210
self . grps . push ( OptGroup {
@@ -213,6 +224,17 @@ impl Options {
213
224
/// * `short_name` - e.g. `"h"` for a `-h` option, or `""` for none
214
225
/// * `long_name` - e.g. `"help"` for a `--help` option, or `""` for none
215
226
/// * `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
+ /// ```
216
238
pub fn optflagmulti ( & mut self , short_name : & str , long_name : & str , desc : & str ) -> & mut Options {
217
239
validate_names ( short_name, long_name) ;
218
240
self . grps . push ( OptGroup {
@@ -233,6 +255,20 @@ impl Options {
233
255
/// * `desc` - Description for usage help
234
256
/// * `hint` - Hint that is used in place of the argument in the usage help,
235
257
/// 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
+ /// ```
236
272
pub fn optflagopt (
237
273
& mut self ,
238
274
short_name : & str ,
@@ -260,6 +296,21 @@ impl Options {
260
296
/// * `desc` - Description for usage help
261
297
/// * `hint` - Hint that is used in place of the argument in the usage help,
262
298
/// 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
+ /// ```
263
314
pub fn optmulti (
264
315
& mut self ,
265
316
short_name : & str ,
@@ -286,6 +337,21 @@ impl Options {
286
337
/// * `desc` - Description for usage help
287
338
/// * `hint` - Hint that is used in place of the argument in the usage help,
288
339
/// 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
+ /// ```
289
355
pub fn optopt (
290
356
& mut self ,
291
357
short_name : & str ,
@@ -312,6 +378,23 @@ impl Options {
312
378
/// * `desc` - Description for usage help
313
379
/// * `hint` - Hint that is used in place of the argument in the usage help,
314
380
/// 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
+ /// ```
315
398
pub fn reqopt (
316
399
& mut self ,
317
400
short_name : & str ,
0 commit comments