aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-06-01 13:02:50 +0800
committer魏曹先生 <1992414357@qq.com>2026-06-01 13:02:50 +0800
commitb796a3e18ef360f88da138e176734fbfb5efcbd0 (patch)
treef6793a7e197848a4681a519b0a0059959d6de1e1
parent1ab19e8765ea2ed62c8a41113d84867facaabf08 (diff)
Refactor built-in flags into reusable setup structs
-rw-r--r--CHANGELOG.md11
-rw-r--r--mingling/src/setups/basic.rs103
2 files changed, 108 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 61805aa..e7b9257 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,17 @@ This macro wraps `::mingling::test::unpack_chain_process_result` to downcast a `
let result = some_chain_function(args).into();
let value: MyType = unpack_chain_process!(result, MyType);
```
+2. **\[core\]** Refactored the built-in flag system in `BasicProgramSetup` into individual, reusable setup structs (`HelpFlagSetup`, `QuietFlagSetup`, `ConfirmFlagSetup`). These setups are now separate implementations of `ProgramSetup`, each with customizable flag aliases and `Default` implementations. `BasicProgramSetup` now composes them via `with_setup` instead of defining flags inline.
+
+```rust
+// Customize individual flags
+program.with_setup(HelpFlagSetup::new(["-h", "--help"]));
+program.with_setup(QuietFlagSetup::new(["-q", "--quiet"]));
+program.with_setup(ConfirmFlagSetup::new(["-C", "--confirm"]));
+
+// Or use defaults via BasicProgramSetup
+program.with_setup(BasicProgramSetup);
+```
#### **BREAKING CHANGES** (API CHANGES):
diff --git a/mingling/src/setups/basic.rs b/mingling/src/setups/basic.rs
index 3783df0..6a69733 100644
--- a/mingling/src/setups/basic.rs
+++ b/mingling/src/setups/basic.rs
@@ -1,4 +1,4 @@
-use mingling_core::{Program, ProgramCollect, setup::ProgramSetup};
+use mingling_core::{Flag, Program, ProgramCollect, setup::ProgramSetup};
/// Performs basic program initialization:
///
@@ -12,17 +12,108 @@ where
C: ProgramCollect<Enum = C>,
{
fn setup(self, program: &mut Program<C>) {
- program.global_flag(["--quiet", "-q"], |p| {
+ program.with_setup(HelpFlagSetup::new(["-h", "--help"]));
+ program.with_setup(QuietFlagSetup::new(["-q", "--quiet"]));
+ program.with_setup(ConfirmFlagSetup::new(["-C", "--confirm"]));
+ }
+}
+
+/// Provides setup for parsing the user help flag
+///
+/// The default value is `--help / -h`
+pub struct HelpFlagSetup {
+ flag: Flag,
+}
+
+impl HelpFlagSetup {
+ /// Creates a new `HelpFlagSetup` with the given flag aliases.
+ pub fn new(flag: impl Into<Flag>) -> Self {
+ Self { flag: flag.into() }
+ }
+}
+
+impl<C> ProgramSetup<C> for HelpFlagSetup
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ program.global_flag(self.flag.clone(), |p| {
+ p.user_context.help = true;
+ });
+ }
+}
+
+impl Default for HelpFlagSetup {
+ fn default() -> Self {
+ Self {
+ flag: ["-h", "--help"].into(),
+ }
+ }
+}
+
+/// Provides setup for parsing the quiet flag
+///
+/// The default value is `--quiet / -q`
+pub struct QuietFlagSetup {
+ flag: Flag,
+}
+
+impl QuietFlagSetup {
+ /// Creates a new `QuietFlagSetup` with the given flag aliases.
+ pub fn new(flag: impl Into<Flag>) -> Self {
+ Self { flag: flag.into() }
+ }
+}
+
+impl<C> ProgramSetup<C> for QuietFlagSetup
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ program.global_flag(self.flag.clone(), |p| {
p.stdout_setting.render_output = false;
p.stdout_setting.error_output = false;
});
+ }
+}
- program.global_flag(["--help", "-h"], |p| {
- p.user_context.help = true;
- });
+impl Default for QuietFlagSetup {
+ fn default() -> Self {
+ Self {
+ flag: ["-q", "--quiet"].into(),
+ }
+ }
+}
- program.global_flag(["--confirm", "-C"], |p| {
+/// Provides setup for parsing the confirm flag
+///
+/// The default value is `--confirm / -C`
+pub struct ConfirmFlagSetup {
+ flag: Flag,
+}
+
+impl ConfirmFlagSetup {
+ /// Creates a new `ConfirmFlagSetup` with the given flag aliases.
+ pub fn new(flag: impl Into<Flag>) -> Self {
+ Self { flag: flag.into() }
+ }
+}
+
+impl<C> ProgramSetup<C> for ConfirmFlagSetup
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ program.global_flag(self.flag.clone(), |p| {
p.user_context.confirm = true;
});
}
}
+
+impl Default for ConfirmFlagSetup {
+ fn default() -> Self {
+ Self {
+ flag: ["-C", "--confirm"].into(),
+ }
+ }
+}