pub enum Arg<'i> {
Long(LongFlag<'i>),
Short(ShortFlag),
Word(Word<'i>),
Separator {
offset: usize,
},
}Expand description
One lexed command-line item.
Its fields are public and plain so a dispatch! arm can match flags as
patterns, both spellings at once:
use winnow::combinator::{dispatch, fail};
use winnow::prelude::*;
use winnow_args::BStr;
use winnow_args::combinator::args;
use winnow_args::token::{Arg, LongFlag, ShortFlag, arg};
use winnow_args::{Argv, Error};
let words = [BStr::new("-p"), BStr::new("x"), BStr::new("f")];
let (mut path, mut files) = (None::<String>, Vec::<String>::new());
let (p, f) = (&mut path, &mut files);
args(dispatch! {arg;
a @ (Arg::Long(LongFlag { name: b"path", .. }) | Arg::Short(ShortFlag { letter: 'p', .. })) => {
a.value_as().map(|v| *p = Some(v))
},
Arg::Word(w) => w.value_as("FILE").map(|file| f.push(file)),
_ => fail,
})
.parse_next(&mut Argv::new(&words))?;
assert_eq!((path.as_deref(), &files[..]), (Some("x"), &["f".to_string()][..]));Variants§
Long(LongFlag<'i>)
--name or --name=value. The word is consumed whole.
Short(ShortFlag)
One letter of -abc. The stream is left on the next letter (or the
attached value), or at the next word if this was the last letter.
Word(Word<'i>)
A word that is not a flag: a positional value, a lone -, or anything
after --.
Separator
The first --.
Implementations§
Source§impl<'i> Arg<'i>
impl<'i> Arg<'i>
Sourcepub fn unexpected(&self) -> Error
pub fn unexpected(&self) -> Error
The error for this item when nothing accepts it.
Sourcepub fn check_switch(&self) -> Result<(), Error>
pub fn check_switch(&self) -> Result<(), Error>
Finish a switch. Only a long flag can have been given a value,
--verbose=yes, which is an error; in -vx the x is another letter.
Sourcepub fn read_value(&self, input: &mut Argv<'i>) -> Result<&'i BStr, Error>
pub fn read_value(&self, input: &mut Argv<'i>) -> Result<&'i BStr, Error>
Finish an option, committing: a missing value is an error, not a reason to try something else.
--name=value: the attached text.-nvalue,-n=value: the rest of the bundle, less one=.--name value,-n value: the next word, unless it is flag-like. A lone-is a value;--is not.
Sourcepub fn read_next(&self, input: &mut Argv<'i>) -> Result<&'i BStr, Error>
pub fn read_next(&self, input: &mut Argv<'i>) -> Result<&'i BStr, Error>
One more value for a flag that takes several (-platform_version macos 11.0 12.0): the next word, whatever it looks like.
Sourcepub fn read_more(
&self,
input: &mut Argv<'i>,
options: ValueOptions,
terminator: Option<&BStr>,
) -> Option<&'i BStr>
pub fn read_more( &self, input: &mut Argv<'i>, options: ValueOptions, terminator: Option<&BStr>, ) -> Option<&'i BStr>
One more value for a flag that takes a variable number of them
(--include a b): the next word, unless the words ran out, it is
flag-like (as Arg::read_value_with judges it under options), or it
is terminator, which is dropped.
Sourcepub fn check_values(&self, got: usize, min: usize) -> Result<(), Error>
pub fn check_values(&self, got: usize, min: usize) -> Result<(), Error>
Fail if a flag declared to take at least min values got fewer.
Sourcepub fn has_attached_value(&self, input: &Argv<'i>) -> bool
pub fn has_attached_value(&self, input: &Argv<'i>) -> bool
Whether a value is attached to this flag in its own word:
--name=value, -nvalue. Asked before reading it.
Sourcepub fn read_value_with(
&self,
input: &mut Argv<'i>,
options: ValueOptions,
) -> Result<&'i BStr, Error>
pub fn read_value_with( &self, input: &mut Argv<'i>, options: ValueOptions, ) -> Result<&'i BStr, Error>
Arg::read_value under options: which detached words it may take.
Sourcepub fn read_value_or(
&self,
input: &mut Argv<'i>,
missing: &'static BStr,
) -> &'i BStr
pub fn read_value_or( &self, input: &mut Argv<'i>, missing: &'static BStr, ) -> &'i BStr
Arg::read_value for a flag whose value may be left out: where that
would report a missing value, --color alone or before another flag,
this gives missing instead.
Sourcepub fn read_value_or_with(
&self,
input: &mut Argv<'i>,
options: ValueOptions,
missing: &'static BStr,
) -> &'i BStr
pub fn read_value_or_with( &self, input: &mut Argv<'i>, options: ValueOptions, missing: &'static BStr, ) -> &'i BStr
Arg::read_value_or under options.
Sourcepub fn read_value_opt_with(
&self,
input: &mut Argv<'i>,
options: ValueOptions,
) -> Option<&'i BStr>
pub fn read_value_opt_with( &self, input: &mut Argv<'i>, options: ValueOptions, ) -> Option<&'i BStr>
Arg::read_value for a flag whose value may be left out, told apart
from a value of zero length: --color alone or before another flag
gives None, --color= gives Some(b""). What an Option field
stores.
Sourcepub fn read_value_as<T: FromArg>(
&self,
input: &mut Argv<'i>,
) -> Result<T, Error>
pub fn read_value_as<T: FromArg>( &self, input: &mut Argv<'i>, ) -> Result<T, Error>
Arg::read_value, converted with FromArg.
Sourcepub fn convert<T: FromArg>(&self, value: &BStr) -> Result<T, Error>
pub fn convert<T: FromArg>(&self, value: &BStr) -> Result<T, Error>
Convert value, one of this flag’s values, naming the flag on failure.
Sourcepub fn values_as<T: FromArg>(
&self,
delimiter: u8,
) -> impl Parser<Argv<'i>, Vec<T>, Error>
pub fn values_as<T: FromArg>( &self, delimiter: u8, ) -> impl Parser<Argv<'i>, Vec<T>, Error>
Arg::read_value split on delimiter, each piece converted.
Sourcepub fn switch(&self) -> impl Parser<Argv<'i>, (), Error>
pub fn switch(&self) -> impl Parser<Argv<'i>, (), Error>
Arg::check_switch as a parser, for a dispatch! arm.
Sourcepub fn value(&self) -> impl Parser<Argv<'i>, &'i BStr, Error>
pub fn value(&self) -> impl Parser<Argv<'i>, &'i BStr, Error>
Arg::read_value as a parser, for a dispatch! arm.
Sourcepub fn value_or<T: FromArg>(
&self,
missing: &'static BStr,
) -> impl Parser<Argv<'i>, T, Error>
pub fn value_or<T: FromArg>( &self, missing: &'static BStr, ) -> impl Parser<Argv<'i>, T, Error>
Arg::read_value_or, converted, as a parser for a dispatch! arm.
Sourcepub fn value_or_with<T: FromArg>(
&self,
options: ValueOptions,
missing: &'static BStr,
) -> impl Parser<Argv<'i>, T, Error>
pub fn value_or_with<T: FromArg>( &self, options: ValueOptions, missing: &'static BStr, ) -> impl Parser<Argv<'i>, T, Error>
Arg::read_value_or_with, converted, as a parser for a dispatch! arm.
Sourcepub fn value_as_with<T: FromArg>(
&self,
options: ValueOptions,
) -> impl Parser<Argv<'i>, T, Error>
pub fn value_as_with<T: FromArg>( &self, options: ValueOptions, ) -> impl Parser<Argv<'i>, T, Error>
Arg::read_value_with, converted, as a parser for a dispatch! arm.
Trait Implementations§
impl<'i> Copy for Arg<'i>
impl<'i> Eq for Arg<'i>
Source§impl<'i, const N: usize> Parser<Argv<'i>, Arg<'i>, Error> for Named<N>
One occurrence of the flag’s name, value not yet read.
impl<'i, const N: usize> Parser<Argv<'i>, Arg<'i>, Error> for Named<N>
One occurrence of the flag’s name, value not yet read.
Source§fn parse(
&mut self,
input: I,
) -> Result<O, ParseError<I, <E as ParserError<I>>::Inner>>where
Self: Sized,
I: Stream + StreamIsPartial,
E: ParserError<I>,
<E as ParserError<I>>::Inner: ParserError<I>,
fn parse(
&mut self,
input: I,
) -> Result<O, ParseError<I, <E as ParserError<I>>::Inner>>where
Self: Sized,
I: Stream + StreamIsPartial,
E: ParserError<I>,
<E as ParserError<I>>::Inner: ParserError<I>,
Source§fn parse_iter(&mut self, input: I) -> ParseIter<'_, Self, I, O, E> ⓘwhere
Self: Sized,
I: Stream + StreamIsPartial,
E: ParserError<I>,
<E as ParserError<I>>::Inner: ParserError<I>,
fn parse_iter(&mut self, input: I) -> ParseIter<'_, Self, I, O, E> ⓘwhere
Self: Sized,
I: Stream + StreamIsPartial,
E: ParserError<I>,
<E as ParserError<I>>::Inner: ParserError<I>,
Source§fn parse_peek(&mut self, input: I) -> Result<(I, O), E>
fn parse_peek(&mut self, input: I) -> Result<(I, O), E>
Source§fn by_ref(&mut self) -> ByRef<'_, Self, I, O, E>where
Self: Sized,
fn by_ref(&mut self) -> ByRef<'_, Self, I, O, E>where
Self: Sized,
&mut Self as a parser Read moreSource§fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E>
fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E>
Source§fn void(self) -> Void<Self, I, O, E>where
Self: Sized,
fn void(self) -> Void<Self, I, O, E>where
Self: Sized,
Parser Read moreSource§fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E>
fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E>
std::convert::From Read moreSource§fn with_taken(self) -> WithTaken<Self, I, O, E>
fn with_taken(self) -> WithTaken<Self, I, O, E>
Source§fn span(self) -> Span<Self, I, O, E>
fn span(self) -> Span<Self, I, O, E>
Source§fn with_span(self) -> WithSpan<Self, I, O, E>
fn with_span(self) -> WithSpan<Self, I, O, E>
Source§fn map<G, O2>(self, map: G) -> Map<Self, G, I, O, O2, E>
fn map<G, O2>(self, map: G) -> Map<Self, G, I, O, O2, E>
Source§fn try_map<G, O2, E2>(self, map: G) -> TryMap<Self, G, I, O, O2, E, E2>where
Self: Sized,
G: FnMut(O) -> Result<O2, E2>,
I: Stream,
E: FromExternalError<I, E2> + ParserError<I>,
fn try_map<G, O2, E2>(self, map: G) -> TryMap<Self, G, I, O, O2, E, E2>where
Self: Sized,
G: FnMut(O) -> Result<O2, E2>,
I: Stream,
E: FromExternalError<I, E2> + ParserError<I>,
Result over the output of a parser. Read moreSource§fn verify_map<G, O2>(self, map: G) -> VerifyMap<Self, G, I, O, O2, E>
fn verify_map<G, O2>(self, map: G) -> VerifyMap<Self, G, I, O, O2, E>
Source§fn flat_map<G, H, O2>(self, map: G) -> FlatMap<Self, G, H, I, O, O2, E>
fn flat_map<G, H, O2>(self, map: G) -> FlatMap<Self, G, H, I, O, O2, E>
Source§fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E>
fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E>
std::str::FromStr to the output of the parser Read more