aboutsummaryrefslogtreecommitdiff
path: root/examples/example-exit-code/src/main.rs
diff options
context:
space:
mode:
authorWeicao-CatilGrass <1992414357@qq.com>2026-05-13 07:28:02 +0800
committerWeicao-CatilGrass <1992414357@qq.com>2026-05-13 07:28:02 +0800
commit95c6b979ca399671eed8bf9c72f53cfe5d46f431 (patch)
tree8225b39bee4ad3b7ebc8a36ab978d1329f667eb8 /examples/example-exit-code/src/main.rs
parentfef888b75b2544765aa06808c14490a2af827313 (diff)
Migrate exit code control to resource-based system
Diffstat (limited to 'examples/example-exit-code/src/main.rs')
-rw-r--r--examples/example-exit-code/src/main.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/example-exit-code/src/main.rs b/examples/example-exit-code/src/main.rs
new file mode 100644
index 0000000..f138fb3
--- /dev/null
+++ b/examples/example-exit-code/src/main.rs
@@ -0,0 +1,42 @@
+//! `Mingling` Example - Exit Code
+//!
+//! This example demonstrates how to modify the program's exit code using `ExitCodeSetup`.
+//! By default, the program exits with code 0. This example shows:
+//! 1. Using `dispatcher!` to define an error command,
+//! 2. Using `chain!` to handle errors and set a custom exit code via `ProgramExitCode`,
+//! 3. Using `renderer!` to print an error message.
+//!
+//! # How to Run
+//! ```bash
+//! cargo run --manifest-path ./examples/example-exit-code/Cargo.toml -- error
+//! ```
+
+use mingling::{
+ macros::{chain, dispatcher, gen_program, pack, r_println, renderer},
+ res::ExitCode,
+ setup::ExitCodeSetup,
+ this,
+};
+
+fn main() {
+ let mut program = ThisProgram::new();
+ program.with_dispatcher(ErrorCommand);
+ program.with_setup(ExitCodeSetup::<ThisProgram>::default());
+ program.exec();
+}
+
+dispatcher!("error", ErrorCommand => ErrorEntry);
+pack!(ResultError = ());
+
+#[chain]
+fn handle_error_entry(_prev: ErrorEntry) -> NextProcess {
+ this::<ThisProgram>().modify_res(|r: &mut ExitCode| r.exit_code = 1);
+ return ResultError::default();
+}
+
+#[renderer]
+fn render_error(_prev: ResultError) {
+ r_println!("Error!");
+}
+
+gen_program!();