1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! A crate for building shell interfaces in Rust.
//!
//! See the README.md and examples for more information.

use std::result;

pub mod command;
mod command_set;
pub mod error;
mod parser;
mod readline;
pub mod shell;
mod tokenizer;

pub type Result<T> = result::Result<T, error::ShiError>;

/// Creates a parent command that has child subcommands underneath it.
#[macro_export]
macro_rules! parent {
    ( $name:expr, $help:literal, $( $x:expr ),* $(,)? ) => {
        {
            $crate::command::Command::Parent(
                $crate::command::ParentCommand::new_with_help(
                    $name,
                    $help,
                    vec![
                    $(
                        $x,
                    )*
                    ],
                )
            )
        }
    };
    ( $name:expr, $( $x:expr ),* $(,)? ) => {
        {
            $crate::command::Command::new_parent(
                $name,
                vec![
                $(
                    $x,
                )*
                ],
            )
        }
    };
}

/// Creates a leaf command from a given Command.
#[macro_export]
macro_rules! leaf {
    ( $cmd:expr ) => {
        $crate::command::Command::new_leaf($cmd)
    };
}

/// Creates a leaf command from the given name and closure.
#[macro_export]
macro_rules! cmd {
    ( $name:expr, $exec:expr ) => {
        $crate::leaf!($crate::command::BasicCommand::new($name, $exec))
    };
    ( $name:expr, $help:literal, $exec:expr ) => {
        $crate::leaf!($crate::command::BasicCommand::new_with_help(
            $name, $help, $exec
        ))
    };
    // Allow trailing comma.
    ( $name:expr, $help:literal, $exec:expr, ) => {
        $crate::leaf!($crate::command::BasicCommand::new_with_help(
            $name, $help, $exec
        ))
    };
}