summaryrefslogtreecommitdiff
path: root/mingling/src/program/flag.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-03-29 00:52:16 +0800
committer魏曹先生 <1992414357@qq.com>2026-03-29 00:52:28 +0800
commitdb9afa0b06355028eafe3bc29fe0b2429ba8fd0a (patch)
tree60bf74d0853fcc0c1e9363813c26109a6ca38a4f /mingling/src/program/flag.rs
parent7ce68cd11516bd7cf037ecea99a92aee7c31b2c3 (diff)
Completed the first preliminary usable version of the Mingling
framework.
Diffstat (limited to 'mingling/src/program/flag.rs')
-rw-r--r--mingling/src/program/flag.rs41
1 files changed, 37 insertions, 4 deletions
diff --git a/mingling/src/program/flag.rs b/mingling/src/program/flag.rs
index 0178ebe..8372517 100644
--- a/mingling/src/program/flag.rs
+++ b/mingling/src/program/flag.rs
@@ -1,4 +1,4 @@
-use crate::Program;
+use crate::{Program, ProgramCollect};
pub struct Flag {
vec: Vec<&'static str>,
@@ -80,11 +80,14 @@ macro_rules! special_argument {
}};
}
-impl Program {
+impl<C> Program<C>
+where
+ C: ProgramCollect,
+{
/// Registers a global argument (with value) and its handler.
pub fn global_argument<F, A>(&mut self, arguments: A, do_fn: F)
where
- F: Fn(&mut Program, String),
+ F: Fn(&mut Program<C>, String),
A: Into<Flag>,
{
let flag = arguments.into();
@@ -100,7 +103,7 @@ impl Program {
/// Registers a global flag (boolean) and its handler.
pub fn global_flag<F, A>(&mut self, flag: A, do_fn: F)
where
- F: Fn(&mut Program),
+ F: Fn(&mut Program<C>),
A: Into<Flag>,
{
let flag = flag.into();
@@ -112,4 +115,34 @@ impl Program {
}
}
}
+
+ /// Extracts a global argument (with value) from arguments
+ pub fn pick_global_argument<F>(&mut self, flag: F) -> Option<String>
+ where
+ F: Into<Flag>,
+ {
+ let flag: Flag = flag.into();
+ for argument in flag.iter() {
+ let value = special_argument!(self.args, argument);
+ if value.is_some() {
+ return value;
+ }
+ }
+ None
+ }
+
+ /// Extracts global flags from arguments
+ pub fn pick_global_flag<F>(&mut self, flag: F) -> bool
+ where
+ F: Into<Flag>,
+ {
+ let flag: Flag = flag.into();
+ for argument in flag.iter() {
+ let enabled = special_flag!(self.args, argument);
+ if enabled {
+ return enabled;
+ }
+ }
+ return false;
+ }
}