Struct sarge::ArgumentReader

source ·
pub struct ArgumentReader {
    pub doc: Option<String>,
    /* private fields */
}
Expand description

The structure that actually reads all your arguments.

Use ArgumentReader::add to register arguments and get ArgumentRefs. Then, use ArgumentReader::parse{_cli,_env,_provided} to get Arguments, which contains the results of parsing. Finally, you can use ArgumentRef::get to retrieve the values of your arguments.

Fields§

§doc: Option<String>

Program-level documentation.

Only available on feature help.

Implementations§

source§

impl ArgumentReader

source

pub fn new() -> Self

Returns an empty ArgumentReader.

Examples found in repository?
examples/help.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    let mut parser = ArgumentReader::new();
    parser.doc = Some("An example demonstrating automatic documentation generation.".into());
    parser.add::<bool>(tag::both('a', "abc").env("ABC").doc("Super duper docs"));
    parser.add::<bool>(tag::short('b').env("BAR"));
    parser.add::<String>(tag::long("baz-arg"));
    parser.add::<u32>(tag::both('f', "foo").doc("Hello, World!"));
    parser.add::<bool>(tag::short('x').doc("Testing testing 123"));
    parser.add::<bool>(tag::long("xy").doc("Testing testing 456"));
    parser.add::<Vec<i8>>(tag::env("ENV_ONLY").doc(
        "This is really, really long, multiline argument\ndocumentation, it'll wrap nicely I hope",
    ));

    parser.print_help();
}
source

pub fn print_help(&self)

Prints help for all the arguments.

Only available on feature help.

§Panics

If the name of the executable could not be found, panics.

Examples found in repository?
examples/help.rs (line 16)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    let mut parser = ArgumentReader::new();
    parser.doc = Some("An example demonstrating automatic documentation generation.".into());
    parser.add::<bool>(tag::both('a', "abc").env("ABC").doc("Super duper docs"));
    parser.add::<bool>(tag::short('b').env("BAR"));
    parser.add::<String>(tag::long("baz-arg"));
    parser.add::<u32>(tag::both('f', "foo").doc("Hello, World!"));
    parser.add::<bool>(tag::short('x').doc("Testing testing 123"));
    parser.add::<bool>(tag::long("xy").doc("Testing testing 456"));
    parser.add::<Vec<i8>>(tag::env("ENV_ONLY").doc(
        "This is really, really long, multiline argument\ndocumentation, it'll wrap nicely I hope",
    ));

    parser.print_help();
}
source

pub fn add<T: ArgumentType>(&mut self, tag: Full) -> ArgumentRef<T>

Adds an argument to the parser.

Examples found in repository?
examples/help.rs (line 6)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    let mut parser = ArgumentReader::new();
    parser.doc = Some("An example demonstrating automatic documentation generation.".into());
    parser.add::<bool>(tag::both('a', "abc").env("ABC").doc("Super duper docs"));
    parser.add::<bool>(tag::short('b').env("BAR"));
    parser.add::<String>(tag::long("baz-arg"));
    parser.add::<u32>(tag::both('f', "foo").doc("Hello, World!"));
    parser.add::<bool>(tag::short('x').doc("Testing testing 123"));
    parser.add::<bool>(tag::long("xy").doc("Testing testing 456"));
    parser.add::<Vec<i8>>(tag::env("ENV_ONLY").doc(
        "This is really, really long, multiline argument\ndocumentation, it'll wrap nicely I hope",
    ));

    parser.print_help();
}
source

pub fn parse(self) -> Result<Arguments, ArgParseError>

Parse arguments from std::env::{args,vars}.

§Errors

If any arguments fail to parse their values, this will forward that error. Otherwise, see ArgParseError for a list of all possible errors.

source

pub fn parse_provided<A: AsRef<str>, IA: IntoIterator<Item = A>, K: AsRef<str>, V: AsRef<str>, IE: IntoIterator<Item = (K, V)>>( self, cli: IA, env: IE ) -> Result<Arguments, ArgParseError>

Parse from the provided environment variables and CLI arguments.

§Errors

If any arguments fail to parse their values, this will forward that error. Otherwise, see ArgParseError for a list of all possible errors.

Trait Implementations§

source§

impl Clone for ArgumentReader

source§

fn clone(&self) -> ArgumentReader

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 ArgumentReader

source§

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

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

impl Default for ArgumentReader

source§

fn default() -> ArgumentReader

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> 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,

§

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>,

§

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>,

§

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.