pub struct Cli<'a> { /* private fields */ }Expand description
Opaque struct holding the registered Commands
Implementations§
Source§impl<'a> Cli<'a>
impl<'a> Cli<'a>
Sourcepub fn new() -> Cli<'a>
pub fn new() -> Cli<'a>
Constructs a new Cli<'a>
This object holds all commands and callbacks registered with it
Sourcepub fn register<T: FnMut(Vec<&str>) + 'a>(
&mut self,
cmd: Vec<&'a str>,
exec: T,
) -> Result<(), ()>
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"Sourcepub 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<(), ()>
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"]Sourcepub fn complete<'b>(&mut self, argv: &'b str) -> Vec<&'a str>
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"]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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more