Struct ArgParser

Source
pub struct ArgParser {
    pub args: Vec<String>,
    /* private fields */
}
Expand description

Our homebrewed Arg Parser

Fields§

§args: Vec<String>

Implementations§

Source§

impl ArgParser

Source

pub fn new(capacity: usize) -> Self

Create a new ArgParser object

The capacity specifies the initial capacity of the number parameters. Always good to set it at the number of flags and opts total. (Used for initial hashmap allocation size)

Source

pub fn add_flag(self, flags: &[&str]) -> Self

Builder method for adding both short and long flags

Flags are just parameters that have no assigned values. They are used for when certain features or options have been enabled for the application

§Examples
> ls -l --human-readable
  ^  ^  ^
  |  |  |
  |  |  `-- A long flag to enable human readable numbers.
  |  `-- A short flag to enable the long format.
  `-- The command to list files.
Source

pub fn add_opt(self, short: &str, long: &str) -> Self

Builder method for adding both short and long opts

Opts are parameters that hold assigned values. They are used for when certain features or options have been enabled for the application

§Examples
> ls -T 4 --color=always
  ^  ^    ^
  |  |    |
  |  |    `-- A long opt to enable the use of color with value `always`.
  |  `-- A short opt to set tab size to the value `4`.
  `-- The command to list files.
Source

pub fn add_opt_default(self, short: &str, long: &str, default: &str) -> Self

Add an opt with a default value. Works the same way as add_opt.

Source

pub fn add_setting(self, setting: &str) -> Self

Builder method for adding settings

Settings are parameters that hold assigned values. They are used in some applications such as dd

§Examples
> dd if=/path/file
  ^  ^
  |  |
  |  |
  |  `-- The setting set to /path/file
  `-- The command to list files.
Source

pub fn add_setting_default(self, setting: &str, default: &str) -> Self

Add a setting with a default value. Works the same way as add_setting

Source

pub fn parse<A: Iterator<Item = String>>(&mut self, args: A)

Start parsing user inputted args for which flags and opts are used at runtime. The rest of the args that are not associated to opts get added to ArgParser.args.

Source

pub fn count<P: Hash + Eq + ?Sized>(&self, name: &P) -> usize
where Param: Borrow<P>,

Get the number of times a flag or opt has been found after parsing.

Source

pub fn found<P: Hash + Eq + ?Sized>(&self, name: &P) -> bool
where Param: Borrow<P>,

Check if a flag or opt has been found after initialization. While the short form of the opt or flag will be recognized, the long form should be used to enhance code readability.

§Examples
if parser.found("my-long-flag") {
    //Do things...
}
Source

pub fn flag<F: Hash + Eq + ?Sized>(&mut self, flag: &F) -> RefMut<'_, bool>
where Param: Borrow<F>,

Modify the state of a flag. Use true if the flag is to be enabled. Use false to disable its use.

Source

pub fn opt<O: Hash + Eq + ?Sized>(&mut self, opt: &O) -> RefMut<'_, String>
where Param: Borrow<O>,

Modify the state value of an opt. Use Some(String) to set if the opt is to be enabled and has been assigned a value from String. Use None to disable the opt’s use.

Source

pub fn get_opt<O: Hash + Eq + ?Sized>(&self, opt: &O) -> Option<String>
where Param: Borrow<O>,

Get the value of an Opt. If it has been set or defaulted, it will return a Some(String) value otherwise it will return None.

Source

pub fn get_setting<O: Hash + Eq + ?Sized>(&self, setting: &O) -> Option<String>
where Param: Borrow<O>,

Get the value of an Setting. If it has been set or defaulted, it will return a Some(String) value otherwise it will return None.

Source

pub fn found_invalid(&self) -> Result<(), String>

Trait Implementations§

Source§

impl Clone for ArgParser

Source§

fn clone(&self) -> ArgParser

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ArgParser

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ArgParser

Source§

fn default() -> ArgParser

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

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.