Skip to main content

Options

Struct Options 

Source
pub struct Options { /* private fields */ }
Expand description

A description of the options that a program can handle.

Implementations§

Source§

impl Options

Source

pub fn new() -> Options

Create a blank set of options.

Source

pub fn parsing_style(&mut self, style: ParsingStyle) -> &mut Options

Set the parsing style.

Source

pub fn long_only(&mut self, long_only: bool) -> &mut Options

Set or clear “long options only” mode.

In “long options only” mode, short options cannot be clustered together, and long options can be given with either a single “-” or the customary “–”. This mode also changes the meaning of “-a=b”; in the ordinary mode this will parse a short option “-a” with argument “=b”; whereas in long-options-only mode the argument will be simply “b”.

Source

pub fn opt( &mut self, short_name: &str, long_name: &str, desc: &str, hint: &str, hasarg: HasArg, occur: Occur, ) -> &mut Options

Create a generic option group, stating all parameters explicitly.

Source

pub fn optflag( &mut self, short_name: &str, long_name: &str, desc: &str, ) -> &mut Options

Create a long option that is optional and does not take an argument.

  • short_name - e.g. "h" for a -h option, or "" for none
  • long_name - e.g. "help" for a --help option, or "" for none
  • desc - Description for usage help
§Example
let mut opts = Options::new();
opts.optflag("h", "help", "help flag");

let matches = opts.parse(&["-h"]).unwrap();
assert!(matches.opt_present("h"));
Source

pub fn optflagmulti( &mut self, short_name: &str, long_name: &str, desc: &str, ) -> &mut Options

Create a long option that can occur more than once and does not take an argument.

  • short_name - e.g. "h" for a -h option, or "" for none
  • long_name - e.g. "help" for a --help option, or "" for none
  • desc - Description for usage help
§Example
let mut opts = Options::new();
opts.optflagmulti("v", "verbose", "verbosity flag");

let matches = opts.parse(&["-v", "--verbose"]).unwrap();
assert_eq!(2, matches.opt_count("v"));
Source

pub fn optflagopt( &mut self, short_name: &str, long_name: &str, desc: &str, hint: &str, ) -> &mut Options

Create a long option that is optional and takes an optional argument.

  • short_name - e.g. "h" for a -h option, or "" for none
  • long_name - e.g. "help" for a --help option, or "" for none
  • desc - Description for usage help
  • hint - Hint that is used in place of the argument in the usage help, e.g. "FILE" for a -o FILE option
§Example
let mut opts = Options::new();
opts.optflagopt("t", "text", "flag with optional argument", "TEXT");

let matches = opts.parse(&["--text"]).unwrap();
assert_eq!(None, matches.opt_str("text"));

let matches = opts.parse(&["--text=foo"]).unwrap();
assert_eq!(Some("foo".to_owned()), matches.opt_str("text"));
Source

pub fn optmulti( &mut self, short_name: &str, long_name: &str, desc: &str, hint: &str, ) -> &mut Options

Create a long option that is optional, takes an argument, and may occur multiple times.

  • short_name - e.g. "h" for a -h option, or "" for none
  • long_name - e.g. "help" for a --help option, or "" for none
  • desc - Description for usage help
  • hint - Hint that is used in place of the argument in the usage help, e.g. "FILE" for a -o FILE option
§Example
let mut opts = Options::new();
opts.optmulti("t", "text", "text option", "TEXT");

let matches = opts.parse(&["-t", "foo", "--text=bar"]).unwrap();

let values = matches.opt_strs("t");
assert_eq!(2, values.len());
assert_eq!("foo", values[0]);
assert_eq!("bar", values[1]);
Source

pub fn optopt( &mut self, short_name: &str, long_name: &str, desc: &str, hint: &str, ) -> &mut Options

Create a long option that is optional and takes an argument.

  • short_name - e.g. "h" for a -h option, or "" for none
  • long_name - e.g. "help" for a --help option, or "" for none
  • desc - Description for usage help
  • hint - Hint that is used in place of the argument in the usage help, e.g. "FILE" for a -o FILE option
§Example
let mut opts = Options::new();
opts.optopt("o", "optional", "optional text option", "TEXT");

let matches = opts.parse(&["arg1"]).unwrap();
assert_eq!(None, matches.opt_str("optional"));

let matches = opts.parse(&["--optional", "foo", "arg1"]).unwrap();
assert_eq!(Some("foo".to_owned()), matches.opt_str("optional"));
Source

pub fn reqopt( &mut self, short_name: &str, long_name: &str, desc: &str, hint: &str, ) -> &mut Options

Create a long option that is required and takes an argument.

  • short_name - e.g. "h" for a -h option, or "" for none
  • long_name - e.g. "help" for a --help option, or "" for none
  • desc - Description for usage help
  • hint - Hint that is used in place of the argument in the usage help, e.g. "FILE" for a -o FILE option
§Example
let mut opts = Options::new();
opts.optopt("o", "optional", "optional text option", "TEXT");
opts.reqopt("m", "mandatory", "madatory text option", "TEXT");

let result = opts.parse(&["--mandatory", "foo"]);
assert!(result.is_ok());

let result = opts.parse(&["--optional", "foo"]);
assert!(result.is_err());
assert_eq!(Fail::OptionMissing("mandatory".to_owned()), result.unwrap_err());
Source

pub fn parse<C: IntoIterator>(&self, args: C) -> Result
where C::Item: AsRef<OsStr>,

Parse command line arguments according to the provided options.

On success returns Ok(Matches). Use methods such as opt_present opt_str, etc. to interrogate results.

§Panics

Returns Err(Fail) on failure: use the Debug implementation of Fail to display information about it.

Source

pub fn short_usage(&self, program_name: &str) -> String

Derive a short one-line usage summary from a set of long options.

Source

pub fn usage(&self, brief: &str) -> String

Derive a formatted message from a set of options.

Source

pub fn usage_with_format<F: FnMut(&mut dyn Iterator<Item = String>) -> String>( &self, formatter: F, ) -> String

Derive a custom formatted message from a set of options. The formatted options provided to a closure as an iterator.

Trait Implementations§

Source§

impl Clone for Options

Source§

fn clone(&self) -> Options

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Options

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Options

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Options

Source§

impl PartialEq for Options

Source§

fn eq(&self, other: &Options) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Options

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.