Condition

Struct Condition 

Source
pub struct Condition<'a, T>(/* private fields */);
Expand description

The condition argument with which to branch the parser. Used with CommandLineParser::branch.

There is an implicit (non-compile time) requirement for the type T of a Condition:

The implementations of std::str::FromStr must invert std::fmt::Display.

This sounds scary and onerous, but most types will naturally adhere to this requirement. Consider rusts implementation for bool, where this is requirement holds:

assert_eq!(bool::from_str("true").unwrap().to_string(), "true");
assert_eq!(bool::from_str("false").unwrap().to_string(), "false");

However, not all types will necessarily adhere to this requirement. Observe the following example enum:

// Implement FromStr to be case-insensitive.
// Implement Display.
enum FooBar {
    Foo,
    Bar,
}
assert_eq!(FooBar::from_str("Foo").unwrap().to_string(), "Foo");
// FromStr does not invert Display!
assert_ne!(FooBar::from_str("foo").unwrap().to_string(), "foo");

Implementations§

Source§

impl<'a, T: FromStr + Display> Condition<'a, T>

Source

pub fn new(value: Scalar<'a, T>, name: &'static str) -> Self

Create a condition parameter.

§Example
use blarg::{Condition, Scalar};
use std::str::FromStr;

// Be sure to implement `std::str::FromStr` so that it inverts `std::fmt::Display`.
enum FooBar {
    Foo,
    Bar,
}

let mut foo_bar: FooBar = FooBar::Foo;
Condition::new(Scalar::new(&mut foo_bar), "foo_bar");
// .. parse()
match foo_bar {
    FooBar::Foo => println!("Do foo'y things."),
    FooBar::Bar => println!("Do bar'y things."),
};
Source

pub fn help(self, description: impl Into<String>) -> Self

Document the help message for this sub-command condition. If repeated, only the final message will apply to the sub-command condition.

A help message describes the condition in full sentence/paragraph format. We recommend allowing blarg to format this field (ex: it is not recommended to use line breaks '\n').

See also:

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

let mut case: u32 = 0;
Condition::new(Scalar::new(&mut case), "case")
    .help("--this will get discarded--")
    .help("Choose the 'case' to execute.  Description may include multiple sentences.");
Source

pub fn meta(self, description: Vec<impl Into<String>>) -> Self

Document the meta message(s) for this sub-command condition. If repeated, only the final message will apply to the sub-command condition.

Meta message(s) describe short format extra details about the condition. We recommend non-sentence information for this field.

See also:

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

let mut case: u32 = 0;
Condition::new(Scalar::new(&mut case), "case")
    .meta(vec!["--this will get discarded--"])
    .meta(vec!["final extra", "details"]);

Trait Implementations§

Source§

impl<'a, T: FromStr + Display> Choices<T> for Condition<'a, T>

Source§

fn choice(self, variant: T, description: impl Into<String>) -> Self

Document a choice’s help message for the sub-command condition. If repeated for the same variant of T, only the final message will apply to the sub-command condition. Repeat using different variants to document multiple choices. Needn’t be exhaustive.

A choice help message describes the variant in full sentence/paragraph format. We recommend allowing blarg to format this field (ex: it is not recommended to use line breaks '\n').

Notice, the documented or un-documented choices do not affect the actual command parser semantics. To actually limit the command parser semantics, be sure to use an enum.

See also:

§Example
use blarg::{prelude::*, Condition, Scalar};
use std::str::FromStr;

// Be sure to implement `std::str::FromStr` so that it inverts `std::fmt::Display`.
enum FooBar {
    Foo,
    Bar,
}

let mut foo_bar: FooBar = FooBar::Foo;
Condition::new(Scalar::new(&mut foo_bar), "foo_bar")
    .choice(FooBar::Foo, "--this will get discarded--")
    .choice(FooBar::Foo, "Do foo'y things.")
    .choice(FooBar::Bar, "Do bar'y things.  Description may include multiple sentences.");

Auto Trait Implementations§

§

impl<'a, T> Freeze for Condition<'a, T>

§

impl<'a, T> !RefUnwindSafe for Condition<'a, T>

§

impl<'a, T> !Send for Condition<'a, T>

§

impl<'a, T> !Sync for Condition<'a, T>

§

impl<'a, T> Unpin for Condition<'a, T>

§

impl<'a, T> !UnwindSafe for Condition<'a, T>

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.