aboutsummaryrefslogtreecommitdiff
path: root/examples/example-dispatch-tree
diff options
context:
space:
mode:
Diffstat (limited to 'examples/example-dispatch-tree')
-rw-r--r--examples/example-dispatch-tree/Cargo.lock10
-rw-r--r--examples/example-dispatch-tree/Cargo.toml8
-rw-r--r--examples/example-dispatch-tree/src/main.rs76
3 files changed, 50 insertions, 44 deletions
diff --git a/examples/example-dispatch-tree/Cargo.lock b/examples/example-dispatch-tree/Cargo.lock
index 4085ce5..4b6c7eb 100644
--- a/examples/example-dispatch-tree/Cargo.lock
+++ b/examples/example-dispatch-tree/Cargo.lock
@@ -16,15 +16,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5454cda0d57db59778608d7a47bff5b16c6705598265869fb052b657f66cf05e"
[[package]]
-name = "just_template"
-version = "0.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "db3edb658c34b10b69c4b3b58f7ba989cd09c82c0621dee1eef51843c2327225"
-dependencies = [
- "just_fmt",
-]
-
-[[package]]
name = "mingling"
version = "0.1.9"
dependencies = [
@@ -37,7 +28,6 @@ name = "mingling_core"
version = "0.1.9"
dependencies = [
"just_fmt",
- "just_template",
]
[[package]]
diff --git a/examples/example-dispatch-tree/Cargo.toml b/examples/example-dispatch-tree/Cargo.toml
index f1c6785..0c31ecb 100644
--- a/examples/example-dispatch-tree/Cargo.toml
+++ b/examples/example-dispatch-tree/Cargo.toml
@@ -3,5 +3,9 @@ name = "example-dispatch-tree"
version = "0.1.0"
edition = "2024"
-[dependencies]
-mingling = { path = "../../mingling", features = ["dispatch_tree", "comp"] }
+[dependencies.mingling]
+path = "../../mingling"
+
+features = [
+ "dispatch_tree",
+]
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!();