pub struct FwdPolicy<S, Chk> { /* private fields */ }Expand description
FwdPolicy matching the command line arguments with Opt in the Set.
The option would match failed if any special Error raised during option processing.
FwdPolicy will return Some(Return) if match successful.
FwdPolicy process the option before any
NOA(Cmd, Pos and Main).
During parsing, you can get the value of any option in the handler of NOA.
§Examples
let mut policy = AFwdPolicy::default();
let mut set = AHCSet::default();
let mut inv = AInvoker::default();
let filter_id = set.add_opt("--/filter=b")?.run()?;
let pos_id = set.add_opt("pos=p@*")?
.set_pos_type::<String>()
.set_values(vec![])
.run()?;
inv.entry(pos_id).on(
move |set, ctx| {
let filter = set.app_data::<Vec<&str>>()?;
let value = ctx.value::<String>()?;
let not_filter = set[filter_id].val::<bool>()?;
let valid = if !*not_filter {
!filter.iter().any(|&v| v == value.as_str())
} else {
true
};
Ok(valid.then(|| value))
},
);
let args = Args::from(["app", "set", "42", "foo", "bar"]);
for opt in set.iter_mut() {
opt.init()?;
}
set.set_app_data(vec!["foo", "bar"]);
policy.parse(&mut set, &mut inv, args)?;
let values = set[pos_id].vals::<String>()?;
assert_eq!(values[0], "set");
assert_eq!(values[1], "42");
let args = Args::from(["app", "--/filter", "set", "42", "foo", "bar"]);
for opt in set.iter_mut() {
opt.init()?;
}
policy.parse(&mut set, &mut inv, args)?;
let values = set[pos_id].vals::<String>()?;
assert_eq!(values[0], "set");
assert_eq!(values[1], "42");
assert_eq!(values[2], "foo");
assert_eq!(values[3], "bar");When prepolicy is enabled,
FwdPolicy will skip any special Error during parse process.
§Example
let mut parser = AFwdParser::default();
let mut cfg_loader = AFwdParser::default();
cfg_loader.set_prepolicy(true);
parser
.add_opt("-check=s")?
.on(|set, ctx| {
let ext = ctx.value::<String>()?;
let mut found = false;
for name in ["-c", "-cxx"] {
if let Ok(opt) = set.find(name) {
if let Ok(file) = opt.vals::<String>() {
if file.contains(&ext) {
found = true;
}
}
}
}
Ok(Some(found))
})?;
cfg_loader.set_app_data(parser);
cfg_loader.add_opt("--load=s")?.on(
|set, ctx| {
let cfg = ctx.value::<String>()?;
let parser = set.app_data_mut::<AFwdParser>()?;
match cfg.as_str() {
"cxx" => {
parser.add_opt("-cxx".infer::<String>())?.set_values(
["cxx", "cpp", "c++", "cc", "hpp", "hxx", "h"]
.map(|v| v.to_owned())
.to_vec(),
);
}
"c" => {
parser
.add_opt("-c=s")?
.set_values_t(["c", "h"].map(|v| v.to_owned()).to_vec());
}
_ => {
panic!("Unknow configuration name")
}
}
Ok(Some(cfg))
},
)?;
let ret = getopt!(
Args::from(["--load", "cxx", "-check", "cc"]),
&mut cfg_loader
)?;
let next_args = ret.ret.clone_args();
let mut parser = cfg_loader.take_app_data::<AFwdParser>()?;
getopt!(Args::from(next_args), &mut parser)?;
assert!(*parser.find_val::<bool>("-check")?);
// pass the parser to AppService
cfg_loader.set_app_data(parser);
let ret = getopt!(
Args::from(["--load", "c", "-check", "c"]),
&mut cfg_loader
)?;
let next_args = ret.ret.clone_args();
let mut parser = cfg_loader.service_mut().take_app_data::<AFwdParser>()?;
getopt!(Args::from(next_args), &mut parser)?;
assert!(*parser.find_val::<bool>("-check")?);Implementations§
Source§impl<S, Chk> FwdPolicy<S, Chk>where
Chk: Default,
impl<S, Chk> FwdPolicy<S, Chk>where
Chk: Default,
pub fn new(strict: bool, style: OptStyleManager) -> Self
Source§impl<S, Chk> FwdPolicy<S, Chk>
impl<S, Chk> FwdPolicy<S, Chk>
Sourcepub fn with_strict(self, strict: bool) -> Self
pub fn with_strict(self, strict: bool) -> Self
In strict mode, if an argument looks like an option (it matched any option prefix), then it must matched.
pub fn with_styles(self, styles: Vec<UserStyle>) -> Self
pub fn with_checker(self, checker: Chk) -> Self
pub fn with_overload(self, overload: bool) -> Self
pub fn with_prepolicy(self, prepolicy: bool) -> Self
pub fn set_checker(&mut self, checker: Chk) -> &mut Self
pub fn checker(&self) -> &Chk
pub fn checker_mut(&mut self) -> &mut Chk
Trait Implementations§
Source§impl<Set, Chk> PolicySettings for FwdPolicy<Set, Chk>
impl<Set, Chk> PolicySettings for FwdPolicy<Set, Chk>
fn style_manager(&self) -> &OptStyleManager
fn style_manager_mut(&mut self) -> &mut OptStyleManager
fn strict(&self) -> bool
fn styles(&self) -> &[UserStyle]
fn no_delay(&self) -> Option<&[String]>
fn overload(&self) -> bool
fn prepolicy(&self) -> bool
fn set_strict(&mut self, strict: bool) -> &mut Self
fn set_styles(&mut self, styles: Vec<UserStyle>) -> &mut Self
fn set_no_delay(&mut self, _: impl Into<String>) -> &mut Self
fn set_overload(&mut self, overload: bool) -> &mut Self
fn set_prepolicy(&mut self, prepolicy: bool) -> &mut Self
Auto Trait Implementations§
impl<S, Chk> Freeze for FwdPolicy<S, Chk>where
Chk: Freeze,
impl<S, Chk> RefUnwindSafe for FwdPolicy<S, Chk>where
Chk: RefUnwindSafe,
S: RefUnwindSafe,
impl<S, Chk> Send for FwdPolicy<S, Chk>
impl<S, Chk> Sync for FwdPolicy<S, Chk>
impl<S, Chk> Unpin for FwdPolicy<S, Chk>
impl<S, Chk> UnsafeUnpin for FwdPolicy<S, Chk>where
Chk: UnsafeUnpin,
impl<S, Chk> UnwindSafe for FwdPolicy<S, Chk>where
Chk: UnwindSafe,
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more