14 releases (breaking)

✓ Uses Rust 2018 edition

0.10.0 Aug 26, 2019
0.9.0 Jun 12, 2019
0.8.0 Jun 3, 2019
0.4.1 Mar 23, 2019

#20 in #command

Download history 106/week @ 2020-03-16 119/week @ 2020-03-23 46/week @ 2020-03-30 72/week @ 2020-04-06 82/week @ 2020-04-13 301/week @ 2020-04-20 119/week @ 2020-04-27 97/week @ 2020-05-04 56/week @ 2020-05-11 51/week @ 2020-05-18 46/week @ 2020-05-25 82/week @ 2020-06-01 69/week @ 2020-06-08 54/week @ 2020-06-15 69/week @ 2020-06-22 50/week @ 2020-06-29

355 downloads per month
Used in papyrus

MIT and MPL-2.0 licenses

52KB
998 lines

Build Status Latest Version Rust Documentation codecov

(Rust) commands tree.

See the rs docs. Look at progress and contribute on github.

cmdtree

Create a tree-like data structure of commands and actions to add an intuitive and interactive experience to an application. cmdtree uses a builder pattern to make constructing the tree ergonomic.

Example

extern crate cmdtree;
use cmdtree::*;

fn main() {
  let cmder = Builder::default_config("cmdtree-example")
    .begin_class("class1", "class1 help message") // a class
    .begin_class("inner-class1", "nested class!") // can nest a class
    .add_action("name", "print class name", |mut wtr, _args| {
      writeln!(wtr, "inner-class1",).unwrap()
    })
    .end_class()
    .end_class() // closes out the classes
    .begin_class("print", "pertains to printing stuff") // start another class sibling to `class1`
    .add_action("echo", "repeat stuff", |mut wtr, args| {
      writeln!(wtr, "{}", args.join(" ")).unwrap()
    })
    .add_action("countdown", "countdown from a number", |mut wtr, args| {
      if args.len() != 1 {
        println!("need one number",);
      } else {
        match str::parse::<u32>(args[0]) {
          Ok(n) => {
            for i in (0..=n).rev() {
              writeln!(wtr, "{}", i).unwrap();
            }
          }
          Err(_) => writeln!(wtr, "expecting a number!",).unwrap(),
        }
      }
    })
    .into_commander() // can short-circuit the closing out of classes
    .unwrap();

  cmder.run(); // run interactively
}

Now run and in your shell:

cmdtree-example=> help            <-- Will print help messages
help -- prints the help messages
cancel | c -- returns to the root class
exit -- sends the exit signal to end the interactive loop
Classes:
        class1 -- class1 help message
        print -- pertains to printing stuff
cmdtree-example=> print            <-- Can navigate the tree
cmdtree-example.print=> help
help -- prints the help messages
cancel | c -- returns to the root class
exit -- sends the exit signal to end the interactive loop
Actions:
        echo -- repeat stuff
        countdown -- countdown from a number
cmdtree-example.print=> echo hello, world!  <-- Call the actions
hello, world!
cmdtree-example.print=> countdown
need one number
cmdtree-example.print=> countdown 10
10
9
8
7
6
5
4
3
2
1
0
cmdtree-example.print=> exit      <-- exit the loop!

Dependencies

~71–700KB
~16K SLoC