aboutsummaryrefslogtreecommitdiff
path: root/examples/example-command-macro/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/example-command-macro/src/main.rs')
-rw-r--r--examples/example-command-macro/src/main.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/example-command-macro/src/main.rs b/examples/example-command-macro/src/main.rs
new file mode 100644
index 0000000..2bf932a
--- /dev/null
+++ b/examples/example-command-macro/src/main.rs
@@ -0,0 +1,66 @@
+//! 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() {
+ let mut program = ThisProgram::new();
+
+ // Import the dispatchers generated by the `#[command]` macro
+ program.with_dispatcher(CMDHelloWorld);
+ program.with_dispatcher(CMDGreetSomeone);
+ program.with_dispatcher(CMDGoodBye);
+
+ program.exec_and_exit();
+}
+
+pack!(ResultGreeting = String);
+pack!(ResultGoodbye = ());
+
+// --------- IMPORTANT ---------
+// Auto-generates dispatcher!("hello.world", CMDHelloWorld => EntryHelloWorld);
+#[command]
+fn hello_world() -> ResultGreeting {
+ ResultGreeting::new("World".to_string())
+}
+
+// Auto-generates dispatcher!("hello-world", CMDGreetSomeone => EntryGreetSomeone);
+#[command(node = "greet-someone")]
+fn greet_someone(args: Vec<String>) -> ResultGreeting {
+ let name = args.pick_or(&arg![String], || "World".to_string()).unwrap();
+ ResultGreeting::new(name)
+}
+
+// Auto-generates dispatcher!("goodbye", CMDGoodBye => EntryGoodBye);
+#[command(name = CMDGoodBye, entry = EntryGoodBye)]
+fn goodbye() -> ResultGoodbye {
+ ResultGoodbye::default()
+}
+// --------- IMPORTANT ---------
+
+#[renderer(buffer)]
+fn render_greeting(result: ResultGreeting) {
+ r_println!("Hello, {}", *result);
+}
+
+#[renderer(buffer)]
+fn render_goodbye(_: ResultGoodbye) {
+ r_println!("Goodbye!");
+}
+
+gen_program!();