diff options
Diffstat (limited to 'mingling')
| -rw-r--r-- | mingling/src/example_docs.rs | 90 | ||||
| -rw-r--r-- | mingling/src/lib.rs | 6 |
2 files changed, 96 insertions, 0 deletions
diff --git a/mingling/src/example_docs.rs b/mingling/src/example_docs.rs index 35cf024..3e9bd26 100644 --- a/mingling/src/example_docs.rs +++ b/mingling/src/example_docs.rs @@ -768,6 +768,96 @@ pub mod example_clap_binding {} /// gen_program!(); /// ``` pub mod example_combine_pathf_dispatch_tree {} +/// 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! +/// ``` +/// +/// Source code (./Cargo.toml) +/// ```toml +/// [package] +/// name = "example-command-macro" +/// version = "0.1.0" +/// edition = "2024" +/// +/// [dependencies.mingling] +/// path = "../../mingling" +/// features = [ +/// # Use `extra_macros` to introduce the `#[command]` macro +/// "extra_macros", +/// +/// # Use `picker` to parse arguments +/// "picker", +/// ] +/// +/// [workspace] +/// ``` +/// +/// Source code (./src/main.rs) +/// ```ignore +/// 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!(); +/// ``` +pub mod example_command_macro {} /// Example Completion /// /// > This example demonstrates how to use **Mingling** to create fully dynamic command-line completions diff --git a/mingling/src/lib.rs b/mingling/src/lib.rs index 19f2188..d7c8e8f 100644 --- a/mingling/src/lib.rs +++ b/mingling/src/lib.rs @@ -52,6 +52,9 @@ pub mod macros { pub use mingling_macros::buffer; /// `#[chain]` - Used to generate a struct implementing the `Chain` trait via a method pub use mingling_macros::chain; + /// `#[command]` - Declares a command from a plain function with `Vec<String>` entry + #[cfg(feature = "extra_macros")] + pub use mingling_macros::command; /// `#[completion(EntryType)]` - Used to generate completion entry #[cfg(feature = "comp")] pub use mingling_macros::completion; @@ -223,6 +226,9 @@ pub mod prelude { /// Re-export of the `chain` macro for defining a chain of commands. #[cfg(feature = "macros")] pub use crate::macros::chain; + /// Re-export of the `#[command]` macro for declaring commands from plain functions. + #[cfg(all(feature = "extra_macros", feature = "macros"))] + pub use crate::macros::command; /// Re-export of the `dispatcher` macro for routing commands. #[cfg(feature = "macros")] pub use crate::macros::dispatcher; |
