From 13408e79b940e9a33ca593ed30d1b20c54e01234 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Tue, 30 Jun 2026 18:05:05 +0800 Subject: 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 --- docs/pages/12-exit-code.md | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 docs/pages/12-exit-code.md (limited to 'docs/pages/12-exit-code.md') 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 @@ +

Exit Code Control

+

+ Managing program exit codes via the resource system +

+ +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); +#[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`, 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!(); +``` + +

+ Written by @Weicao-CatilGrass +

-- cgit