aboutsummaryrefslogtreecommitdiff
path: root/docs/pages/12-exit-code.md
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-06-30 18:05:05 +0800
committer魏曹先生 <1992414357@qq.com>2026-06-30 18:05:05 +0800
commit13408e79b940e9a33ca593ed30d1b20c54e01234 (patch)
tree282549991a3f31791401ca2f3255b9318679d2e9 /docs/pages/12-exit-code.md
parent29867ab5c0b40378a33318d989c809f90fc7d3aa (diff)
feat(docs): add Chinese and English documentation for Mingling tutorials
Add comprehensive documentation covering Declare a Dispatcher, Declare a Chain, Rendering Results, Multi-Command Program, Argument Parsing with Picker and Clap, Program Setup, Error Handling, Help Info, Resource System, Exit Code Control, Hook System, Testing, Completion, Structural Rendering, and Core Concepts
Diffstat (limited to 'docs/pages/12-exit-code.md')
-rw-r--r--docs/pages/12-exit-code.md68
1 files changed, 68 insertions, 0 deletions
diff --git a/docs/pages/12-exit-code.md b/docs/pages/12-exit-code.md
new file mode 100644
index 0000000..6828cde
--- /dev/null
+++ b/docs/pages/12-exit-code.md
@@ -0,0 +1,68 @@
+<h1 align="center">Exit Code Control</h1>
+<p align="center">
+ Managing program exit codes via the resource system
+</p>
+
+Providing the shell with a correct exit code when a program terminates is a basic CLI convention. Mingling offers a ready-to-use `ExitCodeSetup` that, together with the `ResExitCode` resource, makes exit code control incredibly simple.
+
+## Enabling ExitCodeSetup
+
+```rust
+@@@use mingling::prelude::*;
+@@@use mingling::setup::ExitCodeSetup;
+fn main() {
+ let mut program = ThisProgram::new();
+ program.with_setup(ExitCodeSetup::default());
+@@@ program.exec_and_exit();
+}
+```
+
+`ExitCodeSetup` does two things:
+
+1. Registers the `ResExitCode` resource (default value `0`)
+2. Registers a `finish` hook that reads the value of `ResExitCode` as the final exit code before the program exits
+
+## Modifying the Exit Code
+
+In a Chain or Renderer, inject `ResExitCode` to modify the exit code:
+
+```rust
+@@@use mingling::res::ResExitCode;
+@@@use mingling::setup::ExitCodeSetup;
+@@@pack!(EntryCheck = Vec<String>);
+#[chain]
+fn handle_check(_args: EntryCheck, ec: &mut ResExitCode) {
+ // Modify exit code when check fails
+ ec.exit_code = 1;
+}
+```
+
+> [!TIP]
+> `ResExitCode` is simply `struct ResExitCode { pub exit_code: i32 }`. Inject `&mut ResExitCode` and modify the field directly.
+
+## Three Execution Modes of `Program`
+
+`Program` provides three execution modes (excluding `exec_repl` under the `repl` feature):
+
+| Mode | Behavior |
+| ------------------------------- | ----------------------------------------------------------------------------------------- |
+| `program.exec_and_exit()` | Executes and terminates the process directly with the exit code |
+| `program.exec()` | Executes and returns an `i32` exit code, letting the caller decide handling |
+| `program.exec_without_render()` | Returns `Result<RenderResult, ProgramExecuteError>`, with internal `exit_code` accessible |
+
+```rust
+@@@use mingling::setup::ExitCodeSetup;
+fn main() {
+ let mut program = ThisProgram::new();
+ program.with_setup(ExitCodeSetup::default());
+
+ // Get exit code and handle it yourself
+ let exit_code = program.exec();
+ std::process::exit(exit_code);
+}
+@@@gen_program!();
+```
+
+<p align="center" style="font-size: 0.85em; color: gray;">
+ Written by @Weicao-CatilGrass
+</p>