aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md8
-rw-r--r--examples/example-exit-code/src/main.rs5
-rw-r--r--mingling/src/example_docs.rs5
-rw-r--r--mingling/src/res/exit_code.rs24
4 files changed, 31 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 96db9ee..9f2c07e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,7 +11,6 @@ None
#### Features:
1. **\[macros\]** The `gen_program!()` macro now generates `pub fn this() -> &'static Program<#name>` for the generated program type, providing convenient static accessors.
-
2. **\[macros\]** The `#[chain]` macro now supports resource injection parameters (2nd to Nth). When you write:
```rust
@@ -47,11 +46,8 @@ if mingling::feature::MINGLING_ASYNC {
```
5. **\[core\]** Added `with_hook` functions to embed callback events into the program lifecycle
-
6. **\[core\]** Added `user_context.run_hook` configuration item to control whether the program runs hooks
-
7. **\[core\]** Added `exec_and_exit`, which will return an `i32` exit code after the program ends
-
8. **\[core\]** Added `ExitCodeSetup`, you can control the program's exit code by modifying the `mingling::res::ExitCode` resource
```rust
@@ -59,6 +55,9 @@ if mingling::feature::MINGLING_ASYNC {
fn your_chain(_prev: Prev) -> NextProcess {
// Use `modify_res` to modify the value of `ExitCode`
this::<ThisProgram>().modify_res(|r: &mut ExitCode| r.exit_code = 1);
+
+ // Or use:
+ mingling::res::update_exit_code::<ThisProgram>(1);
// ...
}
```
@@ -102,7 +101,6 @@ struct YourCommandEntry {
```
3. **\[clap\]** Added the `stdout_setting.clap_help_print_behaviour` configuration item to `Program`, used to control the behavior of Clap Help
-
4. **\[core\]** Added function `new_with_args` to `Program`
5. **\[core\]** Added function `dispatch_args_dynamic` to `Program`
6. **\[core\]** Impl `std::io::Write` trait for `RenderResult`
diff --git a/examples/example-exit-code/src/main.rs b/examples/example-exit-code/src/main.rs
index f138fb3..f62ad22 100644
--- a/examples/example-exit-code/src/main.rs
+++ b/examples/example-exit-code/src/main.rs
@@ -13,9 +13,8 @@
use mingling::{
macros::{chain, dispatcher, gen_program, pack, r_println, renderer},
- res::ExitCode,
+ res::update_exit_code,
setup::ExitCodeSetup,
- this,
};
fn main() {
@@ -30,7 +29,7 @@ pack!(ResultError = ());
#[chain]
fn handle_error_entry(_prev: ErrorEntry) -> NextProcess {
- this::<ThisProgram>().modify_res(|r: &mut ExitCode| r.exit_code = 1);
+ update_exit_code::<ThisProgram>(1);
return ResultError::default();
}
diff --git a/mingling/src/example_docs.rs b/mingling/src/example_docs.rs
index 09e84a8..1669344 100644
--- a/mingling/src/example_docs.rs
+++ b/mingling/src/example_docs.rs
@@ -371,9 +371,8 @@ pub mod example_dispatch_tree {}
/// ```ignore
/// use mingling::{
/// macros::{chain, dispatcher, gen_program, pack, r_println, renderer},
-/// res::ExitCode,
+/// res::update_exit_code,
/// setup::ExitCodeSetup,
-/// this,
/// };
///
/// fn main() {
@@ -388,7 +387,7 @@ pub mod example_dispatch_tree {}
///
/// #[chain]
/// fn handle_error_entry(_prev: ErrorEntry) -> NextProcess {
-/// this::<ThisProgram>().modify_res(|r: &mut ExitCode| r.exit_code = 1);
+/// update_exit_code::<ThisProgram>(1);
/// return ResultError::default();
/// }
///
diff --git a/mingling/src/res/exit_code.rs b/mingling/src/res/exit_code.rs
index 388a6f1..f4d4238 100644
--- a/mingling/src/res/exit_code.rs
+++ b/mingling/src/res/exit_code.rs
@@ -1,4 +1,28 @@
+use mingling_core::{ProgramCollect, this};
+
+/// Represents a program exit code.
#[derive(Debug, Default, Clone, Copy)]
pub struct ExitCode {
+ /// The numeric exit code value.
pub exit_code: i32,
}
+
+/// Updates the globally stored exit code for the given `ProgramCollect` type.
+pub fn update_exit_code<C>(exit_code: i32)
+where
+ C: ProgramCollect<Enum = C> + 'static,
+{
+ this::<C>().modify_res(|e: &mut ExitCode| e.exit_code = exit_code);
+}
+
+/// Retrieves the globally stored exit code for the given `ProgramCollect` type.
+/// Returns `0` if no exit code has been set.
+pub fn exit_code<C>() -> i32
+where
+ C: ProgramCollect<Enum = C> + 'static,
+{
+ match this::<C>().res::<ExitCode>() {
+ Some(e) => e.exit_code,
+ None => 0,
+ }
+}