Struct Parser

Source
pub struct Parser { /* private fields */ }
Expand description

A struct that parses the command line for certain Args.

§Example

let parser = Parser::new();
let arg = Arg::new().param("num");
let mut args = std::env::args();
args.next();
 
parser.add_arg(arg);
let output = parser.parse(&mut args).unwrap();

Implementations§

Source§

impl Parser

Source

pub fn new() -> Parser

Creates a new Parser struct.

§Example
let parser = Parser::new();
Source

pub fn add_arg(&self, arg: Arg)

Adds an argument to the parser.

§Example
let arg = Arg::new().param("num");
let parser = Parser::new();
 
parser.add_arg(arg);
Source

pub fn add_args(&self, args: Vec<Arg>)

Adds a vector of arguments to the parser.

§Example
let mut args = vec![
    Arg::new().param("num1"),
    Arg::new().param("num2"),
];
let parser = Parser::new();
 
parser.add_args(args);
Source

pub fn args(&self) -> Vec<Arg>

Returns the arguments associated with this parser as a vector.

§Example
let args = parser.args();
Source

pub fn len(&self) -> usize

Returns the number of arguments associated with this parser.

§Example
let n = parser.len();
Source

pub fn parse( &self, args: &mut impl Iterator<Item = String>, ) -> Result<HashMap<String, Option<String>>, Box<dyn Error>>

Parses through the remaining arguments and returns a hashmap of arguments passed and their relevant values.

§Example
let parser = Parser::new();
let mut args = vec![
    Arg::new().param("p1"),
    Arg::new().param("p2"),
    Arg::new().flag("help").short('h');
];
parser.add_args(args);
 
let mut input_args = std::env::args();
input_args.next();
 
let hashmap = parser.parse(input_args).unwrap();
println!("p1: {}, p2: {}", hashmap.get("p1"), hashmap.get("p2"));
if hashmap.contains_key("help") {
    println!("Help requested!");
}

Auto Trait Implementations§

§

impl !Freeze for Parser

§

impl !RefUnwindSafe for Parser

§

impl Send for Parser

§

impl !Sync for Parser

§

impl Unpin for Parser

§

impl UnwindSafe for Parser

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.