ArgParser

Struct ArgParser 

Source
pub struct ArgParser {
    pub args: Vec<String>,
    pub cmd_name: Option<String>,
    pub cmd_parser: Option<Box<ArgParser>>,
    pub cmd_help: bool,
    /* private fields */
}
Expand description

An ArgParser instance can be intialized using the builder pattern.

let mut parser = ArgParser::new()
    .helptext("Usage: appname...")
    .version("1.0")
    .option("bar b", "default")
    .flag("foo f");

Fields§

§args: Vec<String>

Stores positional arguments.

§cmd_name: Option<String>

Stores the command name, if a command was found.

§cmd_parser: Option<Box<ArgParser>>

Stores the command’s ArgParser instance, if a command was found.

§cmd_help: bool

Deprecated. Use .enable_help_command() instead.

Implementations§

Source§

impl ArgParser

Source

pub fn new() -> ArgParser

Creates a new ArgParser instance.

Examples found in repository?
examples/command.rs (line 4)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .command("boo", ArgParser::new()
8            .helptext("Usage: foobar boo...")
9            .callback(cmd_boo)
10        );
11
12    if let Err(err) = parser.parse() {
13        err.exit();
14    }
15}
More examples
Hide additional examples
examples/basic.rs (line 4)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .option("bar b", "default")
8        .flag("foo f");
9
10    if let Err(err) = parser.parse() {
11        err.exit();
12    }
13
14    if parser.found("foo") {
15        println!("Flag --foo/-f found.");
16    }
17
18    println!("Option --bar/-b has value: {}", parser.value("bar"));
19
20    for arg in parser.args {
21        println!("Arg: {}", arg);
22    }
23}
Source

pub fn helptext<S>(self, text: S) -> Self
where S: Into<String>,

Sets the parser’s helptext string. Supplying a helptext string activates support for an automatic --help flag, also a -h shortcut if not registered by another option.

let mut parser = ArgParser::new()
    .helptext("Usage: appname...");
Examples found in repository?
examples/command.rs (line 5)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .command("boo", ArgParser::new()
8            .helptext("Usage: foobar boo...")
9            .callback(cmd_boo)
10        );
11
12    if let Err(err) = parser.parse() {
13        err.exit();
14    }
15}
More examples
Hide additional examples
examples/basic.rs (line 5)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .option("bar b", "default")
8        .flag("foo f");
9
10    if let Err(err) = parser.parse() {
11        err.exit();
12    }
13
14    if parser.found("foo") {
15        println!("Flag --foo/-f found.");
16    }
17
18    println!("Option --bar/-b has value: {}", parser.value("bar"));
19
20    for arg in parser.args {
21        println!("Arg: {}", arg);
22    }
23}
Source

pub fn version<S>(self, text: S) -> Self
where S: Into<String>,

Sets the parser’s version string. Supplying a version string activates support for an automatic --version flag, also a -v shortcut if not registered by another option.

let mut parser = ArgParser::new()
    .version("1.0");
Examples found in repository?
examples/command.rs (line 6)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .command("boo", ArgParser::new()
8            .helptext("Usage: foobar boo...")
9            .callback(cmd_boo)
10        );
11
12    if let Err(err) = parser.parse() {
13        err.exit();
14    }
15}
More examples
Hide additional examples
examples/basic.rs (line 6)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .option("bar b", "default")
8        .flag("foo f");
9
10    if let Err(err) = parser.parse() {
11        err.exit();
12    }
13
14    if parser.found("foo") {
15        println!("Flag --foo/-f found.");
16    }
17
18    println!("Option --bar/-b has value: {}", parser.value("bar"));
19
20    for arg in parser.args {
21        println!("Arg: {}", arg);
22    }
23}
Source

pub fn option(self, name: &str, default: &str) -> Self

Registers a new option. The name parameter accepts an unlimited number of space-separated aliases and single-character shortcuts. The default value will be used if the option is not found.

let mut parser = ArgParser::new()
    .option("foo f", "default value");
Examples found in repository?
examples/basic.rs (line 7)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .option("bar b", "default")
8        .flag("foo f");
9
10    if let Err(err) = parser.parse() {
11        err.exit();
12    }
13
14    if parser.found("foo") {
15        println!("Flag --foo/-f found.");
16    }
17
18    println!("Option --bar/-b has value: {}", parser.value("bar"));
19
20    for arg in parser.args {
21        println!("Arg: {}", arg);
22    }
23}
Source

pub fn flag(self, name: &str) -> Self

Registers a new flag. The name parameter accepts an unlimited number of space-separated aliases and single-character shortcuts.

let mut parser = ArgParser::new()
    .flag("foo f");
