pub struct Opt { /* private fields */ }Expand description
Represents the result of parsing a single command-line option.
This structure contains information about a parsed option, including the option character, any error that occurred during parsing, and the option’s argument if one was provided.
Fields are private; use the val, erropt,
arg, and into_arg accessors.
The argument is stored as a Cow<'static, str> so that borrowed inputs
(string literals, &'static OsStr/&'static CStr from sources such as
the argv crate) flow through without
allocation when they don’t need to be sliced.
Implementations§
Source§impl Opt
impl Opt
Sourcepub fn val(&self) -> char
pub fn val(&self) -> char
Returns the option character that was parsed.
This can be:
- The actual option character if it was valid
- ‘?’ if an unknown option was encountered
- ‘:’ if a missing argument was detected and optstring starts with ‘:’
Sourcepub fn erropt(&self) -> Option<char>
pub fn erropt(&self) -> Option<char>
Returns the error option character if an error occurred during parsing.
Returns:
Some(char)containing the problematic option character if:- An unknown option was encountered
- A required argument was missing
Noneif no error occurred
Sourcepub fn arg(&self) -> Option<&str>
pub fn arg(&self) -> Option<&str>
Returns the argument associated with the option, if any.
Returns:
Some(&str)containing the option’s argument if one was providedNoneif the option takes no argument or if a required argument was missing
Sourcepub fn into_arg(self) -> Option<Cow<'static, str>>
pub fn into_arg(self) -> Option<Cow<'static, str>>
Consumes self and returns the argument associated with the option, if any.
The returned Cow borrows from the original input when possible (e.g. when the
argument was passed as a separate &'static str or valid-UTF-8 &'static OsStr),
and only allocates when the parser had to slice into a larger argument (e.g.
-ofile.txt).
§Returns
Some(Cow<'static, str>)containing the option’s argument if one was providedNoneif the option takes no argument or if a required argument was missing