Struct argparse_rs::argparser::ArgParser [] [src]

pub struct ArgParser { /* fields omitted */ }

This type represents the state and methods for parsing arguments. A new parser must be created for every set of arguments you want to parse.

Methods

impl ArgParser
[src]

Constructs a new ArgParser, given the name of the program that you want to be printed in help messages

Add another option to parse.

Example

// add an option that is a `Flag`, with no default value, with
// a long form of `--verbose`, short form of `v`, that is not
// required to be passed, and has a default value of `false`

use argparse::{ArgParser, ArgType};

let mut parser = ArgParser::new("runner".into());
parser.add_opt("verbose", Some("false"), 'v', false,
    "Whether to produce verbose output", ArgType::Flag);

Remove an option from parsing consideration.

Example

// add an option that is a `Flag`, with no default value, with
// a long form of `--verbose`, short form of `v`, that is not
// required to be passed, and has a default value of `false`

use argparse::{ArgParser, ArgType};

let mut parser = ArgParser::new("runner".into());
parser.add_opt("verbose", Some("false"), 'v', false,
    "Whether to produce verbose output", ArgType::Flag);
assert!(parser.remove_opt("verbose").is_ok())

Parse a set of arguments, given the previous configuration

Example

// add an option that is a `Flag`, with no default value, with
// a long form of `--verbose`, short form of `v`, that is not
// required to be passed, and has a default value of `false`

use argparse::{ArgParser, ArgType};

let mut parser = ArgParser::new("runner".into());
parser.add_opt("verbose", Some("false"), 'v', false,
    "Whether to produce verbose output", ArgType::Flag);

// Normally you'd get this from std::env::args().iter()
let test_1 = "./runner --verbose".split_whitespace()
    .map(|s| s.into())
    .collect::<Vec<String>>();
 
if let Ok(p_res) = parser.parse(test_1.iter()) {
    // do stuff here
}

Prints the help message, which is constructed based on the options used

Example

use argparse::{ArgParser, ArgType};

let mut parser = ArgParser::new("runner".into());
parser.add_opt("verbose", Some("false"), 'v', false,
    "Whether to produce verbose output", ArgType::Flag);

// Normally you'd get this from std::env::args().iter()
let test_1 = "./runner --help".split_whitespace()
    .map(|s| s.into())
    .collect::<Vec<String>>();
 
if let Ok(p_res) = parser.parse(test_1.iter()) {
    if let Some(true) = p_res.get("help") {
        parser.help();
    }
}

Trait Implementations

impl Debug for ArgParser
[src]

Formats the value using the given formatter.

impl Clone for ArgParser
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more