Struct aopt::parser::PrePolicy

source ·
pub struct PrePolicy<Set, Ser, Chk> { /* private fields */ }
Expand description

PrePolicy matching the command line arguments with Opt in the Set. PrePolicy will skip any special Error during parse process. PrePolicy will return Some(ReturnVal) if match successful. PrePolicy doesn’t consume the NOA when process guess_and_invoke.

§Example

let mut parser = AFwdParser::default();
let mut cfg_loader = APreParser::default();

parser
    .add_opt("-check=s")?
    .on(|set: &mut ASet, _: &mut ASer, 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.deref()) {
                        found = true;
                    }
                }
            }
        }
        Ok(Some(found))
    })?;
cfg_loader.set_app_data(parser)?;
cfg_loader.add_opt("--load=s")?.on(
    |_: &mut ASet, ser: &mut ASer, mut cfg: ctx::Value<String>| {
        let parser = ser.sve_val_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.take()))
    },
)?;

let ret = getopt!(
    Args::from(["--load", "cxx", "-check", "cc"]),
    &mut cfg_loader
)?;
let next_args = ret.ret.clone_args();
let mut parser = cfg_loader.service_mut().sve_take_val::<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().sve_take_val::<AFwdParser>()?;

getopt!(Args::from(next_args), &mut parser)?;

assert!(*parser.find_val::<bool>("-check")?);

Implementations§

source§

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

source

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

source§

impl<Set, Ser, Chk> PrePolicy<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, otherwise it will be discarded.

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

source

pub fn ig_failure<T: Default>(res: Result<T, Error>) -> Result<Option<T>, Error>

Ignore failure when parsing.

Trait Implementations§

source§

impl<Set, Ser, Chk> APolicyExt<PrePolicy<Set, Ser, Chk>> for PrePolicy<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) -> <PrePolicy<Set, Ser, Chk> as Policy>::Inv<'a>

source§

impl<Set, Ser, Chk> Clone for PrePolicy<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 PrePolicy<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 PrePolicy<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 PrePolicy<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 PrePolicy<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 PrePolicy<Set, Ser, Chk>
where Chk: Freeze,

§

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

§

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

§

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

§

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

§

impl<Set, Ser, Chk> UnwindSafe for PrePolicy<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,