Skip to main content

Parser

Struct Parser 

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

A single-pass parse over argv.

Created with Parser::new and driven with Parser::next_event.

Implementations§

Source§

impl<'t: 'v, 'a, 'v> Parser<'t, 'a, 'v>

Source

pub fn new(root: &'t Command<'t>, argv: &'a [&'v OsStr]) -> Self

Begin parsing argv against root.

argv excludes the program name.

Source

pub fn command(&self) -> &'t Command<'t>

The command in scope: the root, or the deepest subcommand selected so far.

Source

pub fn double_dash_seen(&self) -> bool

Whether a -- was consumed as a separator.

False when flag interpretation stopped for another reason, such as an automatic argument taking a value, and false for a -- that a preserve argument kept as a value.

Source

pub fn command_path(&self) -> Vec<(&'t Command<'t>, usize)>

Every command entered so far, and where each one’s own words begin.

The ancestors are already kept for flag scoping; this is the same chain with the offsets, which is what lets a completion hand a callback the words of its command rather than of the deepest one — a global flag is declared on an ancestor.

Source

pub fn help_span(&self) -> (usize, usize)

The argv range the help word resolved as a command path.

Empty unless the word was typed. Every token in it named a subcommand of the one before it — the parser resolved them itself, so nothing here is a flag or a flag’s value.

Source

pub fn command_start(&self) -> usize

Where the command in scope began: the index in argv just after its name, or at the unmatched word routed into a default subcommand.

argv[command_start()..] is what that command was given, which is what a completion callback needs to be handed its own command’s half-parsed struct rather than the root’s.

Source

pub fn flags_stopped(&self) -> bool

Whether flag interpretation has stopped, for any reason.

Wider than double_dash_seen, and the question completion asks: past a separator or past the first value of an automatic argument, a dash-prefixed word is a value, so there is no flag there to offer.

Source

pub fn collecting(&self) -> Option<&'t Flag<'t>>

A variadic flag that is still claiming words.

Asked between events, because the answer is gone by the end: the call that finds argv exhausted is the one that clears it. A completion needs it — the next word after --tools a ⌶ is another tool, not the positional that follows.

Source

pub fn pending_arg(&self) -> Option<&'t Arg<'t>>

The positional the next word would fill, if there is one left.

A variadic stays here until it reaches its bound, which is what makes it the answer to “what could go where the cursor is” as many times as it can be filled.

Source

pub fn flags_in_scope(&self) -> impl Iterator<Item = &'t Flag<'t>> + '_

Flags a word here could name: this command’s own, then any ancestor’s globals.

The same set the parser itself would look in, so what is offered and what is accepted cannot disagree — including the shadowing rule, where a subcommand redeclaring an inherited name hides it.

Source

pub fn next_event(&mut self) -> Option<Result<Event<'t, 'a, 'v>, Error<'t, 'v>>>

Read the next event.

Returns None when argv is exhausted. An Err is terminal: the parse stops there, since continuing past a token that could not be understood would only produce bindings derived from a guess. Events already yielded before an error are therefore not a partial result to be used — a caller that assigned them into fields should discard the whole attempt.

One case is stronger than that, because the grammar demands it: a short bundle containing an unrecognized letter yields the error instead of, not after, the letters that did match.

Auto Trait Implementations§

§

impl<'t, 'a, 'v> Freeze for Parser<'t, 'a, 'v>

§

impl<'t, 'a, 'v> RefUnwindSafe for Parser<'t, 'a, 'v>

§

impl<'t, 'a, 'v> Send for Parser<'t, 'a, 'v>

§

impl<'t, 'a, 'v> Sync for Parser<'t, 'a, 'v>

§

impl<'t, 'a, 'v> Unpin for Parser<'t, 'a, 'v>

§

impl<'t, 'a, 'v> UnsafeUnpin for Parser<'t, 'a, 'v>

§

impl<'t, 'a, 'v> UnwindSafe for Parser<'t, 'a, 'v>

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.