Cli

Struct Cli 

Source
pub struct Cli<'a> { /* private fields */ }
Expand description

Opaque struct holding the registered Commands

Implementations§

Source§

impl<'a> Cli<'a>

Source

pub fn new() -> Cli<'a>

Constructs a new Cli<'a>

This object holds all commands and callbacks registered with it

Source

pub fn register<T: FnMut(Vec<&str>) + 'a>( &mut self, cmd: Vec<&'a str>, exec: T, ) -> Result<(), ()>

Registers a command with an exec callback When multiple commands get registered with matching prefixes, then a call to complete will return all “subcommands” available for the given input

cli.register(vec!["foo", "bar"], | args | { println!("called with: {:?}", args); });
// calling complete with "foo" would now return a Vec containing "bar"

cli.register(vec!["foo", "baz"], | args | { println!("called with: {:?}", args); });
// calling complete with "foo" would now return a Vec containing "bar" and "baz"
Source

pub fn register_dyn_complete<T: FnMut(Vec<&str>) + 'a, U: for<'b> FnMut(Vec<&'b str>) -> Vec<&'a str> + 'a>( &mut self, cmd: Vec<&'a str>, exec: T, complete: U, ) -> Result<(), ()>

Registers a command with an exec callback and a dynamic complete callback This comes in handy if the completion is based on data that can change at runtime like a list of active sessions

cli.register_dyn_complete(vec!["foo"],
    | args | { println!("called with: {:?}", args); },
    | args | { vec!["bar", "baz"] });

cli.complete("foo"); // --> vec!["bar", "baz"]
Source

pub fn complete<'b>(&mut self, argv: &'b str) -> Vec<&'a str>

Returns a Vector with “subcommands” that can either complete the current work or follow the given input for registered commands

cli.register(vec!["foo","bar"], | _ | {});
assert!(vec!["foo"] == cli.complete("f"));
assert!(vec!["bar"] == cli.complete("foo"));

cli.register(vec!["foo","baz"], | _ | {}); // -> vec!["bar", "baz"]
Source

pub fn exec<'b>(&mut self, cmd: &'b str)

Calls the execute callback registered with a command specified by cmd

cli.register(vec!["foo"], | args | { println!("called with: {:?}", args); });
cli.exec("foo");

Auto Trait Implementations§

§

impl<'a> Freeze for Cli<'a>

§

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

§

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

§

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

§

impl<'a> Unpin for Cli<'a>

§

impl<'a> !UnwindSafe for Cli<'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.