Skip to main content

Opt

Struct Opt 

Source
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

Source

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 ‘:’
Source

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
  • None if no error occurred
Source

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 provided
  • None if the option takes no argument or if a required argument was missing
Source

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 provided
  • None if the option takes no argument or if a required argument was missing

Trait Implementations§

Source§

impl Clone for Opt

Source§

fn clone(&self) -> Opt

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Opt

Source§

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

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

impl PartialEq<char> for Opt

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 PartialEq for Opt

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Eq for Opt

Source§

impl StructuralPartialEq for Opt

Auto Trait Implementations§

§

impl Freeze for Opt

§

impl RefUnwindSafe for Opt

§

impl Send for Opt

§

impl Sync for Opt

§

impl Unpin for Opt

§

impl UnsafeUnpin for Opt

§

impl UnwindSafe for Opt

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.