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: boolDeprecated. Use .enable_help_command() instead.
Implementations§
Source§impl ArgParser
impl ArgParser
Sourcepub fn new() -> ArgParser
pub fn new() -> ArgParser
Creates a new ArgParser instance.
Examples found in repository?
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
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}Sourcepub fn helptext<S>(self, text: S) -> Self
pub fn helptext<S>(self, text: S) -> Self
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?
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
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}Sourcepub fn version<S>(self, text: S) -> Self
pub fn version<S>(self, text: S) -> Self
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?
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
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}Sourcepub fn option(self, name: &str, default: &str) -> Self
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?
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}Sourcepub fn flag(self, name: &str) -> Self
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?
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}Sourcepub fn command(self, name: &str, cmd_parser: ArgParser) -> Self
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?
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}Sourcepub fn enable_help_command(self, enable: bool) -> Self
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.
Sourcepub fn callback(self, f: fn(&str, &ArgParser)) -> Self
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?
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}Sourcepub fn value(&self, name: &str) -> String
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?
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}Sourcepub fn values(&self, name: &str) -> Vec<String>
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.)
Sourcepub fn count(&self, name: &str) -> usize
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.)
Sourcepub fn found(&self, name: &str) -> bool
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?
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}Sourcepub fn parse(&mut self) -> Result<(), Error>
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?
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
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}