Skip to main content

Parse

Trait Parse 

Source
pub trait Parse: Sized {
    const SPEC: &'static CommandSpec;

    // Required method
    fn from_matches(
        spec: &'static CommandSpec,
        matches: &Matches<'_>,
    ) -> Result<Self, Error>;

    // Provided methods
    fn try_parse_from<'a, I>(args: I) -> Result<Self, Error>
       where I: IntoIterator<Item = &'a str> { ... }
    fn try_parse() -> Result<Self, Error> { ... }
    fn parse() -> Self { ... }
    fn parse_from<'a, I>(args: I) -> Self
       where I: IntoIterator<Item = &'a str> { ... }
}
Expand description

the trait the derive targets, also implementable by hand.

a type carries its static CommandSpec and reads itself out of Matches. Self::parse is the common “parse argv or exit” path, the try_* variants hand back the Error (including the Error::Help / Error::Version signals).

Required Associated Constants§

Source

const SPEC: &'static CommandSpec

this command’s static description.

Required Methods§

Source

fn from_matches( spec: &'static CommandSpec, matches: &Matches<'_>, ) -> Result<Self, Error>

build Self from matches against spec. spec is passed in (not read from Self::SPEC) so the same method works for a subcommand reading against its own spec.

Provided Methods§

Source

fn try_parse_from<'a, I>(args: I) -> Result<Self, Error>
where I: IntoIterator<Item = &'a str>,

parse the given args, returning the typed value or an Error.

args are borrowed (&str), so matched values point straight into them; the iterator’s items must outlive the call (which is fine, Self owns any field it keeps). this is the no_std entry point.

Source

fn try_parse() -> Result<Self, Error>

parse std::env::args() minus the program name.

Source

fn parse() -> Self

parse argv, printing help/version or errors and exiting.

Source

fn parse_from<'a, I>(args: I) -> Self
where I: IntoIterator<Item = &'a str>,

parse the given args, printing help/version or errors and exiting.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§