example/
example.rs

1#[macro_use]
2extern crate tshell;
3
4use tshell::{CommandTree, CommandResult};
5use std::collections::HashMap;
6
7#[derive(Debug, Default)]
8pub struct Context {
9    memory: Option<String>
10}
11
12fn world(args: HashMap<String, &str>, o_context: &mut Option<Context>, history: &HashMap<String, String>) -> CommandResult<Option<String>> {
13    if let Some(context) = history.get("context") {
14        println!("Context: {}", context);
15    }
16    println!("World");
17    Ok(None)
18}
19
20fn darkness(args: HashMap<String, &str>, o_context: &mut Option<Context>, history: &HashMap<String, String>) -> CommandResult<Option<String>> {
21    if let Some(friend) = args.get("friend") {
22        println!("Darkness, friend = {}", friend);
23    }
24    else {
25        println!("friend argument is required!");
26    }
27    Ok(None)
28}
29
30fn new_context(args: HashMap<String, &str>, o_context: &mut Option<Context>, history: &HashMap<String, String>) -> CommandResult<Option<String>> {
31    if let Some(context) = args.get("context") {
32        Ok(Some(format!("context:{}", context)))
33    }
34    else {
35        println!("context argument is required!");
36        Ok(None)
37    }
38}
39
40
41fn main() {
42    let mut context = Context::default();
43    let mut o_context = Some(context);
44    let mut root = shell_command_tree!{my_cli,
45        "MyCLI",
46        "0.1.0",
47        o_context.unwrap(),
48        [
49            shell_command_node!{
50                cmd: hello,
51                txt_help: "Hello Root",
52                nodes: [
53                    shell_command_node!{
54                        cmd: world,
55                        txt_help: "World",
56                        callback: world
57                    },
58                    shell_command_node!{
59                        cmd: darkness,
60                        txt_help: "Darkness",
61                        callback: darkness,
62                        args: [friend => true]
63                    }
64                ]
65            },
66            shell_command_node!{
67                cmd: context,
68                txt_help: "Hello Root",
69                callback: new_context,
70                args: [context => true],
71                nodes: [
72                    shell_command_node!{
73                        cmd: world,
74                        txt_help: "World",
75                        callback: world
76                    },
77                    shell_command_node!{
78                        cmd: darkness,
79                        txt_help: "Darkness",
80                        callback: darkness,
81                        args: [friend => true]
82                    }
83                ]
84            }]
85        };
86        root.run();
87}