Struct Context

Source
pub struct Context { /* private fields */ }
Expand description

Provides configuration info for parsing a command.

§Example

use clapi::{Command, Argument, CommandOption, Context, Parser};
use clapi::validator::validate_type;

let command = Command::new("MyApp")
    .arg(Argument::one_or_more("values"))
    .option(CommandOption::new("enable")
        .alias("e")
        .requires_assign(true)
        .arg(Argument::new().validator(validate_type::<bool>())));

let context = Context::builder(command)
    .alias_prefix("/")      // An alias prefix
    .assign_operator(':')   // An assign operator for options: `--option:value`
    .build();

let result = Parser::new(&context)
    .parse(vec!["/e:false", "--", "hello", "hola"])
    .unwrap();

assert!(result.options().get_arg("enable").unwrap().contains("false"));
assert!(result.arg().unwrap().contains("hello"));
assert!(result.arg().unwrap().contains("hola"));

Implementations§

Source§

impl Context

Source

pub fn new(root: Command) -> Self

Constructs a default Context with the given command.

Source

pub fn builder(root: Command) -> ContextBuilder

Constructs a ContextBuilder with the given command.

Source

pub fn root(&self) -> &Command

Returns the Command used by this context.

Source

pub fn name_prefixes(&self) -> Prefixes<'_>

Returns an iterator over the option name prefixes of this context.

Source

pub fn alias_prefixes(&self) -> Prefixes<'_>

Returns an iterator over the option alias prefixes of this context.

Source

pub fn assign_operators(&self) -> impl ExactSizeIterator<Item = &char>

Returns an iterator over the assign operator chars.

Source

pub fn delimiter(&self) -> char

Returns the delimiter used in this context.

Source

pub fn suggestions(&self) -> Option<&SuggestionSource>

Returns the SuggestionProvider or None if not set.

Source

pub fn help(&self) -> &HelpSource

Returns the HelpSource of this context.

Source

pub fn help_option(&self) -> Option<&CommandOption>

Gets the help CommandOption of this context.

Source

pub fn help_command(&self) -> Option<&Command>

Gets the help Command of this context.

Source

pub fn version_option(&self) -> Option<&CommandOption>

Gets the version CommandOption of this context.

Source

pub fn version_command(&self) -> Option<&Command>

Gets the version Command of this context.

Source

pub fn set_suggestions(&mut self, suggestions: SuggestionSource)

Sets the SuggestionSource of this context.

Source

pub fn set_help(&mut self, help: HelpSource)

Sets the HelpSource of this context.

Source

pub fn set_help_option(&mut self, option: CommandOption)

Sets the help CommandOption of this context.

Source

pub fn set_help_command(&mut self, command: Command)

Sets the help Command of this context.

Source

pub fn set_version_option(&mut self, option: CommandOption)

Sets the version CommandOption of this context.

Source

pub fn set_version_command(&mut self, command: Command)

Sets the version Command of this context.

Source

pub fn get_option(&self, name_or_alias: &str) -> Option<&CommandOption>

Returns the CommandOption with the given name or alias or None if not found.

Source

pub fn get_command(&self, name: &str) -> Option<&Command>

Returns the Command with the given name or None if not found.

Source

pub fn is_name_prefix(&self, value: &str) -> bool

Returns true if the value is a name prefix.

Source

pub fn is_alias_prefix(&self, value: &str) -> bool

Returns true if the value is an alias prefix.

Source

pub fn trim_prefix<'a>(&self, option: &'a str) -> &'a str

Removes the prefix from the given option

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

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 Debug for Context

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Context

§

impl !RefUnwindSafe for Context

§

impl !Send for Context

§

impl !Sync for Context

§

impl Unpin for Context

§

impl !UnwindSafe for Context

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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.