pub struct ArgParser { /* private fields */ }
Expand description
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.
Implementations§
Source§impl ArgParser
impl ArgParser
Sourcepub fn new(name: String) -> ArgParser
pub fn new(name: String) -> ArgParser
Constructs a new ArgParser
, given the name of the program
that you want to be printed in help messages
Sourcepub fn add_opt(
&mut self,
name: &str,
default: Option<&str>,
flag: char,
required: bool,
help: &str,
type_: ArgType,
)
pub 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);
Sourcepub fn remove_opt(&mut self, name: &str) -> Result<(), &'static str>
pub 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())
Sourcepub fn parse<'a, I: Iterator<Item = &'a String>>(&self, args: I) -> ParseResult
pub 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
}
Sourcepub fn help(&self)
pub 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§
Auto Trait Implementations§
impl Freeze for ArgParser
impl RefUnwindSafe for ArgParser
impl Send for ArgParser
impl Sync for ArgParser
impl Unpin for ArgParser
impl UnwindSafe for ArgParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more