Examples found in repository?
examples/basic.rs (line 8)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .option("bar b", "default")
8        .flag("foo f");
9
10    if let Err(err) = parser.parse() {
11        err.exit();
12    }
13
14    if parser.found("foo") {
15        println!("Flag --foo/-f found.");
16    }
17
18    println!("Option --bar/-b has value: {}", parser.value("bar"));
19
20    for arg in parser.args {
21        println!("Arg: {}", arg);
22    }
23}
Source

pub fn command(self, name: &str, cmd_parser: ArgParser) -> Self

Registers a new command. The name parameter accepts an unlimited number of space-separated aliases. The command’s helptext, flags, and options can be registered on the command’s ArgParser instance.

let mut parser = ArgParser::new()
    .helptext("Usage: appname...")
    .command("cmdname", ArgParser::new()
        .helptext("Usage: appname cmdname...")
        .flag("cmdflag")
    );
Examples found in repository?
examples/command.rs (lines 7-10)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .command("boo", ArgParser::new()
8            .helptext("Usage: foobar boo...")
9            .callback(cmd_boo)
10        );
11
12    if let Err(err) = parser.parse() {
13        err.exit();
14    }
15}
Source

pub fn enable_help_command(self, enable: bool) -> Self

This boolean switch toggles support for an automatic help command that prints subcommand helptext. The value defaults to false but gets toggled automatically to true whenever a command with helptext is registered. You can use this method to disable the feature if required.

Source

pub fn callback(self, f: fn(&str, &ArgParser)) -> Self

Registers a callback function on a command parser. If the command is found the function will be called and passed the command name and a reference to the command’s ArgParser instance.

Examples found in repository?
examples/command.rs (line 9)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .command("boo", ArgParser::new()
8            .helptext("Usage: foobar boo...")
9            .callback(cmd_boo)
10        );
11
12    if let Err(err) = parser.parse() {
13        err.exit();
14    }
15}
Source

pub fn value(&self, name: &str) -> String

Returns the value of the named option. Returns the default value registered with the option if the option was not found. Any of the option’s registered aliases or shortcuts can be used for the name parameter. (This function will panic if name is not a registered option name.)

Examples found in repository?
examples/basic.rs (line 18)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .option("bar b", "default")
8        .flag("foo f");
9
10    if let Err(err) = parser.parse() {
11        err.exit();
12    }
13
14    if parser.found("foo") {
15        println!("Flag --foo/-f found.");
16    }
17
18    println!("Option --bar/-b has value: {}", parser.value("bar"));
19
20    for arg in parser.args {
21        println!("Arg: {}", arg);
22    }
23}
Source

pub fn values(&self, name: &str) -> Vec<String>

Returns the named option’s list of values. Any of the option’s registered aliases or shortcuts can be used for the name parameter. (This function will panic if name is not a registered option name.)

Source

pub fn count(&self, name: &str) -> usize

Returns the number of times the named flag or option was found. Any registered alias or shortcut can be used for the name parameter. (This function will panic if name is not a registered flag or option name.)

Source

pub fn found(&self, name: &str) -> bool

Returns true if the named flag or option was found. Any registered alias or shortcut can be used for the name parameter. (This function will panic if name is not a registered flag or option name.)

Examples found in repository?
examples/basic.rs (line 14)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .option("bar b", "default")
8        .flag("foo f");
9
10    if let Err(err) = parser.parse() {
11        err.exit();
12    }
13
14    if parser.found("foo") {
15        println!("Flag --foo/-f found.");
16    }
17
18    println!("Option --bar/-b has value: {}", parser.value("bar"));
19
20    for arg in parser.args {
21        println!("Arg: {}", arg);
22    }
23}
Source

pub fn parse(&mut self) -> Result<(), Error>

Parse the program’s command line arguments.

if let Err(err) = parser.parse() {
    err.exit();
}
Examples found in repository?
examples/command.rs (line 12)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .command("boo", ArgParser::new()
8            .helptext("Usage: foobar boo...")
9            .callback(cmd_boo)
10        );
11
12    if let Err(err) = parser.parse() {
13        err.exit();
14    }
15}
More examples
Hide additional examples
examples/basic.rs (line 10)
3fn main() {
4    let mut parser = ArgParser::new()
5        .helptext("Usage: foobar...")
6        .version("1.0")
7        .option("bar b", "default")
8        .flag("foo f");
9
10    if let Err(err) = parser.parse() {
11        err.exit();
12    }
13
14    if parser.found("foo") {
15        println!("Flag --foo/-f found.");
16    }
17
18    println!("Option --bar/-b has value: {}", parser.value("bar"));
19
20    for arg in parser.args {
21        println!("Arg: {}", arg);
22    }
23}
Source

pub fn parse_vec(&mut self, args: Vec<&str>) -> Result<(), Error>

Parse a vector of arguments.

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