[−][src]Struct cmdtree::Commander
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.
Methods
impl<'r, R> Commander<'r, R>
[src]
pub fn parse_line<W: Write>(
&mut self,
line: &str,
colourise: bool,
writer: &mut W
) -> LineResult<R>
[src]
&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!"
impl<'r, R> Commander<'r, R>
[src]
pub fn root_name(&self) -> &str
[src]
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");
pub fn path(&self) -> &str
[src]
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");
pub fn at_root(&self) -> bool
[src]
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);
pub fn run(self)
[src]
Run the Commander
interactively.
Consumes the instance, and blocks the thread until the loop is exited.
Reads from stdin
using linefeed::Interface
.
This is the most simple way of using a Commander
.
pub fn structure(&self) -> BTreeSet<String>
[src]
Returns the command structure as a sorted set.
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(); assert_eq!(structure.iter().map(|x| x.as_str()).collect::<Vec<_>>(), vec![ "base", "base..action", "base.one", "base.one..action", "base.one.two", ]);
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
ⓘImportant traits for &'_ mut Wfn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,