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
impl Context
Sourcepub fn builder(root: Command) -> ContextBuilder
pub fn builder(root: Command) -> ContextBuilder
Constructs a ContextBuilder
with the given command.
Sourcepub fn name_prefixes(&self) -> Prefixes<'_> ⓘ
pub fn name_prefixes(&self) -> Prefixes<'_> ⓘ
Returns an iterator over the option name prefixes of this context.
Sourcepub fn alias_prefixes(&self) -> Prefixes<'_> ⓘ
pub fn alias_prefixes(&self) -> Prefixes<'_> ⓘ
Returns an iterator over the option alias prefixes of this context.
Sourcepub fn assign_operators(&self) -> impl ExactSizeIterator<Item = &char>
pub fn assign_operators(&self) -> impl ExactSizeIterator<Item = &char>
Returns an iterator over the assign operator char
s.
Sourcepub fn suggestions(&self) -> Option<&SuggestionSource>
pub fn suggestions(&self) -> Option<&SuggestionSource>
Returns the SuggestionProvider
or None
if not set.
Sourcepub fn help(&self) -> &HelpSource
pub fn help(&self) -> &HelpSource
Returns the HelpSource
of this context.
Sourcepub fn help_option(&self) -> Option<&CommandOption>
pub fn help_option(&self) -> Option<&CommandOption>
Gets the help CommandOption
of this context.
Sourcepub fn help_command(&self) -> Option<&Command>
pub fn help_command(&self) -> Option<&Command>
Gets the help Command
of this context.
Sourcepub fn version_option(&self) -> Option<&CommandOption>
pub fn version_option(&self) -> Option<&CommandOption>
Gets the version CommandOption
of this context.
Sourcepub fn version_command(&self) -> Option<&Command>
pub fn version_command(&self) -> Option<&Command>
Gets the version Command
of this context.
Sourcepub fn set_suggestions(&mut self, suggestions: SuggestionSource)
pub fn set_suggestions(&mut self, suggestions: SuggestionSource)
Sets the SuggestionSource
of this context.
Sourcepub fn set_help(&mut self, help: HelpSource)
pub fn set_help(&mut self, help: HelpSource)
Sets the HelpSource
of this context.
Sourcepub fn set_help_option(&mut self, option: CommandOption)
pub fn set_help_option(&mut self, option: CommandOption)
Sets the help CommandOption
of this context.
Sourcepub fn set_help_command(&mut self, command: Command)
pub fn set_help_command(&mut self, command: Command)
Sets the help Command
of this context.
Sourcepub fn set_version_option(&mut self, option: CommandOption)
pub fn set_version_option(&mut self, option: CommandOption)
Sets the version CommandOption
of this context.
Sourcepub fn set_version_command(&mut self, command: Command)
pub fn set_version_command(&mut self, command: Command)
Sets the version Command
of this context.
Sourcepub fn get_option(&self, name_or_alias: &str) -> Option<&CommandOption>
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.
Sourcepub fn get_command(&self, name: &str) -> Option<&Command>
pub fn get_command(&self, name: &str) -> Option<&Command>
Returns the Command
with the given name or None
if not found.
Sourcepub fn is_name_prefix(&self, value: &str) -> bool
pub fn is_name_prefix(&self, value: &str) -> bool
Returns true
if the value is a name prefix.
Sourcepub fn is_alias_prefix(&self, value: &str) -> bool
pub fn is_alias_prefix(&self, value: &str) -> bool
Returns true
if the value is an alias prefix.
Sourcepub fn trim_prefix<'a>(&self, option: &'a str) -> &'a str
pub fn trim_prefix<'a>(&self, option: &'a str) -> &'a str
Removes the prefix from the given option