Arguments

Struct Arguments 

Source
pub struct Arguments<F: ArgumentType, A: ArgumentType> {
    pub flags: Vec<Flag<F>>,
    pub actions: Vec<A>,
}
Expand description

The meat of the library, describes an Argument object and its methods

Fields§

§flags: Vec<Flag<F>>

A list of the user defined Flag types and optional values

§actions: Vec<A>

A list of the user defined Action types

Implementations§

Source§

impl<F: ArgumentType, A: ArgumentType> Arguments<F, A>

Source

pub fn new() -> Self

A default constructor for the Arguments type.

Note: explicit type must be specified when using a default constructor, unlike vectors etc.

§Examples
use adante::{Arguments, ArgumentType};

#[derive(Debug, Clone, Copy, PartialEq)]
enum FlagType {
    Help,
    Verbose,
    Print,
}
impl ArgumentType for FlagType {
    fn from_str<ErrorType>(key: &str, error: ErrorType)
                               -> Result<Self, ErrorType> {
        match key {
            "-h" | "--help" => Ok(Self::Help),
            "-v" | "--verbose" => Ok(Self::Verbose),
            "-p" | "--print" => Ok(Self::Print),
            _ => Err(error),
        }
    }
}
enum ActionType {
    Add,
    Remove,
    Edit,
}
impl ArgumentType for ActionType {
    fn from_str<ErrorType>(key: &str, error: ErrorType)
        -> Result<Self, ErrorType> {
        match key {
            "a" | "add" => Ok(Self::Add),
            "r" | "remove" => Ok(Self::Remove),
            "e" | "edit" => Ok(Self::Edit),
            _ => Err(error),
        }
    }
}
let blank_args: Arguments<FlagType, ActionType> = Arguments::new();

assert_eq!(blank_args.flags.len(), 0);
assert_eq!(blank_args.actions.len(), 0);
Source

pub fn parse<E: Error + Clone + Copy>( env_args: Vec<&str>, error: E, ) -> Result<Arguments<F, A>, E>

The parsing function that returns a full Arguments object.

More complicated usages and tests can be found in the tests.rs file.

§Examples
use adante::{ArgumentType, Error, Arguments};

#[derive(Debug, Clone, Copy)]
enum ErrorType {
    Syntax, // EXTREMELY simple example
            // More complex examples are shown in
            // the documentation for Error
}
impl Error for ErrorType {
    fn handle(&self) {
        ()
    }
    fn as_str(&self) -> &str {
        "Syntax Error"
    }
}


#[derive(Debug, Clone, Copy, PartialEq)]
enum FlagType {
    Help,
    Verbose,
    Print,
    TestFail, // NOTE: For testing only
              // Use Error
}
impl ArgumentType for FlagType {
    fn from_str<ErrorType>(key: &str, error: ErrorType)
                               -> Result<Self, ErrorType> {
        match key {
            "-h" | "--help" => Ok(Self::Help),
            "-v" | "--verbose" => Ok(Self::Verbose),
            "-p" | "--print" => Ok(Self::Print),
            _ => Err(error),
        }
    }
}
enum ActionType {
    Add,
    Remove,
    Edit,
}
impl ArgumentType for ActionType {
    fn from_str<ErrorType>(key: &str, error: ErrorType)
        -> Result<Self, ErrorType> {
        match key {
            "a" | "add" => Ok(Self::Add),
            "r" | "remove" => Ok(Self::Remove),
            "e" | "edit" => Ok(Self::Edit),
            _ => Err(error),
        }
    }
}

let env_args = vec!["-v"];
let env_args: Arguments<FlagType, ActionType> =
    match Arguments::parse(env_args, ErrorType::Syntax) {
        Ok(a) => a,
        Err(e) => Arguments::new()
    };

let mut result: FlagType = FlagType::TestFail;

if env_args.flags.len() != 0 {
    result = env_args.flags[0].key;
}

assert_eq!(result, FlagType::Verbose);

Trait Implementations§

Source§

impl<F: Debug + ArgumentType, A: Debug + ArgumentType> Debug for Arguments<F, A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<F, A> Freeze for Arguments<F, A>

§

impl<F, A> RefUnwindSafe for Arguments<F, A>

§

impl<F, A> Send for Arguments<F, A>
where A: Send, F: Send,

§

impl<F, A> Sync for Arguments<F, A>
where A: Sync, F: Sync,

§

impl<F, A> Unpin for Arguments<F, A>
where A: Unpin, F: Unpin,

§

impl<F, A> UnwindSafe for Arguments<F, A>
where A: UnwindSafe, F: UnwindSafe,

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.