Struct Args

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

A dead simple implementation of command line argument parsing and validation.

Implementations§

Source§

impl Args

Source

pub fn new(program_name: &str, description: &str) -> Args

Creates an empty set of command line options.

Source

pub fn flag( &mut self, short_name: &str, long_name: &str, desc: &str, ) -> &mut Args

Registers an optional flag argument that does not take an argument and defaults to false.

  • 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 - A description of the flag for the usage message
Source

pub fn full_usage(&self) -> String

Generates a combination of the short and verbose usage messages.

Source

pub fn has_options(&self) -> bool

Returns a bool indicating whether or not any options are registered.

Source

pub fn has_value(&self, opt_name: &str) -> bool

Returns a bool indicating whether or not a argument is present.

Source

pub fn iter(&self) -> Iter<'_, String, String>

Returns an iterator visiting all key-value pairs in alphabetical order.

Source

pub fn option( &mut self, short_name: &str, long_name: &str, desc: &str, hint: &str, occur: Occur, default: Option<String>, ) -> &mut Args

Registers an option explicitly.

  • 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 - A description of the flag for the usage message
  • hint - A hint to be used in place of the argument in the usage message, e.g. "FILE" for a -o FILE option
  • occur - An enum representing whether the option is required or not
  • default - The default value for this option if there should be one
Source

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

Parses arguments according to the registered options.

§Failures

Fails if any errors are encountered during parsing.

Source

pub fn parse_from_cli(&mut self) -> Result<(), ArgsError>

Parses arguments directly from the command line according to the registered options.

§Failures

Fails if any errors are encountered during parsing.

Source

pub fn short_usage(&self) -> String

Generates a one-line usage summary from the registered options.

Source

pub fn usage(&self) -> String

Generates a verbose usage summary from the registered options.

Source

pub fn optional_validated_value_of<T>( &self, opt_name: &str, validations: &[Box<dyn Validation<T = T>>], ) -> Result<Option<T>, ArgsError>
where T: FromStr,

Retrieves the optional value of the Opt identified by opt_name, casts it to the type specified by T, runs all provided Validations, and wraps it in an Option.

§Failures

See validated_value_of

Source

pub fn optional_value_of<T: FromStr>( &self, opt_name: &str, ) -> Result<Option<T>, ArgsError>

Retrieves the optional value of the Opt identified by opt_name, casts it to the type specified by T and wraps it in an optional.

§Failures

See value_of

Source

pub fn validated_value_of<T>( &self, opt_name: &str, validations: &[Box<dyn Validation<T = T>>], ) -> Result<T, ArgsError>
where T: FromStr,

Retrieves the value of the Opt identified by opt_name, casts it to the type specified by T and then runs all provided Validations.

§Failures

Returns Err(ArgsError) if no Opt correspond to opt_name, if the value cannot be cast to type T or if any validation is considered invalid.

Source

pub fn value_of<T: FromStr>(&self, opt_name: &str) -> Result<T, ArgsError>

Retrieves the value for the Opt identified by opt_name and casts it to the type specified by T.

§Failures

Returns Err(ArgsError) if no Opt corresponds to opt_name or if the value cannot be cast to type T.

Source

pub fn values_of<T: FromStr>(&self, opt_name: &str) -> Result<Vec<T>, ArgsError>

Retrieves a vector of values for the Opt identified by opt_name and casts each of them to the type specified by T.

§Failures

Returns Err(ArgsError) if no Opt corresponds to opt_name or if any of the values cannot be cast to type T.

Source

pub fn get_option(&self, opt_name: &str) -> Option<Box<dyn Opt>>

Retreives an Option<Opt> for the Opt identified by opt_name

§Failures

Returns None if no Opt corresponds to opt_name.

Trait Implementations§

Source§

impl Display for Args

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Args

§

impl !RefUnwindSafe for Args

§

impl Send for Args

§

impl !Sync for Args

§

impl Unpin for Args

§

impl !UnwindSafe for Args

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.