commander_rust

Struct Cli

Source
pub struct Cli {
    pub cmd: Option<Cmd>,
    pub global_raws: HashMap<String, Raw>,
    pub direct_args: Vec<Raw>,
}
Expand description

Something like Cmd.

But unfortunately, you will use it frequently.

commander_rust will generate a instance of Application according your code. It happens in compile-time. commander_rust will generate a instance of Cli according user’s input. It happens in run-time. What’ the difference? Content in Application will be replaced by something concrete through user’s input. For example, If your code is like this:

#[option(-r, --recursive [dir...], "recursively")]
#[command(rmdir <dir> [otherDirs...], "remove files and directories")]
fn rmdir(dir: i32, other_dirs: Option<Vec<bool>>, cli: Cli) {
    let r: bool = cli.get("recursive").into();
}

Let’s see. The last argument of function is Cli type(you can miss it). So when we want to do something if --recursive is offered by user, how can we? You just need to code like let r: ? = cli.get("recursive").into(), then you can get contents of recursive options if user has inputted it.

That’s why Cli will be used frequently.

Fields§

§cmd: Option<Cmd>§global_raws: HashMap<String, Raw>§direct_args: Vec<Raw>

Implementations§

Source§

impl Cli

Source

pub fn get(&self, idx: &str) -> Raw

Get the content of Options. Options has two types, one is private, the other is global. Of course they are same. Private means they belong to the command. Global means they belong to the global.

Private is more weight than global.

Source

pub fn get_or<T>(&self, idx: &str, d: T) -> T
where T: From<Raw>,

Getting contents of Options. if idx dont exist, return default.

Source

pub fn get_or_else<T, F>(&self, idx: &str, f: F) -> T
where T: From<Raw>, F: FnOnce() -> T,

Get contents of Options. if idx dont exist, call f.

f should return a value of type T.

Source

pub fn has(&self, idx: &str) -> bool

Check user input a option or not.

Trait Implementations§

Source§

impl Debug for Cli

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Cli

§

impl RefUnwindSafe for Cli

§

impl Send for Cli

§

impl Sync for Cli

§

impl Unpin for Cli

§

impl UnwindSafe for Cli

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.