pub struct Parser<'a> { /* private fields */ }
Expand description
A parser for command line arguments.
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn new(raw: &'a dyn RawArgs) -> Parser<'a>
pub fn new(raw: &'a dyn RawArgs) -> Parser<'a>
Create a parser from an iterator. This is useful for testing among other things.
The first item from the iterator must be the binary name, as from std::env::args_os
.
The iterator is consumed immediately.
§Example
let args = ["myapp", "-n", "10", "./foo.bar"];
let mut parser = lexarg_parser::Parser::new(&&args[1..]);
Sourcepub fn next_arg(&mut self) -> Option<Arg<'a>>
pub fn next_arg(&mut self) -> Option<Arg<'a>>
Get the next option or positional Arg
.
Returns None
if the command line has been exhausted.
Returns Arg::Unexpected
on failure
Notes:
=
is always accepted as a [Arg::Short("=")
]. If that isn’t the case in your application, you may want to special case the error for that.
Sourcepub fn next_flag_value(&mut self) -> Option<&'a OsStr>
pub fn next_flag_value(&mut self) -> Option<&'a OsStr>
Get a flag’s value
This function should normally be called right after seeing a flag that expects a value;
positional arguments should be collected with Parser::next_arg()
.
A value is collected even if it looks like an option (i.e., starts with -
).
None
is returned if there is not another applicable flag value, including:
- No more arguments are present
--
was encountered, meaning all remaining arguments are positional- Being called again when the first value was attached (
--flag=value
,-Fvalue
,-F=value
)
Sourcepub fn next_attached_value(&mut self) -> Option<&'a OsStr>
pub fn next_attached_value(&mut self) -> Option<&'a OsStr>
Get a flag’s attached value (--flag=value
, -Fvalue
, -F=value
)
This is a more specialized variant of Parser::next_flag_value
for when only attached
values are allowed, e.g. --color[=<when>]
.
Sourcepub fn next_raw(&mut self) -> Result<Option<&'a OsStr>, ()>
pub fn next_raw(&mut self) -> Result<Option<&'a OsStr>, ()>
Get the next argument, independent of what it looks like
Returns Err(())
if an attached value is present
Sourcepub fn remaining_raw(&mut self) -> Result<impl Iterator<Item = &'a OsStr>, ()>
pub fn remaining_raw(&mut self) -> Result<impl Iterator<Item = &'a OsStr>, ()>
Collect all remaining arguments, independent of what they look like
Returns Err(())
if an attached value is present