aboutsummaryrefslogtreecommitdiff
path: root/examples/example-command-macro/src/main.rs
blob: 6740518ea6c4567a64d29995f137aacac1342044 (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
//! Example Command Macro
//!
//! > Introduced how to use the `#[command]` macro to generate commands with minimal boilerplate
//!
//! Run:
//! ```base
//! cargo run --manifest-path examples/example-command-macro/Cargo.toml --quiet -- hello world
//! cargo run --manifest-path examples/example-command-macro/Cargo.toml --quiet -- greet-someone Alice
//! cargo run --manifest-path examples/example-command-macro/Cargo.toml --quiet -- goodbye
//! ```
//!
//! Output:
//! ```plaintext
//! Hello, World
//! Hello, Alice
//! Goodbye!
//! ```

use mingling::{macros::buffer, picker::IntoPicker, prelude::*};

fn main() {
    ThisProgram::new().exec_and_exit();
}

#[derive(Grouped, Wrap)]
pub struct ResultGreeting(String);

#[derive(Grouped)]
pub struct ResultGoodbye;

// --------- IMPORTANT ---------
// Auto-generates dispatcher!("hello.world", EntryHelloWorld);
#[command]
fn hello_world() -> ResultGreeting {
    ResultGreeting("World".to_string())
}

// Auto-generates dispatcher!("hello-world", EntryGreetSomeone);
#[command(node = "greet-someone")]
fn greet_someone(args: Vec<String>) -> ResultGreeting {
    let name = args.pick_or(&arg![String], || "World".to_string()).unwrap();
    ResultGreeting(name)
}

// Auto-generates dispatcher!("goodbye", EntryGoodBye);
#[command(entry = EntryGoodBye)]
fn goodbye() -> ResultGoodbye {
    ResultGoodbye
}
// --------- IMPORTANT ---------

#[renderer(buffer)]
fn render_greeting(result: ResultGreeting) {
    r_println!("Hello, {}", *result);
}

#[renderer(buffer)]
fn render_goodbye(_: ResultGoodbye) {
    r_println!("Goodbye!");
}

gen_program!();