pub struct Args { /* private fields */ }
Expand description
A dead simple implementation of command line argument parsing and validation.
Implementations§
Source§impl Args
impl Args
Sourcepub fn new(program_name: &str, description: &str) -> Args
pub fn new(program_name: &str, description: &str) -> Args
Creates an empty set of command line options.
Sourcepub fn flag(
&mut self,
short_name: &str,
long_name: &str,
desc: &str,
) -> &mut Args
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 nonelong_name
- e.g."help"
for a--help
option, or""
for nonedesc
- A description of the flag for the usage message
Sourcepub fn full_usage(&self) -> String
pub fn full_usage(&self) -> String
Generates a combination of the short and verbose usage messages.
Sourcepub fn has_options(&self) -> bool
pub fn has_options(&self) -> bool
Returns a bool
indicating whether or not any options are registered.
Sourcepub fn has_value(&self, opt_name: &str) -> bool
pub fn has_value(&self, opt_name: &str) -> bool
Returns a bool
indicating whether or not a argument is present.
Sourcepub fn iter(&self) -> Iter<'_, String, String>
pub fn iter(&self) -> Iter<'_, String, String>
Returns an iterator visiting all key-value pairs in alphabetical order.
Sourcepub fn option(
&mut self,
short_name: &str,
long_name: &str,
desc: &str,
hint: &str,
occur: Occur,
default: Option<String>,
) -> &mut Args
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 nonelong_name
- e.g."help"
for a--help
option, or""
for nonedesc
- A description of the flag for the usage messagehint
- A hint to be used in place of the argument in the usage message, e.g."FILE"
for a-o FILE
optionoccur
- An enum representing whether the option is required or notdefault
- The default value for this option if there should be one
Sourcepub fn parse<C: IntoIterator>(&mut self, raw_args: C) -> Result<(), ArgsError>
pub fn parse<C: IntoIterator>(&mut self, raw_args: C) -> Result<(), ArgsError>
Parses arguments according to the registered options.
§Failures
Fails if any errors are encountered during parsing.
Sourcepub fn parse_from_cli(&mut self) -> Result<(), ArgsError>
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.
Sourcepub fn short_usage(&self) -> String
pub fn short_usage(&self) -> String
Generates a one-line usage summary from the registered options.
Sourcepub fn optional_validated_value_of<T>(
&self,
opt_name: &str,
validations: &[Box<dyn Validation<T = T>>],
) -> Result<Option<T>, ArgsError>where
T: FromStr,
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 Validation
s, and wraps it in an Option
§Failures
See validated_value_of
Sourcepub fn optional_value_of<T: FromStr>(
&self,
opt_name: &str,
) -> Result<Option<T>, ArgsError>
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
Sourcepub fn validated_value_of<T>(
&self,
opt_name: &str,
validations: &[Box<dyn Validation<T = T>>],
) -> Result<T, ArgsError>where
T: FromStr,
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 Validation
s.
§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.
Sourcepub fn value_of<T: FromStr>(&self, opt_name: &str) -> Result<T, ArgsError>
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
.
Sourcepub fn values_of<T: FromStr>(&self, opt_name: &str) -> Result<Vec<T>, ArgsError>
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
.