pub struct Matches {
pub free: Vec<String>,
/* private fields */
}
Expand description
The result of checking command line arguments. Contains a vector of matches and a vector of free strings.
Fields§
§free: Vec<String>
Free string fragments
Implementations§
Source§impl Matches
impl Matches
Sourcepub fn opt_defined(&self, name: &str) -> bool
pub fn opt_defined(&self, name: &str) -> bool
Returns true if an option was defined
Sourcepub fn opt_present(&self, name: &str) -> bool
pub fn opt_present(&self, name: &str) -> bool
Returns true if an option was matched.
§Panics
This function will panic if the option name is not defined.
Sourcepub fn opt_count(&self, name: &str) -> usize
pub fn opt_count(&self, name: &str) -> usize
Returns the number of times an option was matched.
§Panics
This function will panic if the option name is not defined.
Sourcepub fn opt_positions(&self, name: &str) -> Vec<usize>
pub fn opt_positions(&self, name: &str) -> Vec<usize>
Returns a vector of all the positions in which an option was matched.
§Panics
This function will panic if the option name is not defined.
Sourcepub fn opts_present(&self, names: &[String]) -> bool
pub fn opts_present(&self, names: &[String]) -> bool
Returns true if any of several options were matched.
Sourcepub fn opts_present_any<C: IntoIterator>(&self, names: C) -> bool
pub fn opts_present_any<C: IntoIterator>(&self, names: C) -> bool
Returns true if any of several options were matched.
Similar to opts_present
but accepts any argument that can be converted
into an iterator over string references.
§Panics
This function might panic if some option name is not defined.
§Example
let mut opts = Options::new();
opts.optopt("a", "alpha", "first option", "STR");
opts.optopt("b", "beta", "second option", "STR");
let args = vec!["-a", "foo"];
let matches = &match opts.parse(&args) {
Ok(m) => m,
_ => panic!(),
};
assert!(matches.opts_present_any(&["alpha"]));
assert!(!matches.opts_present_any(&["beta"]));
Sourcepub fn opts_str(&self, names: &[String]) -> Option<String>
pub fn opts_str(&self, names: &[String]) -> Option<String>
Returns the string argument supplied to one of several matching options or None
.
Sourcepub fn opts_str_first<C: IntoIterator>(&self, names: C) -> Option<String>
pub fn opts_str_first<C: IntoIterator>(&self, names: C) -> Option<String>
Returns the string argument supplied to the first matching option of
several options or None
.
Similar to opts_str
but accepts any argument that can be converted
into an iterator over string references.
§Panics
This function might panic if some option name is not defined.
§Example
let mut opts = Options::new();
opts.optopt("a", "alpha", "first option", "STR");
opts.optopt("b", "beta", "second option", "STR");
let args = vec!["-a", "foo", "--beta", "bar"];
let matches = &match opts.parse(&args) {
Ok(m) => m,
_ => panic!(),
};
assert_eq!(Some("foo".to_string()), matches.opts_str_first(&["alpha", "beta"]));
assert_eq!(Some("bar".to_string()), matches.opts_str_first(&["beta", "alpha"]));
Sourcepub fn opt_strs(&self, name: &str) -> Vec<String>
pub fn opt_strs(&self, name: &str) -> Vec<String>
Returns a vector of the arguments provided to all matches of the given option.
Used when an option accepts multiple values.
§Panics
This function will panic if the option name is not defined.
Sourcepub fn opt_strs_pos(&self, name: &str) -> Vec<(usize, String)>
pub fn opt_strs_pos(&self, name: &str) -> Vec<(usize, String)>
Returns a vector of the arguments provided to all matches of the given option, together with their positions.
Used when an option accepts multiple values.
§Panics
This function will panic if the option name is not defined.
Sourcepub fn opt_str(&self, name: &str) -> Option<String>
pub fn opt_str(&self, name: &str) -> Option<String>
Returns the string argument supplied to a matching option or None
.
§Panics
This function will panic if the option name is not defined.
Sourcepub fn opt_default(&self, name: &str, def: &str) -> Option<String>
pub fn opt_default(&self, name: &str, def: &str) -> Option<String>
Returns the matching string, a default, or None
.
Returns None
if the option was not present, def
if the option was
present but no argument was provided, and the argument if the option was
present and an argument was provided.
§Panics
This function will panic if the option name is not defined.
Sourcepub fn opt_get<T>(&self, name: &str) -> Result<Option<T>, T::Err>where
T: FromStr,
pub fn opt_get<T>(&self, name: &str) -> Result<Option<T>, T::Err>where
T: FromStr,
Returns some matching value or None
.
Similar to opt_str, also converts matching argument using FromStr.
§Panics
This function will panic if the option name is not defined.
Sourcepub fn opt_get_default<T>(&self, name: &str, def: T) -> Result<T, T::Err>where
T: FromStr,
pub fn opt_get_default<T>(&self, name: &str, def: T) -> Result<T, T::Err>where
T: FromStr,
Returns a matching value or default.
Similar to opt_default, except the two differences.
Instead of returning None when argument was not present, return def
.
Instead of returning &str return type T, parsed using str::parse().
§Panics
This function will panic if the option name is not defined.
Sourcepub fn free_trailing_start(&self) -> Option<usize>
pub fn free_trailing_start(&self) -> Option<usize>
Returns index of first free argument after “–”.
If double-dash separator is present and there are some args after it in
the argument list then the method returns index into free
vector
indicating first argument after it.
behind it.
§Examples
let mut opts = Options::new();
let matches = opts.parse(&vec!["arg1", "--", "arg2"]).unwrap();
let end_pos = matches.free_trailing_start().unwrap();
assert_eq!(end_pos, 1);
assert_eq!(matches.free[end_pos], "arg2".to_owned());
If the double-dash is missing from argument list or if there are no arguments after it:
let mut opts = Options::new();
let matches = opts.parse(&vec!["arg1", "--"]).unwrap();
assert_eq!(matches.free_trailing_start(), None);
let matches = opts.parse(&vec!["arg1", "arg2"]).unwrap();
assert_eq!(matches.free_trailing_start(), None);