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>
impl<F: ArgumentType, A: ArgumentType> Arguments<F, A>
Sourcepub fn new() -> Self
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);Sourcepub fn parse<E: Error + Clone + Copy>(
env_args: Vec<&str>,
error: E,
) -> Result<Arguments<F, A>, E>
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§
Auto Trait Implementations§
impl<F, A> Freeze for Arguments<F, A>
impl<F, A> RefUnwindSafe for Arguments<F, A>where
A: RefUnwindSafe,
F: RefUnwindSafe,
impl<F, A> Send for Arguments<F, A>
impl<F, A> Sync for Arguments<F, A>
impl<F, A> Unpin for Arguments<F, A>
impl<F, A> UnwindSafe for Arguments<F, A>where
A: UnwindSafe,
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more