Struct SubCommandParser

Source
pub struct SubCommandParser<'a, B: Display> { /* private fields */ }
Expand description

The sub-command parser.

Implementations§

Source§

impl<'a, B: FromStr + Display + PartialEq> SubCommandParser<'a, B>

Source

pub fn command( self, variant: B, setup_fn: impl FnOnce(SubCommand<'a>) -> SubCommand<'a>, ) -> Self

Setup a sub-command.

Sub-commands may be added arbitrarily, as long as the correspond to the branching type B. If repeated for the same variant of B, only the final version will be created on the parser. The order of sub-commands does not affect the command parser semantics.

§Example
use blarg::{CommandLineParser, Condition, Parameter, Scalar};

let mut value_a: u32 = 0;
let mut value_b: u32 = 0;
let mut sub_command: String = "".to_string();
let parser = CommandLineParser::new("program")
    .branch(Condition::new(Scalar::new(&mut sub_command), "sub_command"))
    .command("a".to_string(), |sub| sub.add(Parameter::argument(Scalar::new(&mut value_a), "value_a")))
    .command("b".to_string(), |sub| {
        sub.about("Description for the sub-command 'b'.")
            .add(Parameter::argument(Scalar::new(&mut value_b), "value_b"))
    })
    .build();

parser.parse_tokens(vec!["a", "1"].as_slice()).unwrap();

assert_eq!(&sub_command, "a");
assert_eq!(value_a, 1);
assert_eq!(value_b, 0);
Source

pub fn build_parser(self) -> Result<GeneralParser<'a>, ConfigError>

Build the sub-command based 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 sub-command based 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, B> Freeze for SubCommandParser<'a, B>

§

impl<'a, B> !RefUnwindSafe for SubCommandParser<'a, B>

§

impl<'a, B> !Send for SubCommandParser<'a, B>

§

impl<'a, B> !Sync for SubCommandParser<'a, B>

§

impl<'a, B> Unpin for SubCommandParser<'a, B>
where B: Unpin,

§

impl<'a, B> !UnwindSafe for SubCommandParser<'a, B>

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.