Struct Parser

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

Parser struct containing the config, a map of arguments to functions, and a function that is called when an argument is not in the map.

§Example

#[derive(PartialEq, Debug, Default)]
struct Config {
    foo: bool,
    bar: bool,
}

let cfg = arg_fn::Parser::new(Config::default(), |_cfg, _arg| {})
    .arg("-foo", |cfg| cfg.foo = true)
    .arg("-nofoo", |cfg| cfg.foo = false)
    .arg("-bar", |cfg| cfg.bar = true)
    .arg("-nobar", |cfg| cfg.bar = false)
    .parse(["-bar", "-nofoo", "-foo", "-nobar", "-foo"]);

assert_eq!(
    cfg,
    Config {
        foo: true,
        bar: false,
    }
)

Implementations§

Source§

impl<'a, Config: 'a> Parser<'a, Config>

Source

pub fn new(config: Config, unknown: impl Fn(&mut Config, &'a str) + 'a) -> Self

Source

pub fn with_arguments( config: Config, arguments: HashMap<Cow<'a, str>, Box<dyn Fn(&mut Config) + 'a>>, unknown: impl Fn(&mut Config, &'a str) + 'a, ) -> Self

Source

pub fn arg( self, argument: impl Into<Cow<'a, str>>, callback: impl Fn(&mut Config) + 'a, ) -> Self

Source

pub fn parse(self, input: impl IntoIterator<Item = &'a str>) -> Config

Trait Implementations§

Source§

impl<'a, Config: Default> Default for Parser<'a, Config>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, Config> Freeze for Parser<'a, Config>
where Config: Freeze,

§

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

§

impl<'a, Config> !Send for Parser<'a, Config>

§

impl<'a, Config> !Sync for Parser<'a, Config>

§

impl<'a, Config> Unpin for Parser<'a, Config>
where Config: Unpin,

§

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

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.