15个版本 (重大变更)
0.10.1 | 2020年7月14日 |
---|---|
0.10.0 | 2019年8月26日 |
0.9.0 | 2019年6月12日 |
0.4.1 | 2019年3月23日 |
#586 in 数据结构
每月 23 次下载
在 papyrus 中使用
52KB
998 代码行
(Rust) 命令树。
cmdtree
创建一个命令和动作的树形数据结构,以添加直观和交互式的体验到应用程序中。cmdtree使用构建者模式使构建树更加便捷。
示例
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
}
现在运行并在您的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!
依赖
~0–11MB
~74K SLoC