aboutsummaryrefslogtreecommitdiff
path: root/docs/_zh_CN/pages
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-06-15 00:36:34 +0800
committer魏曹先生 <1992414357@qq.com>2026-06-15 00:36:34 +0800
commite214d07f6783009869c93bbab1e4bec9829584ee (patch)
tree2f5e4ebeba925a5676728f498119a614ffc654b5 /docs/_zh_CN/pages
parent14666265b235626c3f0f5b60fb4e9969f9e7bf3c (diff)
Add Introduction, Getting Started, and Features docs
Diffstat (limited to 'docs/_zh_CN/pages')
-rw-r--r--docs/_zh_CN/pages/1-intro.md32
-rw-r--r--docs/_zh_CN/pages/2-getting-started.md62
-rw-r--r--docs/_zh_CN/pages/other/features.md246
3 files changed, 340 insertions, 0 deletions
diff --git a/docs/_zh_CN/pages/1-intro.md b/docs/_zh_CN/pages/1-intro.md
new file mode 100644
index 0000000..7210d2e
--- /dev/null
+++ b/docs/_zh_CN/pages/1-intro.md
@@ -0,0 +1,32 @@
+<h1 align="center">介绍</h1>
+
+如果你正被这些问题困扰——
+
+1. 子命令一多,`main.rs` 就迅速膨胀;
+2. Handler 中的逻辑与副作用混杂在一起;
+3. 日志、鉴权等横切关注点难以无侵入地插入;
+4. 为多个平台维护 shell 补全脚本令人心力交瘁;
+5. 全局资源到处都是,测试非常困难。
+
+…… 那么,你来对地方了。
+
+当然,如果只是对「怎么用 Rust 写出好维护的 CLI」这件事感兴趣,那你更来对地方了 —— 它会很有意思的。
+
+## 什么是 Mingling?
+
+> **Mìng Lìng** 是中文 **命令** 的汉语拼音,
+> 对应的英文单词是 **Command**。
+
+Mingling 是一个用 Rust 构建的 CLI 框架。它免费、开源,且使用宽松的 MIT / Apache 2.0 开源协议。
+
+Mingling 的设计目标:
+
+- **可扩展**:从 3 个子命令到 30 个,同一套模式,不换框架
+- **解耦**:参数解析写一次,业务逻辑写一次,输出格式写一次。各不相干
+- **类型驱动**:整个流水线上传递的是清晰、类型化的数据,而非 `Vec<String>`
+- **轻量依赖**:核心功能依赖少,引入负担低;按需启用高级特性,不拖慢编译
+- **高效**:编译期生成分发逻辑,运行时没有不必要的开销
+
+---
+
+好了,想来试试么?
diff --git a/docs/_zh_CN/pages/2-getting-started.md b/docs/_zh_CN/pages/2-getting-started.md
new file mode 100644
index 0000000..57b32e4
--- /dev/null
+++ b/docs/_zh_CN/pages/2-getting-started.md
@@ -0,0 +1,62 @@
+<h1 align="center">起步</h1>
+
+## 创建一个新项目
+
+```bash
+cargo new my-cli
+cd my-cli
+```
+
+## 添加依赖
+
+在 `Cargo.toml` 写入如下内容
+
+```toml
+[dependencies.mingling]
+version = "0.2"
+features = []
+```
+
+## 启用特性
+
+**Mingling** 默认所有特性关闭,且不提供类似 `full` 的全开特性。
+
+因为部分特性会 **直接影响整个生命周期的行为**,需要你按需启用,例如:
+
+```toml
+[dependencies.mingling]
+version = "0.2"
+features = [
+ "parser",
+ "comp",
+]
+```
+
+> [!NOTE]
+> 请前往 [docs.rs](https://docs.rs/mingling/latest/mingling/feature/index.html) 或 [特性](pages/other/features) 以了解所有特性
+
+## 编写基本入口
+
+编写 `src/main.rs`,写入以下代码:
+
+```rust
+use mingling::prelude::*;
+
+fn main() {
+ let mut program = ThisProgram::new();
+
+ program.exec_and_exit();
+}
+
+gen_program!();
+```
+
+## 编译验证
+
+```plaintext
+~# cargo check
+```
+
+---
+
+一切无误后,开始写点什么吧!
diff --git a/docs/_zh_CN/pages/other/features.md b/docs/_zh_CN/pages/other/features.md
new file mode 100644
index 0000000..b04f1b8
--- /dev/null
+++ b/docs/_zh_CN/pages/other/features.md
@@ -0,0 +1,246 @@
+<h1 align="center">特性</h1>
+<p align="center">
+ <b>Mingling</b> 的所有特性一览
+</p>
+
+## 特性 `all_serde_fmt`
+
+**介绍:**
+
+为 `general_renderer` 启用所有序列化格式(JSON、RON、TOML、YAML)的 serde 格式化支持。
+
+开启此特性将自动启用 `json_serde_fmt`、`ron_serde_fmt`、`toml_serde_fmt`、`yaml_serde_fmt` 四个子特性。
+
+## 特性 `async`
+
+**介绍:**
+
+启用异步运行时支持,允许 `#[chain]` 绑定 `async` 函数,例如:
+
+```rust
+// Features: ["async"]
+
+pack!(StateFoo = ());
+
+#[chain]
+async fn handle_state_foo(foo: StateFoo) -> Next {
+ StateFoo::new(())
+}
+```
+
+详见 [示例](https://mingling-rs.github.io/mingling/docs/example-viewer.html?name=example-async-support)
+
+## 特性 `builds`
+
+**介绍:**
+
+启用部分需要在 `build.rs` 使用的脚本,目前包含:
+
+1. `comp` 特性下的补全脚本生成:
+
+```rust
+// Features: ["builds", "comp"]
+use mingling::build::build_comp_scripts;
+
+fn main() {
+ // 为 `myprogram` 生成补全脚本
+ build_comp_scripts("myprogram").unwrap();
+}
+```
+
+## 特性 `clap`
+
+**介绍:**
+
+启用与 [clap](https://crates.io/crates/clap) 命令行参数解析库的集成,方便构建 CLI 应用程序。
+
+开启此特性后,可以使用 `#[dispatcher_clap]` 属性宏从 `clap::Parser` 结构体生成 dispatcher。
+
+详见 [示例](https://mingling-rs.github.io/mingling/docs/example-viewer.html?name=example-clap-binding)
+
+## 特性 `comp`
+
+**介绍:**
+
+启用代码补全功能,为交互式环境提供自动补全支持。
+
+开启后可以使用 `#[completion]` 属性宏定义动态补全逻辑,并支持为 bash、zsh、fish、pwsh 等 shell 生成补全脚本。
+
+详见 [示例](https://mingling-rs.github.io/mingling/docs/example-viewer.html?name=example-completion)
+
+## 特性 `debug`
+
+**介绍:**
+
+启用调试相关功能,提供更详细的错误信息和诊断输出。
+
+## 特性 `dispatch_tree`
+
+**介绍:**
+
+启用调度树机制,支持基于条件的分发和路由功能。
+
+开启后,Mingling 在**编译时**将子命令结构硬编码为前缀树(Trie),实现极速的子命令查找。查找复杂度为 **O(n)**,其中 _n_ 是输入长度,而非命令数量。
+
+详见 [示例](https://mingling-rs.github.io/mingling/docs/example-viewer.html?name=example-dispatch-tree)
+
+## 特性 `extra_macros`
+
+**介绍:**
+
+启用额外的宏集合,提供更多便捷的语法糖和元编程能力。
+
+例如,允许 `dispatcher!("greet")` 的缩写形式,自动生成 `CMDGreet` / `EntryGreet`。
+
+| 宏 | 说明 |
+|---|---|
+| `empty_result!()` | 链中提前返回空结果的简写 |
+| `entry!(Type, ["a", "b"])` | 构造入口类型的测试数据 |
+| `#[program_setup]` | 声明程序初始化函数 |
+| `dispatcher!("cmd.path")` **缩写形式** | 省略 `CMDStruct => EntryStruct`,名字自动推导 |
+
+<details>
+<summary> Details </summary>
+
+### `empty_result!()`
+
+```rust
+// Features: ["extra_macros"]
+
+pack!(StatePrev1 = ());
+pack!(StatePrev2 = ());
+
+pack!(StateNext = ());
+
+#[chain]
+fn handle_state_prev2(_p: StatePrev1) {
+ // 无 Next 的 #[chain] 可以直接不返回值
+}
+
+#[chain]
+fn handle_state_prev1(_p: StatePrev1) -> Next {
+ let foo = 1;
+ let bar = 2;
+ if foo != bar {
+ // 当需要 Next 且不需要返回值,便可以使用它
+ empty_result!()
+ } else {
+ StateNext::new(()).into()
+ }
+}
+```
+
+### `#[program_setup]`
+
+```rust
+// Features: ["extra_macros"]
+use mingling::{macros::program_setup, Program};
+
+fn main() {
+ let mut program = ThisProgram::new();
+ program.with_setup(NoErrorSetup);
+ program.exec_and_exit();
+}
+
+#[program_setup]
+fn no_error_setup(program: &mut Program<ThisProgram>) {
+ program.global_flag(["--no-error"], |program| {
+ program.stdout_setting.error_output = false;
+ });
+}
+```
+
+### `entry!`
+
+```rust
+// Features: ["extra_macros"]
+use mingling::macros::entry;
+
+pack!(EntryHello = Vec<String>);
+
+fn main() {
+ let result = handle_hello(entry!("--name", "Bob")).into();
+ // ... 此处为断言逻辑
+}
+
+#[chain]
+fn handle_hello(args: EntryHello) {}
+```
+
+</details>
+
+## 特性 `general_renderer`
+
+**介绍:**
+
+启用通用渲染器,提供基础的内容渲染能力。开启此特性将自动启用 `json_serde_fmt`。
+
+开启后,用户可以通过 `--json` 或 `--yaml` 等标志获取结构化输出。
+
+详见 [示例](https://mingling-rs.github.io/mingling/docs/example-viewer.html?name=example-general-renderer)
+
+## 特性 `general_renderer_empty`
+
+**介绍:**
+
+启用通用渲染器的空实现版本,适用于不需要实际渲染功能的场景。此特性不启用任何 serde 格式化后端。
+
+## 特性 `general_renderer_full`
+
+**介绍:**
+
+启用通用渲染器的完整实现,包含所有渲染功能和序列化格式支持。开启此特性将自动启用 `all_serde_fmt`。
+
+## 特性 `json_serde_fmt`
+
+**介绍:**
+
+启用 JSON 格式的 serde 序列化/反序列化格式化支持。
+
+## 特性 `nightly`
+
+**介绍:**
+
+启用仅在不稳定(Nightly)Rust 编译器中可用的实验性功能。
+
+## 特性 `parser`
+
+**介绍:**
+
+启用解析器模块,提供文本解析和语法分析功能。
+
+开启后可以使用 `Picker` 进行零成本的参数提取,支持 `pick()` 和 `pick_or()` 等方法。
+
+详见 [示例](https://mingling-rs.github.io/mingling/docs/example-viewer.html?name=example-argument-parse)
+
+## 特性 `repl`
+
+**介绍:**
+
+启用交互式 REPL(Read-Eval-Print Loop)环境支持。
+
+开启后,可以通过 `program.exec_repl()` 将 CLI 转变为交互式 shell。
+
+详见 [示例](https://mingling-rs.github.io/mingling/docs/example-viewer.html?name=example-repl-basic)
+
+## 特性 `ron_serde_fmt`
+
+**介绍:**
+
+启用 RON(Rusty Object Notation)格式的 serde 序列化/反序列化格式化支持。
+
+## 特性 `toml_serde_fmt`
+
+**介绍:**
+
+启用 TOML 格式的 serde 序列化/反序列化格式化支持。
+
+## 特性 `yaml_serde_fmt`
+
+**介绍:**
+
+启用 YAML 格式的 serde 序列化/反序列化格式化支持。
+
+<p align="center" style="font-size: 0.85em; color: gray;">
+ Written by @Weicao-CatilGrass
+</p>