[go: up one dir, main page]

Struct clap::ArgGroup

source ·
pub struct ArgGroup<'n, 'ar> { /* private fields */ }
Expand description

ArgGroups are a family of related arguments and way for you to say, “Any of these arguments”. By placing arguments in a logical group, you can make easier requirement and exclusion rules intead of having to list each individually, or when you want a rule to apply “any but not all” arguments.

For instance, you can make an entire ArgGroup required, this means that one (and only one) argument. from that group must be present. Using more than one argument from an ArgGroup causes a failure (graceful exit).

You can also do things such as name an ArgGroup as a confliction or requirement, meaning any of the arguments that belong to that group will cause a failure if present, or must present respectively.

Perhaps the most common use of ArgGroups is to require one and only one argument to be present out of a given set. Imagine that you had multiple arguments, and you want one of them to be required, but making all of them required isn’t feasible because perhaps they conflict with each other. For example, lets say that you were building an application where one could set a given version number by supplying a string with an option argument, i.e. --set-ver v1.2.3, you also wanted to support automatically using a previous version number and simply incrementing one of the three numbers. So you create three flags --major, --minor, and --patch. All of these arguments shouldn’t be used at one time but you want to specify that at least one of them is used. For this, you can create a group.

§Example

let _ = App::new("app")
.args_from_usage("--set-ver [ver] 'set the version manually'
                  --major         'auto increase major'
                  --minor         'auto increase minor'
                  --patch         'auto increase patch")
.arg_group(ArgGroup::with_name("vers")
                    .add_all(vec!["ver", "major", "minor","patch"])
                    .required(true))

Implementations§

source§

impl<'n, 'ar> ArgGroup<'n, 'ar>

source

pub fn with_name(n: &'n str) -> ArgGroup<'n, 'ar>

Creates a new instace of ArgGroup using a unique string name. The name will only be used by the library consumer and not displayed to the use.

§Example
ArgGroup::with_name("conifg")
source

pub fn add(self, n: &'ar str) -> ArgGroup<'n, 'ar>

Adds an argument to this group by name

§Example
.add("config")
source

pub fn add_all(self, ns: Vec<&'ar str>) -> ArgGroup<'n, 'ar>

Adds multiple arguments to this group by name using a Vec

§Example
.add_all(vec!["config", "input", "output"])
source

pub fn required(self, r: bool) -> ArgGroup<'n, 'ar>

Sets the requirement of this group. A required group will be displayed in the usage string of the application in the format [arg|arg2|arg3]. A required ArgGroup simply states that one, and only one argument from this group must be present at runtime (unless conflicting with another argument).

§Example
.required(true)
source

pub fn requires(self, n: &'ar str) -> ArgGroup<'n, 'ar>

Sets the requirement rules of this group. This is not to be confused with a required group. Requirement rules function just like argument requirement rules, you can name other arguments or groups that must be present when one of the arguments from this group is used.

NOTE: The name provided may be an argument, or group name

§Example
.requires("config")
source

pub fn requires_all(self, ns: Vec<&'ar str>) -> ArgGroup<'n, 'ar>

Sets the requirement rules of this group. This is not to be confused with a required group. Requirement rules function just like argument requirement rules, you can name other arguments or groups that must be present when one of the arguments from this group is used.

NOTE: The names provided may be an argument, or group name

§Example
.requires_all(vec!["config", "input"])
source

pub fn conflicts_with(self, n: &'ar str) -> ArgGroup<'n, 'ar>

Sets the exclusion rules of this group. Exclusion rules function just like argument exclusion rules, you can name other arguments or groups that must not be present when one of the arguments from this group are used.

NOTE: The name provided may be an argument, or group name

§Example
.conflicts_with("config")
source

pub fn conflicts_with_all(self, ns: Vec<&'ar str>) -> ArgGroup<'n, 'ar>

Sets the exclusion rules of this group. Exclusion rules function just like argument exclusion rules, you can name other arguments or groups that must not be present when one of the arguments from this group are used.

NOTE: The names provided may be an argument, or group name

§Example
.conflicts_with_all(vec!["config", "input"])

Trait Implementations§

source§

impl<'n, 'ar> Debug for ArgGroup<'n, 'ar>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'n, 'ar> Freeze for ArgGroup<'n, 'ar>

§

impl<'n, 'ar> RefUnwindSafe for ArgGroup<'n, 'ar>

§

impl<'n, 'ar> Send for ArgGroup<'n, 'ar>

§

impl<'n, 'ar> Sync for ArgGroup<'n, 'ar>

§

impl<'n, 'ar> Unpin for ArgGroup<'n, 'ar>

§

impl<'n, 'ar> UnwindSafe for ArgGroup<'n, 'ar>

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