]>
git.proxmox.com Git - rustc.git/blob - vendor/clap/src/build/possible_value.rs
3 use crate::util
::eq_ignore_case
;
5 /// A possible value of an argument.
7 /// This is used for specifying [possible values] of [Args].
9 /// **NOTE:** This struct is likely not needed for most usecases as it is only required to
10 /// [hide] single values from help messages and shell completions or to attach [help] to possible values.
15 /// # use clap::{Arg, PossibleValue};
16 /// let cfg = Arg::new("config")
17 /// .takes_value(true)
18 /// .value_name("FILE")
19 /// .possible_value(PossibleValue::new("fast"))
20 /// .possible_value(PossibleValue::new("slow").help("slower than fast"))
21 /// .possible_value(PossibleValue::new("secret speed").hide(true));
23 /// [Args]: crate::Arg
24 /// [possible values]: crate::Arg::possible_value()
25 /// [hide]: PossibleValue::hide()
26 /// [help]: PossibleValue::help()
27 #[derive(Debug, Default, Clone, PartialEq, Eq)]
28 pub struct PossibleValue
<'help
> {
30 help
: Option
<&'help
str>,
31 aliases
: Vec
<&'help
str>, // (name, visible)
35 impl<'help
> PossibleValue
<'help
> {
36 /// Create a [`PossibleValue`] with its name.
38 /// The name will be used to decide whether this value was provided by the user to an argument.
40 /// **NOTE:** In case it is not [hidden] it will also be shown in help messages for arguments
41 /// that use it as a [possible value] and have not hidden them through [`Arg::hide_possible_values(true)`].
46 /// # use clap::PossibleValue;
47 /// PossibleValue::new("fast")
50 /// [hidden]: PossibleValue::hide
51 /// [possible value]: crate::Arg::possible_values
52 /// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values()
53 pub fn new(name
: &'help
str) -> Self {
60 /// Sets the help description of the value.
62 /// This is typically displayed in completions (where supported) and should be a short, one-line
68 /// # use clap::PossibleValue;
69 /// PossibleValue::new("slow")
75 pub fn help(mut self, help
: &'help
str) -> Self {
76 self.help
= Some(help
);
80 /// Hides this value from help and shell completions.
82 /// This is an alternative to hiding through [`Arg::hide_possible_values(true)`], if you only
83 /// want to hide some values.
88 /// # use clap::PossibleValue;
89 /// PossibleValue::new("secret")
93 /// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values()
96 pub fn hide(mut self, yes
: bool
) -> Self {
101 /// Sets a *hidden* alias for this argument value.
106 /// # use clap::PossibleValue;
107 /// PossibleValue::new("slow")
108 /// .alias("not-fast")
112 pub fn alias(mut self, name
: &'help
str) -> Self {
113 self.aliases
.push(name
);
117 /// Sets multiple *hidden* aliases for this argument value.
122 /// # use clap::PossibleValue;
123 /// PossibleValue::new("slow")
124 /// .aliases(["not-fast", "snake-like"])
128 pub fn aliases
<I
>(mut self, names
: I
) -> Self
130 I
: IntoIterator
<Item
= &'help
str>,
132 self.aliases
.extend(names
.into_iter());
138 impl<'help
> PossibleValue
<'help
> {
139 /// Get the name of the argument value
141 pub fn get_name(&self) -> &'help
str {
145 /// Get the help specified for this argument, if any
147 pub fn get_help(&self) -> Option
<&'help
str> {
151 /// Deprecated, replaced with [`PossibleValue::is_hide_set`]
153 #[deprecated(since = "3.1.0", note = "Replaced with `PossibleValue::is_hide_set`")]
154 pub fn is_hidden(&self) -> bool
{
158 /// Report if [`PossibleValue::hide`] is set
160 pub fn is_hide_set(&self) -> bool
{
164 /// Get the name if argument value is not hidden, `None` otherwise
165 pub fn get_visible_name(&self) -> Option
<&'help
str> {
173 /// Returns all valid values of the argument value.
175 /// Namely the name and all aliases.
176 pub fn get_name_and_aliases(&self) -> impl Iterator
<Item
= &'help
str> + '_
{
177 iter
::once(&self.name
).chain(&self.aliases
).copied()
180 /// Tests if the value is valid for this argument value
182 /// The value is valid if it is either the name or one of the aliases.
187 /// # use clap::PossibleValue;
188 /// let arg_value = PossibleValue::new("fast").alias("not-slow");
190 /// assert!(arg_value.matches("fast", false));
191 /// assert!(arg_value.matches("not-slow", false));
193 /// assert!(arg_value.matches("FAST", true));
194 /// assert!(!arg_value.matches("FAST", false));
196 pub fn matches(&self, value
: &str, ignore_case
: bool
) -> bool
{
198 self.get_name_and_aliases()
199 .any(|name
| eq_ignore_case(name
, value
))
201 self.get_name_and_aliases().any(|name
| name
== value
)
206 impl<'help
> From
<&'help
str> for PossibleValue
<'help
> {
207 fn from(s
: &'help
str) -> Self {
212 impl<'help
> From
<&'help
&'help
str> for PossibleValue
<'help
> {
213 fn from(s
: &'help
&'help
str) -> Self {