aboutsummaryrefslogtreecommitdiff
path: root/examples/example-basic/src/main.rs
blob: d741c3b940eaeb6b56466f98b51ac4557b80903a (plain) (blame)
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
//! Example The Basic Usage of Mingling
//!
//! Run:
//! ```base
//! cargo run --manifest-path examples/example-basic/Cargo.toml --quiet -- greet
//! cargo run --manifest-path examples/example-basic/Cargo.toml --quiet -- greet Alice
//! ```
//!
//! Output:
//! ```plaintext
//! Hello, World!
//! Hello, Alice!
//! ```

// Import commonly used Mingling modules
use mingling::prelude::*;

// Define the `greet` subcommand
//            _____________________________ subcmd name, can be nested (e.g. "remote.add" "remote.rm")
//           /        _____________________ dispatcher name
//           |       /            _________ entry, records raw arguments
//           |       |           /                         ^^^^^^^^^^^^^
//           vvvvv   vvvvvvvv    vvvvvvvvvv                \_ equivalent to pack!(EntryGreet = Vec<String>)
dispatcher!("greet", CMDGreet => EntryGreet);

fn main() {
    // Create a new ThisProgram
    let mut program = ThisProgram::new();

    // Add the CMDGreet dispatcher
    program.with_dispatcher(CMDGreet);

    // Run the program, then exit the process
    program.exec_and_exit();
}

// Quickly wrap a type into a type recognizable by the current program
//     ____________________ Wrapped type name
//    /             _______ Wrapped type inner value
//    |            /
//    vvvvvvvvvv   vvvvvv
pack!(ResultName = String);

// Define the `handle_greet` chain for parsing input text
//                     ____________________ Previous type:
//                    /                       Mingling deduces types at runtime and routes them to this function
//                    |               _____ will be expanded to:
//                    |              /        impl Into<mingling::ChainProcess<ThisProgram>>
#[chain] //           vvvvvvvvvv     vvvv
fn handle_greet(args: EntryGreet) -> Next {
    let name: ResultName = args
        .inner
        .first()
        .cloned()
        .unwrap_or_else(|| "World".to_string())
        .into();
    name
}

// Define renderer `render_name`, used to render `ResultName`
#[renderer]
fn render_name(name: ResultName) {
    r_println!("Hello, {}!", *name);
}

// Note: This macro generates the program entry point.
// It must be placed at the end of the root module of the crate (>= mingling@0.1.8).
//                          ^^^^^^     ^^^^^^^^^^^
// For example: lib.rs, main.rs
gen_program!();