Struct CommandOption

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

Represents a command-line option.

Implementations§

Source§

impl CommandOption

Source

pub fn new<S: Into<String>>(name: S) -> Self

Constructs a new CommandOption.

§Panics:

Panics if the name is empty.

§Example
use clapi::{Command, CommandOption};

let result = Command::new("MyApp")
    .option(CommandOption::new("enable"))
    .parse_from(vec!["--enable"])
    .unwrap();

assert!(result.options().contains("enable"));
Source

pub fn get_name(&self) -> &str

Returns the name of this option.

Source

pub fn get_aliases(&self) -> Aliases<'_>

Returns an Iterator over the aliases of this option.

Source

pub fn get_description(&self) -> Option<&str>

Returns a short description of this option or None if not set.

Source

pub fn is_required(&self) -> bool

Returns true if this option is required.

Source

pub fn is_hidden(&self) -> bool

Returns true if this option is no visible for help.

Source

pub fn is_global(&self) -> bool

Returns true if this is an global option.

Source

pub fn allow_multiple(&self) -> bool

Returns true if this option is allowed to appear multiple times.

Source

pub fn is_assign_required(&self) -> bool

Returns true if the option requires an assign operator.

Source

pub fn get_arg(&self) -> Option<&Argument>

Returns the Argument this option takes or None if have more than 1 argument.

Source

pub fn get_args(&self) -> &ArgumentList

Returns the Arguments of this option.

Source

pub fn take_args(&self) -> bool

Returns true if this option take arguments.

Source

pub fn has_alias<S: AsRef<str>>(&self, alias: S) -> bool

Returns true if option contains the specified alias.

Source

pub fn alias<S: Into<String>>(self, alias: S) -> Self

Adds a new alias to this option.

§Panics:

Panics if the alias is empty.

§Example
use clapi::{Command, CommandOption};

let result = Command::new("MyApp")
    .option(CommandOption::new("test").alias("t"))
    .parse_from(vec!["-t"])
    .unwrap();

assert!(result.options().contains("test"));
Source

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"));
Source

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());
Source

pub fn hidden(self, is_hidden: bool) -> Self

Specify if this option is hidden for the help.

§Example
use clapi::CommandOption;

let option = CommandOption::new("enable").hidden(true);
assert!(option.is_hidden());
Source

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"));
Source

pub fn global(self, is_global: bool) -> Self

Specify if this is a global option.

Source

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());
Source

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/"));
Source

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

Source§

fn clone(&self) -> CommandOption

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 CommandOption

Source§

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

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

impl<'de> Deserialize<'de> for CommandOption

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Self, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for CommandOption

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for CommandOption

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for CommandOption

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for CommandOption

Auto Trait Implementations§

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,