aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-05-15 22:36:02 +0800
committer魏曹先生 <1992414357@qq.com>2026-05-15 22:36:57 +0800
commit31638f3cf6cf8266edc0c8b30f44407229f79637 (patch)
tree147c91d6c4ad36c6ff08d339a54501bd810c8936
parent6d61e0c46d33b917438386191e1e11351359abac (diff)
Add option to silence panic messages in stdout settings
-rw-r--r--CHANGELOG.md1
-rw-r--r--mingling_core/src/program.rs20
-rw-r--r--mingling_core/src/program/config.rs4
3 files changed, 25 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 89c0ba5..7fec970 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -79,6 +79,7 @@ this::<ThisProgram>().modify_res::<ExitCode>(|code| {
```
11. **\[core\]** Added panic catch for program execution.
+12. **\[core\]** Added the `stdout_setting.silence_panic` option, which is disabled by default. When enabled, `Program`'s `panic!` will not output to the console
#### **BREAKING CHANGES**:
diff --git a/mingling_core/src/program.rs b/mingling_core/src/program.rs
index e7e9ec0..a00b2c7 100644
--- a/mingling_core/src/program.rs
+++ b/mingling_core/src/program.rs
@@ -193,6 +193,16 @@ where
.unwrap()
.downcast_ref::<Program<C>>()
.unwrap();
+
+ #[cfg(not(panic = "abort"))]
+ if program.stdout_setting.silence_panic {
+ std::panic::set_hook(Box::new(|_| {}));
+ }
+
+ #[cfg(panic = "abort")]
+ return Ok(f(program));
+
+ #[cfg(not(panic = "abort"))]
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(program))) {
Ok(fut) => Ok(fut.await),
Err(panic_info) => {
@@ -294,6 +304,16 @@ where
.unwrap()
.downcast_ref::<Program<C>>()
.unwrap();
+
+ #[cfg(not(panic = "abort"))]
+ if program.stdout_setting.silence_panic {
+ std::panic::set_hook(Box::new(|_| {}));
+ }
+
+ #[cfg(panic = "abort")]
+ return Ok(f(program));
+
+ #[cfg(not(panic = "abort"))]
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(program))) {
Ok(result) => Ok(result),
Err(panic_info) => {
diff --git a/mingling_core/src/program/config.rs b/mingling_core/src/program/config.rs
index 35b9392..c3b1b0e 100644
--- a/mingling_core/src/program/config.rs
+++ b/mingling_core/src/program/config.rs
@@ -7,6 +7,9 @@ pub struct ProgramStdoutSetting {
/// Render results and output
pub render_output: bool,
+ /// Silence panic messages
+ pub silence_panic: bool,
+
#[cfg(feature = "clap")]
/// Behavior when Clap Dispatcher outputs help information
pub clap_help_print_behaviour: ClapHelpPrintBehaviour,
@@ -28,6 +31,7 @@ impl Default for ProgramStdoutSetting {
ProgramStdoutSetting {
error_output: true,
render_output: true,
+ silence_panic: false,
#[cfg(feature = "clap")]
clap_help_print_behaviour: ClapHelpPrintBehaviour::default(),
}