CommandLineParser

Struct CommandLineParser 

Source
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>

Source

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

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

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

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

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

Source

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

Auto Trait Implementations§

§

impl<'a> Freeze for CommandLineParser<'a>

§

impl<'a> !RefUnwindSafe for CommandLineParser<'a>

§

impl<'a> !Send for CommandLineParser<'a>

§

impl<'a> !Sync for CommandLineParser<'a>

§

impl<'a> Unpin for CommandLineParser<'a>

§

impl<'a> !UnwindSafe for CommandLineParser<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.