Struct Commander

Source
pub struct Commander<R> { /* private fields */ }
Expand description

A constructed command tree.

Most of the time a user will want to use run() which will handle all the parsing and navigating of the tree. Alternatively, parse_line can be used to simulate a read input and update the command tree position.

To construct a command tree, look at the builder module.

Implementations§

Source§

impl<'r, R> Commander<R>

Source

pub fn run_with_completion<C: 'static + Completer<DefaultTerminal>, F: Fn(&Self) -> C>( self, completer_fn: F, )

Run the Commander interactively, with a completer constructed on every loop. Consumes the instance, and blocks the thread until the loop is exited.

See examples for how to construct a completer.

Source§

impl<R> Commander<R>

Source

pub fn parse_line<W: Write>( &mut self, line: &str, colourise: bool, writer: &mut W, ) -> LineResult<R>

Parse a line of commands and updates the Commander state.

Parsing a line is akin to sending an input line to the commander in the run loop. Commands are space separated, and executed within this function, any actions that are specified will be invoked.

Most branches result in a LineResult::Continue apart from an exit command which will result in a LineResult::Exit. It is up to the developer to decide on the behaviour.

§Example
use cmdtree::*;
let mut cmder = Builder::default_config("base")
       .begin_class("one", "")
       .begin_class("two", "")
    .add_action("echo", "", |_wtr, args| println!("{}", args.join(" ")))
       .into_commander().unwrap();

   assert_eq!(cmder.path(), "base");
   cmder.parse_line("one two", true,  &mut std::io::sink());
   assert_eq!(cmder.path(), "base.one.two");
cmder.parse_line("echo Hello, world!", true, &mut std::io::sink());    // should print "Hello, world!"
Source§

impl<R> Commander<R>

Source

pub fn root_name(&self) -> &str

Return the root name.

§Example
let mut cmder = Builder::default_config("base")
       .begin_class("one", "")
       .begin_class("two", "")
       .into_commander().unwrap();

assert_eq!(cmder.root_name(), "base");
Source

pub fn path(&self) -> &str

Return the path of the current class, separated by ..

§Example
let mut cmder = Builder::default_config("base")
       .begin_class("one", "")
       .begin_class("two", "")
       .into_commander().unwrap();

assert_eq!(cmder.path(), "base");
cmder.parse_line("one two", true,  &mut std::io::sink());
assert_eq!(cmder.path(), "base.one.two");
Source

pub fn at_root(&self) -> bool

Returns if the commander is sitting at the root class.

§Example
let mut cmder = Builder::default_config("base")
       .begin_class("one", "")
       .begin_class("two", "")
       .into_commander().unwrap();

assert!(cmder.at_root());
cmder.parse_line("one two", true,  &mut std::io::sink());
assert_eq!(cmder.at_root(), false);
Source

pub fn run(self)

Run the Commander interactively. Consumes the instance, and blocks the thread until the loop is exited.

This is the most simple way of using a Commander.

Source

pub fn structure(&self, from_root: bool) -> BTreeSet<StructureInfo>

Returns the command structure as a sorted set.

Can return from the the current class or the root.

Each item is a dot separated path, except for actions which are separated by a double dot.

§Examples
let cmder = Builder::default_config("base")
       .begin_class("one", "")
       .begin_class("two", "")
    .end_class()
    .add_action("action", "", |_,_| ())
    .end_class()
    .add_action("action", "", |_,_| ())
       .into_commander().unwrap();

let structure = cmder.structure(true);

assert_eq!(structure.iter().map(|x| x.path.as_str()).collect::<Vec<_>>(), vec![
    "..action",
    "one",
    "one..action",
    "one.two",
]);

Auto Trait Implementations§

§

impl<R> Freeze for Commander<R>

§

impl<R> RefUnwindSafe for Commander<R>

§

impl<R> Send for Commander<R>

§

impl<R> Sync for Commander<R>

§

impl<R> Unpin for Commander<R>

§

impl<R> UnwindSafe for Commander<R>

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.