pub struct CommandOption { /* private fields */ }Expand description
Represents a command-line option.
Implementations§
Source§impl CommandOption
impl CommandOption
Sourcepub fn get_aliases(&self) -> Aliases<'_> ⓘ
pub fn get_aliases(&self) -> Aliases<'_> ⓘ
Returns an Iterator over the aliases of this option.
Sourcepub fn get_description(&self) -> Option<&str>
pub fn get_description(&self) -> Option<&str>
Returns a short description of this option or None if not set.
Sourcepub fn is_required(&self) -> bool
pub fn is_required(&self) -> bool
Returns true if this option is required.
Returns true if this option is no visible for help.
Sourcepub fn allow_multiple(&self) -> bool
pub fn allow_multiple(&self) -> bool
Returns true if this option is allowed to appear multiple times.
Sourcepub fn is_assign_required(&self) -> bool
pub fn is_assign_required(&self) -> bool
Returns true if the option requires an assign operator.
Sourcepub fn get_arg(&self) -> Option<&Argument>
pub fn get_arg(&self) -> Option<&Argument>
Returns the Argument this option takes or None if have more than 1 argument.
Sourcepub fn get_args(&self) -> &ArgumentList
pub fn get_args(&self) -> &ArgumentList
Returns the Arguments of this option.
Sourcepub fn has_alias<S: AsRef<str>>(&self, alias: S) -> bool
pub fn has_alias<S: AsRef<str>>(&self, alias: S) -> bool
Returns true if option contains the specified alias.
Sourcepub fn description<S: Into<String>>(self, description: S) -> Self
pub fn description<S: Into<String>>(self, description: S) -> Self
Sets a short description of this option.
§Example
use clapi::CommandOption;
let option = CommandOption::new("test")
.description("Enable tests");
assert_eq!(option.get_description(), Some("Enable tests"));Sourcepub fn required(self, is_required: bool) -> Self
pub fn required(self, is_required: bool) -> Self
Specify if this option is required, by default is false.
§Examples
use clapi::{Command, CommandOption, Argument};
use clapi::validator::validate_type;
let result = Command::new("MyApp")
.option(CommandOption::new("test"))
.option(CommandOption::new("number")
.required(true)
.arg(Argument::new()
.validator(validate_type::<i64>())))
.parse_from(vec!["--test", "--number", "10"])
.unwrap();
assert!(result.options().get_arg("number").unwrap().contains("10"));
assert!(result.options().contains("test"));Other example where the option is ommited
use clapi::{Command, CommandOption, Argument};
use clapi::validator::validate_type;
let result = Command::new("MyApp")
.option(CommandOption::new("test"))
.option(CommandOption::new("number")
.required(true)
.arg(Argument::new()
.validator(validate_type::<i64>())))
.parse_from(vec!["--test"]);
assert!(result.is_err());Specify if this option is hidden for the help.
§Example
use clapi::CommandOption;
let option = CommandOption::new("enable").hidden(true);
assert!(option.is_hidden());Sourcepub fn multiple(self, allow_multiple: bool) -> Self
pub fn multiple(self, allow_multiple: bool) -> Self
Specify if this option can appear multiple times.
§Example
use clapi::{Command, CommandOption, Argument};
let result = Command::new("MyApp")
.option(CommandOption::new("numbers")
.multiple(true)
.arg(Argument::new().min_values(1)))
.parse_from(vec!["--numbers", "10", "--numbers", "20", "--numbers", "30"])
.unwrap();
assert!(result.options().get_arg("numbers").unwrap().contains("10"));
assert!(result.options().get_arg("numbers").unwrap().contains("20"));
assert!(result.options().get_arg("numbers").unwrap().contains("30"));Sourcepub fn requires_assign(self, requires_assign: bool) -> Self
pub fn requires_assign(self, requires_assign: bool) -> Self
Specify if this option requires an assign operator.
§Example
use clapi::{Command, CommandOption, Argument};
let result = Command::new("MyApp")
.option(CommandOption::new("numbers")
.requires_assign(true)
.arg(Argument::new().min_values(1)))
.parse_from(vec!["--numbers=10,20,30"])
.unwrap();
assert!(result.options().get_arg("numbers").unwrap().contains("10"));
assert!(result.options().get_arg("numbers").unwrap().contains("20"));
assert!(result.options().get_arg("numbers").unwrap().contains("30"));Using it like this will fail
use clapi::{Command, CommandOption, Argument};
let result = Command::new("MyApp")
.option(CommandOption::new("numbers")
.requires_assign(true)
.arg(Argument::new().min_values(1)))
.parse_from(vec!["--numbers", "10", "20", "30"]);
assert!(result.is_err());Sourcepub fn arg(self, arg: Argument) -> Self
pub fn arg(self, arg: Argument) -> Self
Adds a new Argument to this option.
§Example
use clapi::{Command, CommandOption, Argument};
let result = Command::new("MyApp")
.option(CommandOption::new("copy")
.arg(Argument::with_name("from"))
.arg(Argument::with_name("to")))
.parse_from(vec!["--copy", "/src/file.txt", "/src/utils/"])
.unwrap();
assert!(result.options().get_args("copy").unwrap().get("from").unwrap().contains("/src/file.txt"));
assert!(result.options().get_args("copy").unwrap().get("to").unwrap().contains("/src/utils/"));Sourcepub fn args(self, args: ArgumentList) -> Self
pub fn args(self, args: ArgumentList) -> Self
Sets the arguments of this option.
§Example
use clapi::{ArgumentList, Argument, CommandOption};
let mut args = ArgumentList::new();
args.add(Argument::with_name("from")).unwrap();
args.add(Argument::with_name("to")).unwrap();
let option = CommandOption::new("copy").args(args);
assert!(option.get_args().contains("from"));
assert!(option.get_args().contains("to"));Trait Implementations§
Source§impl Clone for CommandOption
impl Clone for CommandOption
Source§fn clone(&self) -> CommandOption
fn clone(&self) -> CommandOption
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more