Struct aopt::parser::FwdPolicy

source ·
pub struct FwdPolicy<Set, Ser, 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(ReturnVal) 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 = policy.default_set();
let mut inv = policy.default_inv();
let mut ser = policy.default_ser();
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: &mut ASet,
            _: &mut ASer,
            filter: ser::Value<Vec<&str>>,
            mut 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.take()))
    },
);

let args = Args::from(["app", "set", "42", "foo", "bar"]);

for opt in set.iter_mut() {
    opt.init()?;
}
ser.sve_insert(ser::Value::new(vec!["foo", "bar"]));
policy.parse(&mut set, &mut inv, &mut ser, ARef::new(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, &mut ser, ARef::new(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");

Implementations§

source§

impl<Set, Ser, Chk> FwdPolicy<Set, Ser, Chk>
where Chk: Default,

source

pub fn new(strict: bool, style: OptStyleManager) -> Self

source§

impl<Set, Ser, Chk> FwdPolicy<Set, Ser, Chk>

source

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.

source

pub fn with_styles(self, styles: Vec<UserStyle>) -> Self

source

pub fn with_checker(self, checker: Chk) -> Self

source

pub fn with_overload(self, overload: bool) -> Self

source

pub fn set_checker(&mut self, checker: Chk) -> &mut Self

source

pub fn checker(&self) -> &Chk

source

pub fn checker_mut(&mut self) -> &mut Chk

Trait Implementations§

source§

impl<Set, Ser, Chk> APolicyExt<FwdPolicy<Set, Ser, Chk>> for FwdPolicy<Set, Ser, Chk>
where Ser: Default, Chk: SetChecker<Set>, Set: Set + OptParser + OptValidator + Default,

source§

fn default_set(&self) -> Set

source§

fn default_ser(&self) -> Ser

source§

fn default_inv<'a>(&self) -> <FwdPolicy<Set, Ser, Chk> as Policy>::Inv<'a>

source§

impl<Set, Ser, Chk> Clone for FwdPolicy<Set, Ser, Chk>
where Chk: Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Set, Ser, Chk> Debug for FwdPolicy<Set, Ser, Chk>
where Chk: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Set, Ser, Chk> Default for FwdPolicy<Set, Ser, Chk>
where Chk: Default,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<Set, Ser, Chk> Policy for FwdPolicy<Set, Ser, Chk>
where SetOpt<Set>: Opt, Chk: SetChecker<Set>, Set: Set + OptParser + OptValidator,

§

type Ret = ReturnVal

§

type Set = Set

§

type Inv<'a> = Invoker<'a, Set, Ser>

§

type Ser = Ser

§

type Error = Error

source§

fn parse( &mut self, set: &mut Self::Set, inv: &mut Self::Inv<'_>, ser: &mut Self::Ser, args: ARef<Args> ) -> Result<Self::Ret, Self::Error>

source§

impl<Set, Ser, Chk> PolicySettings for FwdPolicy<Set, Ser, Chk>

source§

fn style_manager(&self) -> &OptStyleManager

source§

fn style_manager_mut(&mut self) -> &mut OptStyleManager

source§

fn strict(&self) -> bool

source§

fn styles(&self) -> &[UserStyle]

source§

fn no_delay(&self) -> Option<&[AStr]>

source§

fn overload(&self) -> bool

source§

fn set_strict(&mut self, strict: bool) -> &mut Self

source§

fn set_styles(&mut self, styles: Vec<UserStyle>) -> &mut Self

source§

fn set_no_delay(&mut self, _: impl Into<AStr>) -> &mut Self

source§

fn set_overload(&mut self, overload: bool) -> &mut Self

Auto Trait Implementations§

§

impl<Set, Ser, Chk> Freeze for FwdPolicy<Set, Ser, Chk>
where Chk: Freeze,

§

impl<Set, Ser, Chk> RefUnwindSafe for FwdPolicy<Set, Ser, Chk>
where Chk: RefUnwindSafe, Set: RefUnwindSafe, Ser: RefUnwindSafe,

§

impl<Set, Ser, Chk> Send for FwdPolicy<Set, Ser, Chk>
where Chk: Send, Set: Send, Ser: Send,

§

impl<Set, Ser, Chk> Sync for FwdPolicy<Set, Ser, Chk>
where Chk: Sync, Set: Sync, Ser: Sync,

§

impl<Set, Ser, Chk> Unpin for FwdPolicy<Set, Ser, Chk>
where Chk: Unpin, Set: Unpin, Ser: Unpin,

§

impl<Set, Ser, Chk> UnwindSafe for FwdPolicy<Set, Ser, Chk>
where Chk: UnwindSafe, Set: UnwindSafe, Ser: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedTy for T
where T: Any + 'static,