Skip to main content

Parser

Struct Parser 

Source
pub struct Parser<'a> { /* private fields */ }
Expand description

Parses raw argument slices against a slice of registered Commands.

Create a Parser with Parser::new, then call Parser::parse for each invocation. The parser borrows the command slice for lifetime 'a; the returned ParsedCommand also carries that lifetime.

§Examples

let cmds = vec![Command::builder("status").build().unwrap()];
let parser = Parser::new(&cmds);
let parsed = parser.parse(&["status"]).unwrap();
assert_eq!(parsed.command.canonical, "status");

Implementations§

Source§

impl<'a> Parser<'a>

Source

pub fn new(commands: &'a [Command]) -> Self

Create a new Parser over the given command slice.

§Arguments
Source

pub fn parse(&self, argv: &[&str]) -> Result<ParsedCommand<'a>, ParseError>

Parse argv (the full argument list including the command name) into a ParsedCommand that borrows from the registered command tree.

The first element of argv must be a word token naming the top-level command. Subsequent tokens are processed as described in the module documentation.

§Arguments
  • argv — The argument slice to parse. Should not include the program name (argv[0] in std::env::args); the first element must be the command name.
§Errors
§Examples
let cmd = Command::builder("build")
    .flag(
        Flag::builder("target")
            .takes_value()
            .default_value("debug")
            .build()
            .unwrap(),
    )
    .build()
    .unwrap();

let cmds = vec![cmd];
let parser = Parser::new(&cmds);

let parsed = parser.parse(&["build", "--target=release"]).unwrap();
assert_eq!(parsed.flags["target"], "release");

// Default is applied when flag is absent
let parsed2 = parser.parse(&["build"]).unwrap();
assert_eq!(parsed2.flags["target"], "debug");

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> !RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnsafeUnpin for Parser<'a>

§

impl<'a> !UnwindSafe for Parser<'a>

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> 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, 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.