Clappers

Struct Clappers 

Source
pub struct Clappers { /* private fields */ }

Implementations§

Source§

impl Clappers

Source

pub fn new() -> Self

Creates a Clappers parser

§Parameters

None.

§Return value

An empty Clappers parser, which is ready to be configured by chaining:

  • set_flags()
  • set_singles()
  • set_multiples()

Once configured, build() is chained last to build the actual command line arguments parser

§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .set_flags(vec!["h|help", "v|verbose"])
        .set_singles(vec!["o|output", "u|username"])
        .set_multiples(vec!["i|input", "host"])
        .build();

    // ...
}
Source

pub fn set_flags(self, arg_specs: Vec<&str>) -> Self

Add flag argument parsing to the Clappers config

Flag arguments are true if they were supplied on the command line, and false otherwise e.g:

 -h
 --help
 -v
 --verbose

Note: flag arguments do not take values

§Parameters

arg_specs specifies which flag arguments on the command line to care about.

Each arg_spec contains “|” separated flag argument alias names e.g:

 clappers.set_flags(vec!["h|help", "v|verbose"]);
§Return value

The Clappers parser so that it can be chained

§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .set_flags(vec!["h|help", "v|verbose"])
        .set_singles(vec!["o|output", "u|username"])
        .set_multiples(vec!["i|input", "host"])
        .build();

    // ...
}
Source

pub fn set_singles(self, arg_specs: Vec<&str>) -> Self

Add single value argument parsing to the Clappers config

Single value arguments contain a single String value if they were supplied on the command line, and empty String otherwise e.g:

 -o filename.txt
 --output filename.txt
 -u Zelensky
 --username Zelensky
§Parameters

arg_specs specifies which single value arguments on the command line to care about.

Each arg_spec contains “|” separated single value argument alias names e.g:

 clappers.set_singles(vec!["o|output", "u|username"]);
§Return value

The Clappers parser so that it can be chained

§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .set_flags(vec!["h|help", "v|verbose"])
        .set_singles(vec!["o|output", "u|username"])
        .set_multiples(vec!["i|input", "host"])
        .build();

    // ...
}
Source

pub fn set_multiples(self, arg_specs: Vec<&str>) -> Self

Add multiple value argument parsing to the Clappers config

Multiple value arguments contain at least a singly populated Vec<String> value if they were supplied on the command line, and empty Vec<String> otherwise e.g:

 -i file1.txt
 --input file1.txt
 --host host1

They can also contain multiple values, by repetition on the command line e.g:

 -i file1.txt -i file2.txt ... -i fileN.txt
 --host host1 --host host2 ... --host hostN

The following format also works, reading from the first value until either the next argument is reached, or until the end of the entire command line arguments e.g:

 -i file1.txt file2.txt ... fileN.txt -n next_argument
 --host host1 host2 hostN
§Parameters

arg_specs specifies which multiple value arguments on the command line to care about.

Each arg_spec contains “|” separated multiple value argument alias names e.g:

 clappers.set_multiples(vec!["i|input", "host"]);
§Return value

The Clappers parser so that it can be chained

§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .set_flags(vec!["h|help", "v|verbose"])
        .set_singles(vec!["o|output", "u|username"])
        .set_multiples(vec!["i|input", "host"])
        .build();

    // ...
}
Source

pub fn build(self) -> Self

Build the command line arguments parser with the current Clappers config

§Parameters

None

§Return value

The Clappers parser containing the parsed command line arguments values, accessed with:

  • get_flags()
  • get_singles()
  • get_multiples()
  • get_leftovers()
§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .set_flags(vec!["h|help", "v|verbose"])
        .set_singles(vec!["o|output", "u|username"])
        .set_multiples(vec!["i|input", "host"])
        .build();

    if clappers.get_flag("help") {
        // Show help text
    }

    // ...
}
Source

pub fn get_flag(&self, argument: &str) -> bool

Check if the flag was supplied on the command line for the specified argument

§Parameters

argument is any alias of the specified argument

§Return value

true if the flag was supplied on the command line, and false otherwise

§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .set_flags(vec!["h|help"])
        .build();

    if clappers.get_flag("help") {
        // Show help text
    }

    if clappers.get_flag("h") {
        // This will also show the help text
    }

    // ...
}
Source

pub fn get_single(&self, argument: &str) -> String

Get the single value supplied on the command line for the specified argument

§Parameters

argument is any alias of the specified argument

§Return value

The single String value if they were supplied on the command line, and empty String otherwise

§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .set_singles(vec!["output"])
        .build();

    println!("Output filename is {}", clappers.get_single("output"));

    // ...
}
Source

pub fn get_multiple(&self, argument: &str) -> Vec<String>

Get multiple values supplied on the command line for the specified argument

§Parameters

argument is any alias of the specified argument

§Return value

Multiple String values if they were supplied on the command line, and empty Vec<String> otherwise

§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .set_multiples(vec!["input"])
        .build();

    println!("Input filenames are {:#?}", clappers.get_multiple("input"));

    // ...
}
Source

pub fn get_leftovers(&self) -> Vec<String>

Get all values supplied on the command line that are not associated with any argument

§Parameters

None

§Return value

All String values supplied on the command line that are not associated with any argument, and empty Vec<String> otherwise

§Example
use clappers::Clappers;

fn main() {
    let clappers = Clappers::new()
        .build();

    println!("`ls *` returned the following filenames: {:#?}", clappers.get_leftovers());

    // ...
}

Trait Implementations§

Source§

impl Clone for Clappers

Source§

fn clone(&self) -> Clappers

Returns a duplicate 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 Clappers

Source§

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

Formats the value using the given formatter. 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.