From 0a2ef958c0dca21d19e4ffc38ba5a7c4078e182a Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Sat, 23 May 2026 23:41:04 +0800 Subject: Rework examples and add entry macro for testing --- examples/example-dispatch-tree/src/main.rs | 76 +++++++++++++++++------------- 1 file changed, 44 insertions(+), 32 deletions(-) (limited to 'examples/example-dispatch-tree/src/main.rs') diff --git a/examples/example-dispatch-tree/src/main.rs b/examples/example-dispatch-tree/src/main.rs index d8be32a..08714d1 100644 --- a/examples/example-dispatch-tree/src/main.rs +++ b/examples/example-dispatch-tree/src/main.rs @@ -1,47 +1,59 @@ -//! `Mingling` Example - Dispatch Tree +//! Example Dispatch Tree //! -//! # How to Deploy -//! 1. Enable the `dispatch_tree` feature (`comp` is optional) -//! ```toml -//! mingling = { version = "...", features = [ -//! "dispatch_tree", // Enable this feature -//! "comp" // optional -//! ] } -//! ``` +//! > This example will introduce how to use `dispatch_tree` +//! > to optimize your command line lookup efficiency +//! +//! When the number of commands in your project increases, you can use `dispatch_tree` to complete command registration at compile time. +//! It will generate a trie for quickly finding related commands by prefix. //! -//! 2. Using `cargo expand`: +//! Therefore, after enabling this feature, +//! `Program` will no longer store a Dispatcher list internally, and the `with_dispatcher` function will not be compiled. //! +//! Run: //! ```bash -//! cargo expand --manifest-path examples/example-dispatch-tree/Cargo.toml > expanded.rs -//! cat expanded.rs +//! cargo run --manifest-path examples/example-dispatch-tree/Cargo.toml --quiet -- cmd5 //! ``` - -#![allow(unused_mut)] +//! +//! Output: +//! ```plaintext +//! It's works! +//! ``` +//! use mingling::prelude::*; +// --------- IMPORTANT --------- +// You have a large number of subcommands +dispatcher!("cmd1", CMD1 => Entry1); +dispatcher!("cmd2.sub1", CMD2Sub1 => Entry2Sub1); +dispatcher!("cmd2.sub2", CMD2Sub2 => Entry2Sub2); +dispatcher!("cmd3.sub1.leaf1", CMD3Sub1Leaf1 => Entry3Sub1Leaf1); +dispatcher!("cmd3.sub1.leaf2", CMD3Sub1Leaf2 => Entry3Sub1Leaf2); +dispatcher!("cmd3.sub2", CMD3Sub2 => Entry3Sub2); +dispatcher!("cmd4.sub1.subsub1.deep", CMD4Deep => Entry4Deep); +dispatcher!("cmd4.sub1.subsub2", CMD4SubSub2 => Entry4SubSub2); +dispatcher!("cmd5", CMD5 => Entry5); +dispatcher!("cmd5.extra", CMD5Extra => Entry5Extra); +dispatcher!("nested.a.b.c", CMDA => EntryA); +dispatcher!("nested.a.b.d", CMDB => EntryB); +dispatcher!("nested.a.e", CMDC => EntryC); +dispatcher!("nested.f", CMDD => EntryD); +// --------- IMPORTANT --------- + fn main() { - let mut program = ThisProgram::new(); + let program = ThisProgram::new(); - // // After enabling `dispatch_tree`, this method will no longer exist - // program.with_dispatcher(CommandGreet); - // - // // The `CompletionDispatcher` automatically generated by `comp` will also be imported automatically - // program.with_dispatcher(CompletionDispatcher); + // --------- IMPORTANT --------- + // // You no longer need to use `with_dispatcher` anymore; + // // it'll be collected automatically once the `dispatch_tree` feature is enabled + // program.with_dispatcher(...); - program.exec(); + program.exec_and_exit() } -dispatcher!("greet", CommandGreet => EntryGreet); -dispatcher!("help", CommandHelp => EntryHelp); -dispatcher!("quit", CommandQuit => EntryQuit); -dispatcher!("list", CommandList => EntryList); -dispatcher!("status", CommandStatus => EntryStatus); -dispatcher!("save", CommandSave => EntrySave); -dispatcher!("load", CommandLoad => EntryLoad); -dispatcher!("config", CommandConfig => EntryConfig); -dispatcher!("run", CommandRun => EntryRun); -dispatcher!("debug", CommandDebug => EntryDebug); -dispatcher!("version", CommandVersion => EntryVersion); +#[renderer] +fn render_cmd5(_: Entry5) { + r_println!("It's works!"); +} gen_program!(); -- cgit