pub struct CommandLineParser<'a> { /* private fields */ }Expand description
The base command line parser.
§Example
use blarg::{CommandLineParser};
let parser = CommandLineParser::new("program")
// Configure with CommandLineParser::add and CommandLineParser::branch.
.build();
parser.parse_tokens(empty::slice()).unwrap();Implementations§
Source§impl<'a> CommandLineParser<'a>
impl<'a> CommandLineParser<'a>
Sourcepub fn new(program: impl Into<String>) -> Self
pub fn new(program: impl Into<String>) -> Self
Create a command line parser.
§Example
use blarg::CommandLineParser;
let parser = CommandLineParser::new("program")
.build();
parser.parse_tokens(vec![].as_slice()).unwrap();Sourcepub fn about(self, description: impl Into<String>) -> Self
pub fn about(self, description: impl Into<String>) -> Self
Document the about message for this command line parser. If repeated, only the final help message will apply.
An about message documents the command line parser in full sentence/paragraph format.
We recommend allowing blarg to format this field (ex: it is not recommended to use line breaks '\n').
§Example
use blarg::CommandLineParser;
let parser = CommandLineParser::new("program")
.about("--this will get discarded--")
.about("My program that does awesome stuff. Check it out!")
.build();
parser.parse_tokens(vec![].as_slice()).unwrap();Sourcepub fn add<T>(self, parameter: Parameter<'a, T>) -> Self
pub fn add<T>(self, parameter: Parameter<'a, T>) -> Self
Add an argument/option to the command line parser.
The order of argument parameters corresponds to their positional order during parsing. The order of option parameters does not affect the command parser semantics.
§Example
use blarg::{CommandLineParser, Parameter, Scalar};
let mut a: u32 = 0;
let mut b: u32 = 0;
let parser = CommandLineParser::new("program")
.add(Parameter::argument(Scalar::new(&mut a), "a"))
.add(Parameter::argument(Scalar::new(&mut b), "b"))
.build();
parser.parse_tokens(vec!["1", "2"].as_slice()).unwrap();
assert_eq!(a, 1);
assert_eq!(b, 2);Sourcepub fn branch<T: FromStr + Display + PartialEq>(
self,
condition: Condition<'a, T>,
) -> SubCommandParser<'a, T>
pub fn branch<T: FromStr + Display + PartialEq>( self, condition: Condition<'a, T>, ) -> SubCommandParser<'a, T>
Branch into a sub-command parser.
This changes the command line parser into a sub-command style command line parser. Any parameters added before the branch apply to the root parser.
Branching is always done with a special Scalar argument: Condition.
§Example
use blarg::{CommandLineParser, Parameter, Scalar, Condition};
let mut belongs_to_root: u32 = 0;
let mut sub_command: String = "".to_string();
let mut belongs_to_sub_command: u32 = 0;
let parser = CommandLineParser::new("program")
.add(Parameter::argument(Scalar::new(&mut belongs_to_root), "belongs_to_root"))
.branch(Condition::new(Scalar::new(&mut sub_command), "sub_command"))
.command("the-command".to_string(), |sub| {
sub.add(Parameter::argument(Scalar::new(&mut belongs_to_sub_command), "belongs_to_sub_command"))
})
.build();
parser.parse_tokens(vec!["1", "the-command", "2"].as_slice()).unwrap();
assert_eq!(belongs_to_root, 1);
assert_eq!(&sub_command, "the-command");
assert_eq!(belongs_to_sub_command, 2);Sourcepub fn build_parser(self) -> Result<GeneralParser<'a>, ConfigError>
pub fn build_parser(self) -> Result<GeneralParser<'a>, ConfigError>
Build the command line parser as a Result. This finalizes the configuration and checks for errors (ex: a repeated parameter name).
Sourcepub fn build(self) -> GeneralParser<'a>
pub fn build(self) -> GeneralParser<'a>
Build the command line parser.
This finalizes the configuration and checks for errors (ex: a repeated parameter name).
If an error is encountered, exits with error code 1 (via std::process::exit).