Struct argparse::argparser::ArgParser [] [src]

pub struct ArgParser {
    // some 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

fn new(name: String) -> ArgParser

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

fn add_opt(&mut self, name: &str, default: Option<&str>, flag: char, required: bool, help: &str, type_: ArgType)

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);

fn remove_opt(&mut self, name: &str) -> Result<(), &'static str>

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())

fn parse<'a, I: Iterator<Item=&'a String>>(&self, args: I) -> ParseResult

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
}

fn help(&self)

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

Derived Implementations

impl Clone for ArgParser

fn clone(&self) -> ArgParser

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

impl Debug for ArgParser

fn fmt(&self, __arg_0: &mut Formatter) -> Result