Giter Site home page Giter Site logo

cmdtree's Introduction

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!

cmdtree's People

Contributors

kurtlawrence avatar

Stargazers

 avatar  avatar Nicolas Marshall avatar Jonathan McHugh avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

cloudcalvin

cmdtree's Issues

Panic in completion.rs

thread 'main' panicked at 'failed to start interface: Os { code: 6, kind: Other, message: "The handle is invalid." }', .\.cargo\registry\src\github.com-1ecc6299db9ec823\cmdtree-0.10.1\src\completion.rs:24:53
stack backtrace:
``
This is what I am getting when attempting to run example program on Windows 10.  Does this library support Windows?

I am a newbie to rust, but not a newbie coder.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.