| 1 |
// Std |
| 2 |
#[cfg(feature = "env")] |
| 3 |
use std::env; |
| 4 |
#[cfg(feature = "env")] |
| 5 |
use std::ffi::OsString; |
| 6 |
use std::{ |
| 7 |
cmp::{Ord, Ordering}, |
| 8 |
fmt::{self, Display, Formatter}, |
| 9 |
str, |
| 10 |
}; |
| 11 |
|
| 12 |
// Internal |
| 13 |
use super::{ArgFlags, ArgSettings}; |
| 14 |
use crate::ArgAction; |
| 15 |
use crate::INTERNAL_ERROR_MSG; |
| 16 |
use crate::Id; |
| 17 |
use crate::ValueHint; |
| 18 |
use crate::builder::ArgPredicate; |
| 19 |
use crate::builder::IntoResettable; |
| 20 |
use crate::builder::OsStr; |
| 21 |
use crate::builder::PossibleValue; |
| 22 |
use crate::builder::Str; |
| 23 |
use crate::builder::StyledStr; |
| 24 |
use crate::builder::Styles; |
| 25 |
use crate::builder::ValueRange; |
| 26 |
#[cfg(feature = "unstable-ext")] |
| 27 |
use crate::builder::ext::Extension; |
| 28 |
use crate::builder::ext::Extensions; |
| 29 |
use crate::util::AnyValueId; |
| 30 |
|
| 31 |
/// The abstract representation of a command line argument. Used to set all the options and |
| 32 |
/// relationships that define a valid argument for the program. |
| 33 |
/// |
| 34 |
/// There are two methods for constructing [`Arg`]s, using the builder pattern and setting options |
| 35 |
/// manually, or using a usage string which is far less verbose but has fewer options. You can also |
| 36 |
/// use a combination of the two methods to achieve the best of both worlds. |
| 37 |
/// |
| 38 |
/// - [Basic API][crate::Arg#basic-api] |
| 39 |
/// - [Value Handling][crate::Arg#value-handling] |
| 40 |
/// - [Help][crate::Arg#help-1] |
| 41 |
/// - [Advanced Argument Relations][crate::Arg#advanced-argument-relations] |
| 42 |
/// - [Reflection][crate::Arg#reflection] |
| 43 |
/// |
| 44 |
/// # Examples |
| 45 |
/// |
| 46 |
/// ```rust |
| 47 |
/// # use clap_builder as clap; |
| 48 |
/// # use clap::{Arg, arg, ArgAction}; |
| 49 |
/// // Using the traditional builder pattern and setting each option manually |
| 50 |
/// let cfg = Arg::new("config") |
| 51 |
/// .short('c') |
| 52 |
/// .long("config") |
| 53 |
/// .action(ArgAction::Set) |
| 54 |
/// .value_name("FILE") |
| 55 |
/// .help("Provides a config file to myprog"); |
| 56 |
/// // Using a usage string (setting a similar argument to the one above) |
| 57 |
/// let input = arg!(-i --input <FILE> "Provides an input file to the program"); |
| 58 |
/// ``` |
| 59 |
#[derive(Default, Clone)] |
| 60 |
pub struct Arg { |
| 61 |
pub(crate) id: Id, |
| 62 |
pub(crate) help: Option<StyledStr>, |
| 63 |
pub(crate) long_help: Option<StyledStr>, |
| 64 |
pub(crate) action: Option<ArgAction>, |
| 65 |
pub(crate) value_parser: Option<super::ValueParser>, |
| 66 |
pub(crate) conflicts: Vec<Id>, |
| 67 |
pub(crate) settings: ArgFlags, |
| 68 |
pub(crate) overrides: Vec<Id>, |
| 69 |
pub(crate) groups: Vec<Id>, |
| 70 |
pub(crate) requires: Vec<(ArgPredicate, Id)>, |
| 71 |
pub(crate) r_ifs: Vec<(Id, OsStr)>, |
| 72 |
pub(crate) r_ifs_all: Vec<(Id, OsStr)>, |
| 73 |
pub(crate) r_unless: Vec<Id>, |
| 74 |
pub(crate) r_unless_all: Vec<Id>, |
| 75 |
pub(crate) short: Option<char>, |
| 76 |
pub(crate) long: Option<Str>, |
| 77 |
pub(crate) aliases: Vec<(Str, bool)>, // (name, visible) |
| 78 |
pub(crate) short_aliases: Vec<(char, bool)>, // (name, visible) |
| 79 |
pub(crate) disp_ord: Option<usize>, |
| 80 |
pub(crate) val_names: Vec<Str>, |
| 81 |
pub(crate) num_vals: Option<ValueRange>, |
| 82 |
pub(crate) val_delim: Option<char>, |
| 83 |
pub(crate) default_vals: Vec<OsStr>, |
| 84 |
pub(crate) default_vals_ifs: Vec<(Id, ArgPredicate, Option<Vec<OsStr>>)>, |
| 85 |
pub(crate) default_missing_vals: Vec<OsStr>, |
| 86 |
#[cfg(feature = "env")] |
| 87 |
pub(crate) env: Option<(OsStr, Option<OsString>)>, |
| 88 |
pub(crate) terminator: Option<Str>, |
| 89 |
pub(crate) index: Option<usize>, |
| 90 |
pub(crate) help_heading: Option<Option<Str>>, |
| 91 |
pub(crate) ext: Extensions, |
| 92 |
} |
| 93 |
|
| 94 |
/// # Basic API |
| 95 |
impl Arg { |
| 96 |
/// Create a new [`Arg`] with a unique name. |
| 97 |
/// |
| 98 |
/// The name is used to check whether or not the argument was used at |
| 99 |
/// runtime, get values, set relationships with other args, etc.. |
| 100 |
/// |
| 101 |
/// By default, an `Arg` is |
| 102 |
/// - Positional, see [`Arg::short`] or [`Arg::long`] turn it into an option |
| 103 |
/// - Accept a single value, see [`Arg::action`] to override this |
| 104 |
/// |
| 105 |
/// <div class="warning"> |
| 106 |
/// |
| 107 |
/// **NOTE:** In the case of arguments that take values (i.e. [`Arg::action(ArgAction::Set)`]) |
| 108 |
/// and positional arguments (i.e. those without a preceding `-` or `--`) the name will also |
| 109 |
/// be displayed when the user prints the usage/help information of the program. |
| 110 |
/// |
| 111 |
/// </div> |
| 112 |
/// |
| 113 |
/// # Examples |
| 114 |
/// |
| 115 |
/// ```rust |
| 116 |
/// # use clap_builder as clap; |
| 117 |
/// # use clap::{Command, Arg}; |
| 118 |
/// Arg::new("config") |
| 119 |
/// # ; |
| 120 |
/// ``` |
| 121 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 122 |
pub fn new(id: impl Into<Id>) -> Self { |
| 123 |
Self::default().id(id) |
| 124 |
} |
| 125 |
|
| 126 |
/// Set the identifier used for referencing this argument in the clap API. |
| 127 |
/// |
| 128 |
/// See [`Arg::new`] for more details. |
| 129 |
#[must_use] |
| 130 |
pub fn id(mut self, id: impl Into<Id>) -> Self { |
| 131 |
self.id = id.into(); |
| 132 |
self |
| 133 |
} |
| 134 |
|
| 135 |
/// Sets the short version of the argument without the preceding `-`. |
| 136 |
/// |
| 137 |
/// By default `V` and `h` are used by the auto-generated `version` and `help` arguments, |
| 138 |
/// respectively. You will need to disable the auto-generated flags |
| 139 |
/// ([`disable_help_flag`][crate::Command::disable_help_flag], |
| 140 |
/// [`disable_version_flag`][crate::Command::disable_version_flag]) and define your own. |
| 141 |
/// |
| 142 |
/// # Examples |
| 143 |
/// |
| 144 |
/// When calling `short`, use a single valid UTF-8 character which will allow using the |
| 145 |
/// argument via a single hyphen (`-`) such as `-c`: |
| 146 |
/// |
| 147 |
/// ```rust |
| 148 |
/// # use clap_builder as clap; |
| 149 |
/// # use clap::{Command, Arg, ArgAction}; |
| 150 |
/// let m = Command::new("prog") |
| 151 |
/// .arg(Arg::new("config") |
| 152 |
/// .short('c') |
| 153 |
/// .action(ArgAction::Set)) |
| 154 |
/// .get_matches_from(vec![ |
| 155 |
/// "prog", "-c", "file.toml" |
| 156 |
/// ]); |
| 157 |
/// |
| 158 |
/// assert_eq!(m.get_one::<String>("config").map(String::as_str), Some("file.toml")); |
| 159 |
/// ``` |
| 160 |
/// |
| 161 |
/// To use `-h` for your own flag and still have help: |
| 162 |
/// ```rust |
| 163 |
/// # use clap_builder as clap; |
| 164 |
/// # use clap::{Command, Arg, ArgAction}; |
| 165 |
/// let m = Command::new("prog") |
| 166 |
/// .disable_help_flag(true) |
| 167 |
/// .arg(Arg::new("host") |
| 168 |
/// .short('h') |
| 169 |
/// .long("host")) |
| 170 |
/// .arg(Arg::new("help") |
| 171 |
/// .long("help") |
| 172 |
/// .global(true) |
| 173 |
/// .action(ArgAction::Help)) |
| 174 |
/// .get_matches_from(vec![ |
| 175 |
/// "prog", "-h", "wikipedia.org" |
| 176 |
/// ]); |
| 177 |
/// |
| 178 |
/// assert_eq!(m.get_one::<String>("host").map(String::as_str), Some("wikipedia.org")); |
| 179 |
/// ``` |
| 180 |
#[inline] |
| 181 |
#[must_use] |
| 182 |
pub fn short(mut self, s: impl IntoResettable<char>) -> Self { |
| 183 |
if let Some(s) = s.into_resettable().into_option() { |
| 184 |
debug_assert!(s != '-', "short option name cannot be `-`"); |
| 185 |
self.short = Some(s); |
| 186 |
} else { |
| 187 |
self.short = None; |
| 188 |
} |
| 189 |
self |
| 190 |
} |
| 191 |
|
| 192 |
/// Sets the long version of the argument without the preceding `--`. |
| 193 |
/// |
| 194 |
/// By default `version` and `help` are used by the auto-generated `version` and `help` |
| 195 |
/// arguments, respectively. You may use the word `version` or `help` for the long form of your |
| 196 |
/// own arguments, in which case `clap` simply will not assign those to the auto-generated |
| 197 |
/// `version` or `help` arguments. |
| 198 |
/// |
| 199 |
/// <div class="warning"> |
| 200 |
/// |
| 201 |
/// **NOTE:** Any leading `-` characters will be stripped |
| 202 |
/// |
| 203 |
/// </div> |
| 204 |
/// |
| 205 |
/// # Examples |
| 206 |
/// |
| 207 |
/// To set `long` use a word containing valid UTF-8. If you supply a double leading |
| 208 |
/// `--` such as `--config` they will be stripped. Hyphens in the middle of the word, however, |
| 209 |
/// will *not* be stripped (i.e. `config-file` is allowed). |
| 210 |
/// |
| 211 |
/// Setting `long` allows using the argument via a double hyphen (`--`) such as `--config` |
| 212 |
/// |
| 213 |
/// ```rust |
| 214 |
/// # use clap_builder as clap; |
| 215 |
/// # use clap::{Command, Arg, ArgAction}; |
| 216 |
/// let m = Command::new("prog") |
| 217 |
/// .arg(Arg::new("cfg") |
| 218 |
/// .long("config") |
| 219 |
/// .action(ArgAction::Set)) |
| 220 |
/// .get_matches_from(vec![ |
| 221 |
/// "prog", "--config", "file.toml" |
| 222 |
/// ]); |
| 223 |
/// |
| 224 |
/// assert_eq!(m.get_one::<String>("cfg").map(String::as_str), Some("file.toml")); |
| 225 |
/// ``` |
| 226 |
#[inline] |
| 227 |
#[must_use] |
| 228 |
pub fn long(mut self, l: impl IntoResettable<Str>) -> Self { |
| 229 |
self.long = l.into_resettable().into_option(); |
| 230 |
self |
| 231 |
} |
| 232 |
|
| 233 |
/// Add an alias, which functions as a hidden long flag. |
| 234 |
/// |
| 235 |
/// This is more efficient, and easier than creating multiple hidden arguments as one only |
| 236 |
/// needs to check for the existence of this command, and not all variants. |
| 237 |
/// |
| 238 |
/// # Examples |
| 239 |
/// |
| 240 |
/// ```rust |
| 241 |
/// # use clap_builder as clap; |
| 242 |
/// # use clap::{Command, Arg, ArgAction}; |
| 243 |
/// let m = Command::new("prog") |
| 244 |
/// .arg(Arg::new("test") |
| 245 |
/// .long("test") |
| 246 |
/// .alias("alias") |
| 247 |
/// .action(ArgAction::Set)) |
| 248 |
/// .get_matches_from(vec![ |
| 249 |
/// "prog", "--alias", "cool" |
| 250 |
/// ]); |
| 251 |
/// assert_eq!(m.get_one::<String>("test").unwrap(), "cool"); |
| 252 |
/// ``` |
| 253 |
#[must_use] |
| 254 |
pub fn alias(mut self, name: impl IntoResettable<Str>) -> Self { |
| 255 |
if let Some(name) = name.into_resettable().into_option() { |
| 256 |
self.aliases.push((name, false)); |
| 257 |
} else { |
| 258 |
self.aliases.clear(); |
| 259 |
} |
| 260 |
self |
| 261 |
} |
| 262 |
|
| 263 |
/// Add an alias, which functions as a hidden short flag. |
| 264 |
/// |
| 265 |
/// This is more efficient, and easier than creating multiple hidden arguments as one only |
| 266 |
/// needs to check for the existence of this command, and not all variants. |
| 267 |
/// |
| 268 |
/// # Examples |
| 269 |
/// |
| 270 |
/// ```rust |
| 271 |
/// # use clap_builder as clap; |
| 272 |
/// # use clap::{Command, Arg, ArgAction}; |
| 273 |
/// let m = Command::new("prog") |
| 274 |
/// .arg(Arg::new("test") |
| 275 |
/// .short('t') |
| 276 |
/// .short_alias('e') |
| 277 |
/// .action(ArgAction::Set)) |
| 278 |
/// .get_matches_from(vec![ |
| 279 |
/// "prog", "-e", "cool" |
| 280 |
/// ]); |
| 281 |
/// assert_eq!(m.get_one::<String>("test").unwrap(), "cool"); |
| 282 |
/// ``` |
| 283 |
#[must_use] |
| 284 |
pub fn short_alias(mut self, name: impl IntoResettable<char>) -> Self { |
| 285 |
if let Some(name) = name.into_resettable().into_option() { |
| 286 |
debug_assert!(name != '-', "short alias name cannot be `-`"); |
| 287 |
self.short_aliases.push((name, false)); |
| 288 |
} else { |
| 289 |
self.short_aliases.clear(); |
| 290 |
} |
| 291 |
self |
| 292 |
} |
| 293 |
|
| 294 |
/// Add aliases, which function as hidden long flags. |
| 295 |
/// |
| 296 |
/// This is more efficient, and easier than creating multiple hidden subcommands as one only |
| 297 |
/// needs to check for the existence of this command, and not all variants. |
| 298 |
/// |
| 299 |
/// # Examples |
| 300 |
/// |
| 301 |
/// ```rust |
| 302 |
/// # use clap_builder as clap; |
| 303 |
/// # use clap::{Command, Arg, ArgAction}; |
| 304 |
/// let m = Command::new("prog") |
| 305 |
/// .arg(Arg::new("test") |
| 306 |
/// .long("test") |
| 307 |
/// .aliases(["do-stuff", "do-tests", "tests"]) |
| 308 |
/// .action(ArgAction::SetTrue) |
| 309 |
/// .help("the file to add") |
| 310 |
/// .required(false)) |
| 311 |
/// .get_matches_from(vec![ |
| 312 |
/// "prog", "--do-tests" |
| 313 |
/// ]); |
| 314 |
/// assert_eq!(m.get_flag("test"), true); |
| 315 |
/// ``` |
| 316 |
#[must_use] |
| 317 |
pub fn aliases(mut self, names: impl IntoIterator<Item = impl Into<Str>>) -> Self { |
| 318 |
self.aliases |
| 319 |
.extend(names.into_iter().map(|x| (x.into(), false))); |
| 320 |
self |
| 321 |
} |
| 322 |
|
| 323 |
/// Add aliases, which functions as a hidden short flag. |
| 324 |
/// |
| 325 |
/// This is more efficient, and easier than creating multiple hidden subcommands as one only |
| 326 |
/// needs to check for the existence of this command, and not all variants. |
| 327 |
/// |
| 328 |
/// # Examples |
| 329 |
/// |
| 330 |
/// ```rust |
| 331 |
/// # use clap_builder as clap; |
| 332 |
/// # use clap::{Command, Arg, ArgAction}; |
| 333 |
/// let m = Command::new("prog") |
| 334 |
/// .arg(Arg::new("test") |
| 335 |
/// .short('t') |
| 336 |
/// .short_aliases(['e', 's']) |
| 337 |
/// .action(ArgAction::SetTrue) |
| 338 |
/// .help("the file to add") |
| 339 |
/// .required(false)) |
| 340 |
/// .get_matches_from(vec![ |
| 341 |
/// "prog", "-s" |
| 342 |
/// ]); |
| 343 |
/// assert_eq!(m.get_flag("test"), true); |
| 344 |
/// ``` |
| 345 |
#[must_use] |
| 346 |
pub fn short_aliases(mut self, names: impl IntoIterator<Item = char>) -> Self { |
| 347 |
for s in names { |
| 348 |
debug_assert!(s != '-', "short alias name cannot be `-`"); |
| 349 |
self.short_aliases.push((s, false)); |
| 350 |
} |
| 351 |
self |
| 352 |
} |
| 353 |
|
| 354 |
/// Add an alias, which functions as a visible long flag. |
| 355 |
/// |
| 356 |
/// Like [`Arg::alias`], except that they are visible inside the help message. |
| 357 |
/// |
| 358 |
/// # Examples |
| 359 |
/// |
| 360 |
/// ```rust |
| 361 |
/// # use clap_builder as clap; |
| 362 |
/// # use clap::{Command, Arg, ArgAction}; |
| 363 |
/// let m = Command::new("prog") |
| 364 |
/// .arg(Arg::new("test") |
| 365 |
/// .visible_alias("something-awesome") |
| 366 |
/// .long("test") |
| 367 |
/// .action(ArgAction::Set)) |
| 368 |
/// .get_matches_from(vec![ |
| 369 |
/// "prog", "--something-awesome", "coffee" |
| 370 |
/// ]); |
| 371 |
/// assert_eq!(m.get_one::<String>("test").unwrap(), "coffee"); |
| 372 |
/// ``` |
| 373 |
/// [`Command::alias`]: Arg::alias() |
| 374 |
#[must_use] |
| 375 |
pub fn visible_alias(mut self, name: impl IntoResettable<Str>) -> Self { |
| 376 |
if let Some(name) = name.into_resettable().into_option() { |
| 377 |
self.aliases.push((name, true)); |
| 378 |
} else { |
| 379 |
self.aliases.clear(); |
| 380 |
} |
| 381 |
self |
| 382 |
} |
| 383 |
|
| 384 |
/// Add an alias, which functions as a visible short flag. |
| 385 |
/// |
| 386 |
/// Like [`Arg::short_alias`], except that they are visible inside the help message. |
| 387 |
/// |
| 388 |
/// # Examples |
| 389 |
/// |
| 390 |
/// ```rust |
| 391 |
/// # use clap_builder as clap; |
| 392 |
/// # use clap::{Command, Arg, ArgAction}; |
| 393 |
/// let m = Command::new("prog") |
| 394 |
/// .arg(Arg::new("test") |
| 395 |
/// .long("test") |
| 396 |
/// .visible_short_alias('t') |
| 397 |
/// .action(ArgAction::Set)) |
| 398 |
/// .get_matches_from(vec![ |
| 399 |
/// "prog", "-t", "coffee" |
| 400 |
/// ]); |
| 401 |
/// assert_eq!(m.get_one::<String>("test").unwrap(), "coffee"); |
| 402 |
/// ``` |
| 403 |
#[must_use] |
| 404 |
pub fn visible_short_alias(mut self, name: impl IntoResettable<char>) -> Self { |
| 405 |
if let Some(name) = name.into_resettable().into_option() { |
| 406 |
debug_assert!(name != '-', "short alias name cannot be `-`"); |
| 407 |
self.short_aliases.push((name, true)); |
| 408 |
} else { |
| 409 |
self.short_aliases.clear(); |
| 410 |
} |
| 411 |
self |
| 412 |
} |
| 413 |
|
| 414 |
/// Add aliases, which function as visible long flags. |
| 415 |
/// |
| 416 |
/// Like [`Arg::aliases`], except that they are visible inside the help message. |
| 417 |
/// |
| 418 |
/// # Examples |
| 419 |
/// |
| 420 |
/// ```rust |
| 421 |
/// # use clap_builder as clap; |
| 422 |
/// # use clap::{Command, Arg, ArgAction}; |
| 423 |
/// let m = Command::new("prog") |
| 424 |
/// .arg(Arg::new("test") |
| 425 |
/// .long("test") |
| 426 |
/// .action(ArgAction::SetTrue) |
| 427 |
/// .visible_aliases(["something", "awesome", "cool"])) |
| 428 |
/// .get_matches_from(vec![ |
| 429 |
/// "prog", "--awesome" |
| 430 |
/// ]); |
| 431 |
/// assert_eq!(m.get_flag("test"), true); |
| 432 |
/// ``` |
| 433 |
/// [`Command::aliases`]: Arg::aliases() |
| 434 |
#[must_use] |
| 435 |
pub fn visible_aliases(mut self, names: impl IntoIterator<Item = impl Into<Str>>) -> Self { |
| 436 |
self.aliases |
| 437 |
.extend(names.into_iter().map(|n| (n.into(), true))); |
| 438 |
self |
| 439 |
} |
| 440 |
|
| 441 |
/// Add aliases, which function as visible short flags. |
| 442 |
/// |
| 443 |
/// Like [`Arg::short_aliases`], except that they are visible inside the help message. |
| 444 |
/// |
| 445 |
/// # Examples |
| 446 |
/// |
| 447 |
/// ```rust |
| 448 |
/// # use clap_builder as clap; |
| 449 |
/// # use clap::{Command, Arg, ArgAction}; |
| 450 |
/// let m = Command::new("prog") |
| 451 |
/// .arg(Arg::new("test") |
| 452 |
/// .long("test") |
| 453 |
/// .action(ArgAction::SetTrue) |
| 454 |
/// .visible_short_aliases(['t', 'e'])) |
| 455 |
/// .get_matches_from(vec![ |
| 456 |
/// "prog", "-t" |
| 457 |
/// ]); |
| 458 |
/// assert_eq!(m.get_flag("test"), true); |
| 459 |
/// ``` |
| 460 |
#[must_use] |
| 461 |
pub fn visible_short_aliases(mut self, names: impl IntoIterator<Item = char>) -> Self { |
| 462 |
for n in names { |
| 463 |
debug_assert!(n != '-', "short alias name cannot be `-`"); |
| 464 |
self.short_aliases.push((n, true)); |
| 465 |
} |
| 466 |
self |
| 467 |
} |
| 468 |
|
| 469 |
/// Specifies the index of a positional argument **starting at** 1. |
| 470 |
/// |
| 471 |
/// <div class="warning"> |
| 472 |
/// |
| 473 |
/// **NOTE:** The index refers to position according to **other positional argument**. It does |
| 474 |
/// not define position in the argument list as a whole. |
| 475 |
/// |
| 476 |
/// </div> |
| 477 |
/// |
| 478 |
/// <div class="warning"> |
| 479 |
/// |
| 480 |
/// **NOTE:** You can optionally leave off the `index` method, and the index will be |
| 481 |
/// assigned in order of evaluation. Utilizing the `index` method allows for setting |
| 482 |
/// indexes out of order |
| 483 |
/// |
| 484 |
/// </div> |
| 485 |
/// |
| 486 |
/// <div class="warning"> |
| 487 |
/// |
| 488 |
/// **NOTE:** This is only meant to be used for positional arguments and shouldn't to be used |
| 489 |
/// with [`Arg::short`] or [`Arg::long`]. |
| 490 |
/// |
| 491 |
/// </div> |
| 492 |
/// |
| 493 |
/// <div class="warning"> |
| 494 |
/// |
| 495 |
/// **NOTE:** When utilized with [`Arg::num_args(1..)`][Arg::num_args], only the **last** positional argument |
| 496 |
/// may be defined as having a variable number of arguments (i.e. with the highest index) |
| 497 |
/// |
| 498 |
/// </div> |
| 499 |
/// |
| 500 |
/// # Panics |
| 501 |
/// |
| 502 |
/// [`Command`] will [`panic!`] if indexes are skipped (such as defining `index(1)` and `index(3)` |
| 503 |
/// but not `index(2)`, or a positional argument is defined as multiple and is not the highest |
| 504 |
/// index (debug builds) |
| 505 |
/// |
| 506 |
/// # Examples |
| 507 |
/// |
| 508 |
/// ```rust |
| 509 |
/// # use clap_builder as clap; |
| 510 |
/// # use clap::{Command, Arg}; |
| 511 |
/// Arg::new("config") |
| 512 |
/// .index(1) |
| 513 |
/// # ; |
| 514 |
/// ``` |
| 515 |
/// |
| 516 |
/// ```rust |
| 517 |
/// # use clap_builder as clap; |
| 518 |
/// # use clap::{Command, Arg, ArgAction}; |
| 519 |
/// let m = Command::new("prog") |
| 520 |
/// .arg(Arg::new("mode") |
| 521 |
/// .index(1)) |
| 522 |
/// .arg(Arg::new("debug") |
| 523 |
/// .long("debug") |
| 524 |
/// .action(ArgAction::SetTrue)) |
| 525 |
/// .get_matches_from(vec![ |
| 526 |
/// "prog", "--debug", "fast" |
| 527 |
/// ]); |
| 528 |
/// |
| 529 |
/// assert!(m.contains_id("mode")); |
| 530 |
/// assert_eq!(m.get_one::<String>("mode").unwrap(), "fast"); // notice index(1) means "first positional" |
| 531 |
/// // *not* first argument |
| 532 |
/// ``` |
| 533 |
/// [`Arg::short`]: Arg::short() |
| 534 |
/// [`Arg::long`]: Arg::long() |
| 535 |
/// [`Arg::num_args(true)`]: Arg::num_args() |
| 536 |
/// [`Command`]: crate::Command |
| 537 |
#[inline] |
| 538 |
#[must_use] |
| 539 |
pub fn index(mut self, idx: impl IntoResettable<usize>) -> Self { |
| 540 |
self.index = idx.into_resettable().into_option(); |
| 541 |
self |
| 542 |
} |
| 543 |
|
| 544 |
/// This is a "var arg" and everything that follows should be captured by it, as if the user had |
| 545 |
/// used a `--`. |
| 546 |
/// |
| 547 |
/// <div class="warning"> |
| 548 |
/// |
| 549 |
/// **NOTE:** To start the trailing "var arg" on unknown flags (and not just a positional |
| 550 |
/// value), set [`allow_hyphen_values`][Arg::allow_hyphen_values]. Either way, users still |
| 551 |
/// have the option to explicitly escape ambiguous arguments with `--`. |
| 552 |
/// |
| 553 |
/// </div> |
| 554 |
/// |
| 555 |
/// <div class="warning"> |
| 556 |
/// |
| 557 |
/// **NOTE:** [`Arg::value_delimiter`] still applies if set. |
| 558 |
/// |
| 559 |
/// </div> |
| 560 |
/// |
| 561 |
/// <div class="warning"> |
| 562 |
/// |
| 563 |
/// **NOTE:** Setting this requires [`Arg::num_args(..)`]. |
| 564 |
/// |
| 565 |
/// </div> |
| 566 |
/// |
| 567 |
/// # Examples |
| 568 |
/// |
| 569 |
/// ```rust |
| 570 |
/// # use clap_builder as clap; |
| 571 |
/// # use clap::{Command, arg}; |
| 572 |
/// let m = Command::new("myprog") |
| 573 |
/// .arg(arg!(<cmd> ... "commands to run").trailing_var_arg(true)) |
| 574 |
/// .get_matches_from(vec!["myprog", "arg1", "-r", "val1"]); |
| 575 |
/// |
| 576 |
/// let trail: Vec<_> = m.get_many::<String>("cmd").unwrap().collect(); |
| 577 |
/// assert_eq!(trail, ["arg1", "-r", "val1"]); |
| 578 |
/// ``` |
| 579 |
/// [`Arg::num_args(..)`]: crate::Arg::num_args() |
| 580 |
pub fn trailing_var_arg(self, yes: bool) -> Self { |
| 581 |
if yes { |
| 582 |
self.setting(ArgSettings::TrailingVarArg) |
| 583 |
} else { |
| 584 |
self.unset_setting(ArgSettings::TrailingVarArg) |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
/// This arg is the last, or final, positional argument (i.e. has the highest |
| 589 |
/// index) and is *only* able to be accessed via the `--` syntax (i.e. `$ prog args -- |
| 590 |
/// last_arg`). |
| 591 |
/// |
| 592 |
/// Even, if no other arguments are left to parse, if the user omits the `--` syntax |
| 593 |
/// they will receive an [`UnknownArgument`] error. Setting an argument to `.last(true)` also |
| 594 |
/// allows one to access this arg early using the `--` syntax. Accessing an arg early, even with |
| 595 |
/// the `--` syntax is otherwise not possible. |
| 596 |
/// |
| 597 |
/// <div class="warning"> |
| 598 |
/// |
| 599 |
/// **NOTE:** This will change the usage string to look like `$ prog [OPTIONS] [-- <ARG>]` if |
| 600 |
/// `ARG` is marked as `.last(true)`. |
| 601 |
/// |
| 602 |
/// </div> |
| 603 |
/// |
| 604 |
/// <div class="warning"> |
| 605 |
/// |
| 606 |
/// **NOTE:** This setting will imply [`crate::Command::dont_collapse_args_in_usage`] because failing |
| 607 |
/// to set this can make the usage string very confusing. |
| 608 |
/// |
| 609 |
/// </div> |
| 610 |
/// |
| 611 |
/// <div class="warning"> |
| 612 |
/// |
| 613 |
/// **NOTE**: This setting only applies to positional arguments, and has no effect on OPTIONS |
| 614 |
/// |
| 615 |
/// </div> |
| 616 |
/// |
| 617 |
/// <div class="warning"> |
| 618 |
/// |
| 619 |
/// **NOTE:** Setting this requires [taking values][Arg::num_args] |
| 620 |
/// |
| 621 |
/// </div> |
| 622 |
/// |
| 623 |
/// <div class="warning"> |
| 624 |
/// |
| 625 |
/// **WARNING:** Using this setting *and* having child subcommands is not |
| 626 |
/// recommended with the exception of *also* using |
| 627 |
/// [`crate::Command::args_conflicts_with_subcommands`] |
| 628 |
/// (or [`crate::Command::subcommand_negates_reqs`] if the argument marked `Last` is also |
| 629 |
/// marked [`Arg::required`]) |
| 630 |
/// |
| 631 |
/// </div> |
| 632 |
/// |
| 633 |
/// # Examples |
| 634 |
/// |
| 635 |
/// ```rust |
| 636 |
/// # use clap_builder as clap; |
| 637 |
/// # use clap::{Arg, ArgAction}; |
| 638 |
/// Arg::new("args") |
| 639 |
/// .action(ArgAction::Set) |
| 640 |
/// .last(true) |
| 641 |
/// # ; |
| 642 |
/// ``` |
| 643 |
/// |
| 644 |
/// Setting `last` ensures the arg has the highest [index] of all positional args |
| 645 |
/// and requires that the `--` syntax be used to access it early. |
| 646 |
/// |
| 647 |
/// ```rust |
| 648 |
/// # use clap_builder as clap; |
| 649 |
/// # use clap::{Command, Arg, ArgAction}; |
| 650 |
/// let res = Command::new("prog") |
| 651 |
/// .arg(Arg::new("first")) |
| 652 |
/// .arg(Arg::new("second")) |
| 653 |
/// .arg(Arg::new("third") |
| 654 |
/// .action(ArgAction::Set) |
| 655 |
/// .last(true)) |
| 656 |
/// .try_get_matches_from(vec![ |
| 657 |
/// "prog", "one", "--", "three" |
| 658 |
/// ]); |
| 659 |
/// |
| 660 |
/// assert!(res.is_ok()); |
| 661 |
/// let m = res.unwrap(); |
| 662 |
/// assert_eq!(m.get_one::<String>("third").unwrap(), "three"); |
| 663 |
/// assert_eq!(m.get_one::<String>("second"), None); |
| 664 |
/// ``` |
| 665 |
/// |
| 666 |
/// Even if the positional argument marked `Last` is the only argument left to parse, |
| 667 |
/// failing to use the `--` syntax results in an error. |
| 668 |
/// |
| 669 |
/// ```rust |
| 670 |
/// # use clap_builder as clap; |
| 671 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 672 |
/// let res = Command::new("prog") |
| 673 |
/// .arg(Arg::new("first")) |
| 674 |
/// .arg(Arg::new("second")) |
| 675 |
/// .arg(Arg::new("third") |
| 676 |
/// .action(ArgAction::Set) |
| 677 |
/// .last(true)) |
| 678 |
/// .try_get_matches_from(vec![ |
| 679 |
/// "prog", "one", "two", "three" |
| 680 |
/// ]); |
| 681 |
/// |
| 682 |
/// assert!(res.is_err()); |
| 683 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument); |
| 684 |
/// ``` |
| 685 |
/// [index]: Arg::index() |
| 686 |
/// [`UnknownArgument`]: crate::error::ErrorKind::UnknownArgument |
| 687 |
#[inline] |
| 688 |
#[must_use] |
| 689 |
pub fn last(self, yes: bool) -> Self { |
| 690 |
if yes { |
| 691 |
self.setting(ArgSettings::Last) |
| 692 |
} else { |
| 693 |
self.unset_setting(ArgSettings::Last) |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
/// Specifies that the argument must be present. |
| 698 |
/// |
| 699 |
/// Required by default means it is required, when no other conflicting rules or overrides have |
| 700 |
/// been evaluated. Conflicting rules take precedence over being required. |
| 701 |
/// |
| 702 |
/// **Pro tip:** Flags (i.e. not positional, or arguments that take values) shouldn't be |
| 703 |
/// required by default. This is because if a flag were to be required, it should simply be |
| 704 |
/// implied. No additional information is required from user. Flags by their very nature are |
| 705 |
/// simply boolean on/off switches. The only time a user *should* be required to use a flag |
| 706 |
/// is if the operation is destructive in nature, and the user is essentially proving to you, |
| 707 |
/// "Yes, I know what I'm doing." |
| 708 |
/// |
| 709 |
/// # Examples |
| 710 |
/// |
| 711 |
/// ```rust |
| 712 |
/// # use clap_builder as clap; |
| 713 |
/// # use clap::Arg; |
| 714 |
/// Arg::new("config") |
| 715 |
/// .required(true) |
| 716 |
/// # ; |
| 717 |
/// ``` |
| 718 |
/// |
| 719 |
/// Setting required requires that the argument be used at runtime. |
| 720 |
/// |
| 721 |
/// ```rust |
| 722 |
/// # use clap_builder as clap; |
| 723 |
/// # use clap::{Command, Arg, ArgAction}; |
| 724 |
/// let res = Command::new("prog") |
| 725 |
/// .arg(Arg::new("cfg") |
| 726 |
/// .required(true) |
| 727 |
/// .action(ArgAction::Set) |
| 728 |
/// .long("config")) |
| 729 |
/// .try_get_matches_from(vec![ |
| 730 |
/// "prog", "--config", "file.conf", |
| 731 |
/// ]); |
| 732 |
/// |
| 733 |
/// assert!(res.is_ok()); |
| 734 |
/// ``` |
| 735 |
/// |
| 736 |
/// Setting required and then *not* supplying that argument at runtime is an error. |
| 737 |
/// |
| 738 |
/// ```rust |
| 739 |
/// # use clap_builder as clap; |
| 740 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 741 |
/// let res = Command::new("prog") |
| 742 |
/// .arg(Arg::new("cfg") |
| 743 |
/// .required(true) |
| 744 |
/// .action(ArgAction::Set) |
| 745 |
/// .long("config")) |
| 746 |
/// .try_get_matches_from(vec![ |
| 747 |
/// "prog" |
| 748 |
/// ]); |
| 749 |
/// |
| 750 |
/// assert!(res.is_err()); |
| 751 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 752 |
/// ``` |
| 753 |
#[inline] |
| 754 |
#[must_use] |
| 755 |
pub fn required(self, yes: bool) -> Self { |
| 756 |
if yes { |
| 757 |
self.setting(ArgSettings::Required) |
| 758 |
} else { |
| 759 |
self.unset_setting(ArgSettings::Required) |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
/// Sets an argument that is required when this one is present |
| 764 |
/// |
| 765 |
/// i.e. when using this argument, the following argument *must* be present. |
| 766 |
/// |
| 767 |
/// <div class="warning"> |
| 768 |
/// |
| 769 |
/// **NOTE:** [Conflicting] rules and [override] rules take precedence over being required |
| 770 |
/// |
| 771 |
/// </div> |
| 772 |
/// |
| 773 |
/// # Examples |
| 774 |
/// |
| 775 |
/// ```rust |
| 776 |
/// # use clap_builder as clap; |
| 777 |
/// # use clap::Arg; |
| 778 |
/// Arg::new("config") |
| 779 |
/// .requires("input") |
| 780 |
/// # ; |
| 781 |
/// ``` |
| 782 |
/// |
| 783 |
/// Setting [`Arg::requires(name)`] requires that the argument be used at runtime if the |
| 784 |
/// defining argument is used. If the defining argument isn't used, the other argument isn't |
| 785 |
/// required |
| 786 |
/// |
| 787 |
/// ```rust |
| 788 |
/// # use clap_builder as clap; |
| 789 |
/// # use clap::{Command, Arg, ArgAction}; |
| 790 |
/// let res = Command::new("prog") |
| 791 |
/// .arg(Arg::new("cfg") |
| 792 |
/// .action(ArgAction::Set) |
| 793 |
/// .requires("input") |
| 794 |
/// .long("config")) |
| 795 |
/// .arg(Arg::new("input")) |
| 796 |
/// .try_get_matches_from(vec![ |
| 797 |
/// "prog" |
| 798 |
/// ]); |
| 799 |
/// |
| 800 |
/// assert!(res.is_ok()); // We didn't use cfg, so input wasn't required |
| 801 |
/// ``` |
| 802 |
/// |
| 803 |
/// Setting [`Arg::requires(name)`] and *not* supplying that argument is an error. |
| 804 |
/// |
| 805 |
/// ```rust |
| 806 |
/// # use clap_builder as clap; |
| 807 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 808 |
/// let res = Command::new("prog") |
| 809 |
/// .arg(Arg::new("cfg") |
| 810 |
/// .action(ArgAction::Set) |
| 811 |
/// .requires("input") |
| 812 |
/// .long("config")) |
| 813 |
/// .arg(Arg::new("input")) |
| 814 |
/// .try_get_matches_from(vec![ |
| 815 |
/// "prog", "--config", "file.conf" |
| 816 |
/// ]); |
| 817 |
/// |
| 818 |
/// assert!(res.is_err()); |
| 819 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 820 |
/// ``` |
| 821 |
/// [`Arg::requires(name)`]: Arg::requires() |
| 822 |
/// [Conflicting]: Arg::conflicts_with() |
| 823 |
/// [override]: Arg::overrides_with() |
| 824 |
#[must_use] |
| 825 |
pub fn requires(mut self, arg_id: impl IntoResettable<Id>) -> Self { |
| 826 |
if let Some(arg_id) = arg_id.into_resettable().into_option() { |
| 827 |
self.requires.push((ArgPredicate::IsPresent, arg_id)); |
| 828 |
} else { |
| 829 |
self.requires.clear(); |
| 830 |
} |
| 831 |
self |
| 832 |
} |
| 833 |
|
| 834 |
/// This argument must be passed alone; it conflicts with all other arguments. |
| 835 |
/// |
| 836 |
/// # Examples |
| 837 |
/// |
| 838 |
/// ```rust |
| 839 |
/// # use clap_builder as clap; |
| 840 |
/// # use clap::Arg; |
| 841 |
/// Arg::new("config") |
| 842 |
/// .exclusive(true) |
| 843 |
/// # ; |
| 844 |
/// ``` |
| 845 |
/// |
| 846 |
/// Setting an exclusive argument and having any other arguments present at runtime |
| 847 |
/// is an error. |
| 848 |
/// |
| 849 |
/// ```rust |
| 850 |
/// # use clap_builder as clap; |
| 851 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 852 |
/// let res = Command::new("prog") |
| 853 |
/// .arg(Arg::new("exclusive") |
| 854 |
/// .action(ArgAction::Set) |
| 855 |
/// .exclusive(true) |
| 856 |
/// .long("exclusive")) |
| 857 |
/// .arg(Arg::new("debug") |
| 858 |
/// .long("debug")) |
| 859 |
/// .arg(Arg::new("input")) |
| 860 |
/// .try_get_matches_from(vec![ |
| 861 |
/// "prog", "--exclusive", "file.conf", "file.txt" |
| 862 |
/// ]); |
| 863 |
/// |
| 864 |
/// assert!(res.is_err()); |
| 865 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::ArgumentConflict); |
| 866 |
/// ``` |
| 867 |
#[inline] |
| 868 |
#[must_use] |
| 869 |
pub fn exclusive(self, yes: bool) -> Self { |
| 870 |
if yes { |
| 871 |
self.setting(ArgSettings::Exclusive) |
| 872 |
} else { |
| 873 |
self.unset_setting(ArgSettings::Exclusive) |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
/// Specifies that an argument can be matched to all child [`Subcommand`]s. |
| 878 |
/// |
| 879 |
/// <div class="warning"> |
| 880 |
/// |
| 881 |
/// **NOTE:** Global arguments *only* propagate down, **not** up (to parent commands), however |
| 882 |
/// their values once a user uses them will be propagated back up to parents. In effect, this |
| 883 |
/// means one should *define* all global arguments at the top level, however it doesn't matter |
| 884 |
/// where the user *uses* the global argument. |
| 885 |
/// |
| 886 |
/// </div> |
| 887 |
/// |
| 888 |
/// # Examples |
| 889 |
/// |
| 890 |
/// Assume an application with two subcommands, and you'd like to define a |
| 891 |
/// `--verbose` flag that can be called on any of the subcommands and parent, but you don't |
| 892 |
/// want to clutter the source with three duplicate [`Arg`] definitions. |
| 893 |
/// |
| 894 |
/// ```rust |
| 895 |
/// # use clap_builder as clap; |
| 896 |
/// # use clap::{Command, Arg, ArgAction}; |
| 897 |
/// let m = Command::new("prog") |
| 898 |
/// .arg(Arg::new("verb") |
| 899 |
/// .long("verbose") |
| 900 |
/// .short('v') |
| 901 |
/// .action(ArgAction::SetTrue) |
| 902 |
/// .global(true)) |
| 903 |
/// .subcommand(Command::new("test")) |
| 904 |
/// .subcommand(Command::new("do-stuff")) |
| 905 |
/// .get_matches_from(vec![ |
| 906 |
/// "prog", "do-stuff", "--verbose" |
| 907 |
/// ]); |
| 908 |
/// |
| 909 |
/// assert_eq!(m.subcommand_name(), Some("do-stuff")); |
| 910 |
/// let sub_m = m.subcommand_matches("do-stuff").unwrap(); |
| 911 |
/// assert_eq!(sub_m.get_flag("verb"), true); |
| 912 |
/// ``` |
| 913 |
/// |
| 914 |
/// [`Subcommand`]: crate::Subcommand |
| 915 |
#[inline] |
| 916 |
#[must_use] |
| 917 |
pub fn global(self, yes: bool) -> Self { |
| 918 |
if yes { |
| 919 |
self.setting(ArgSettings::Global) |
| 920 |
} else { |
| 921 |
self.unset_setting(ArgSettings::Global) |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
#[inline] |
| 926 |
pub(crate) fn is_set(&self, s: ArgSettings) -> bool { |
| 927 |
self.settings.is_set(s) |
| 928 |
} |
| 929 |
|
| 930 |
#[inline] |
| 931 |
#[must_use] |
| 932 |
pub(crate) fn setting(mut self, setting: ArgSettings) -> Self { |
| 933 |
self.settings.set(setting); |
| 934 |
self |
| 935 |
} |
| 936 |
|
| 937 |
#[inline] |
| 938 |
#[must_use] |
| 939 |
pub(crate) fn unset_setting(mut self, setting: ArgSettings) -> Self { |
| 940 |
self.settings.unset(setting); |
| 941 |
self |
| 942 |
} |
| 943 |
|
| 944 |
/// Extend [`Arg`] with [`ArgExt`] data |
| 945 |
#[cfg(feature = "unstable-ext")] |
| 946 |
#[allow(clippy::should_implement_trait)] |
| 947 |
pub fn add<T: ArgExt + Extension>(mut self, tagged: T) -> Self { |
| 948 |
self.ext.set(tagged); |
| 949 |
self |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
/// # Value Handling |
| 954 |
impl Arg { |
| 955 |
/// Specify how to react to an argument when parsing it. |
| 956 |
/// |
| 957 |
/// [`ArgAction`] controls things like |
| 958 |
/// - Overwriting previous values with new ones |
| 959 |
/// - Appending new values to all previous ones |
| 960 |
/// - Counting how many times a flag occurs |
| 961 |
/// |
| 962 |
/// The default action is `ArgAction::Set` |
| 963 |
/// |
| 964 |
/// # Examples |
| 965 |
/// |
| 966 |
/// ```rust |
| 967 |
/// # use clap_builder as clap; |
| 968 |
/// # use clap::Command; |
| 969 |
/// # use clap::Arg; |
| 970 |
/// let cmd = Command::new("mycmd") |
| 971 |
/// .arg( |
| 972 |
/// Arg::new("flag") |
| 973 |
/// .long("flag") |
| 974 |
/// .action(clap::ArgAction::Append) |
| 975 |
/// ); |
| 976 |
/// |
| 977 |
/// let matches = cmd.try_get_matches_from(["mycmd", "--flag", "value"]).unwrap(); |
| 978 |
/// assert!(matches.contains_id("flag")); |
| 979 |
/// assert_eq!( |
| 980 |
/// matches.get_many::<String>("flag").unwrap_or_default().map(|v| v.as_str()).collect::<Vec<_>>(), |
| 981 |
/// vec!["value"] |
| 982 |
/// ); |
| 983 |
/// ``` |
| 984 |
#[inline] |
| 985 |
#[must_use] |
| 986 |
pub fn action(mut self, action: impl IntoResettable<ArgAction>) -> Self { |
| 987 |
self.action = action.into_resettable().into_option(); |
| 988 |
self |
| 989 |
} |
| 990 |
|
| 991 |
/// Specify the typed behavior of the argument. |
| 992 |
/// |
| 993 |
/// This allows parsing and validating a value before storing it into |
| 994 |
/// [`ArgMatches`][crate::ArgMatches] as the given type. |
| 995 |
/// |
| 996 |
/// Possible value parsers include: |
| 997 |
/// - [`value_parser!(T)`][crate::value_parser!] for auto-selecting a value parser for a given type |
| 998 |
/// - Or [range expressions like `0..=1`][std::ops::RangeBounds] as a shorthand for [`RangedI64ValueParser`][crate::builder::RangedI64ValueParser] |
| 999 |
/// - `Fn(&str) -> Result<T, E>` |
| 1000 |
/// - `[&str]` and [`PossibleValuesParser`][crate::builder::PossibleValuesParser] for static enumerated values |
| 1001 |
/// - [`BoolishValueParser`][crate::builder::BoolishValueParser], and [`FalseyValueParser`][crate::builder::FalseyValueParser] for alternative `bool` implementations |
| 1002 |
/// - [`NonEmptyStringValueParser`][crate::builder::NonEmptyStringValueParser] for basic validation for strings |
| 1003 |
/// - or any other [`TypedValueParser`][crate::builder::TypedValueParser] implementation |
| 1004 |
/// |
| 1005 |
/// The default value is [`ValueParser::string`][crate::builder::ValueParser::string]. |
| 1006 |
/// |
| 1007 |
/// ```rust |
| 1008 |
/// # use clap_builder as clap; |
| 1009 |
/// # use clap::ArgAction; |
| 1010 |
/// let mut cmd = clap::Command::new("raw") |
| 1011 |
/// .arg( |
| 1012 |
/// clap::Arg::new("color") |
| 1013 |
/// .long("color") |
| 1014 |
/// .value_parser(["always", "auto", "never"]) |
| 1015 |
/// .default_value("auto") |
| 1016 |
/// ) |
| 1017 |
/// .arg( |
| 1018 |
/// clap::Arg::new("hostname") |
| 1019 |
/// .long("hostname") |
| 1020 |
/// .value_parser(clap::builder::NonEmptyStringValueParser::new()) |
| 1021 |
/// .action(ArgAction::Set) |
| 1022 |
/// .required(true) |
| 1023 |
/// ) |
| 1024 |
/// .arg( |
| 1025 |
/// clap::Arg::new("port") |
| 1026 |
/// .long("port") |
| 1027 |
/// .value_parser(clap::value_parser!(u16).range(3000..)) |
| 1028 |
/// .action(ArgAction::Set) |
| 1029 |
/// .required(true) |
| 1030 |
/// ); |
| 1031 |
/// |
| 1032 |
/// let m = cmd.try_get_matches_from_mut( |
| 1033 |
/// ["cmd", "--hostname", "rust-lang.org", "--port", "3001"] |
| 1034 |
/// ).unwrap(); |
| 1035 |
/// |
| 1036 |
/// let color: &String = m.get_one("color") |
| 1037 |
/// .expect("default"); |
| 1038 |
/// assert_eq!(color, "auto"); |
| 1039 |
/// |
| 1040 |
/// let hostname: &String = m.get_one("hostname") |
| 1041 |
/// .expect("required"); |
| 1042 |
/// assert_eq!(hostname, "rust-lang.org"); |
| 1043 |
/// |
| 1044 |
/// let port: u16 = *m.get_one("port") |
| 1045 |
/// .expect("required"); |
| 1046 |
/// assert_eq!(port, 3001); |
| 1047 |
/// ``` |
| 1048 |
pub fn value_parser(mut self, parser: impl IntoResettable<super::ValueParser>) -> Self { |
| 1049 |
self.value_parser = parser.into_resettable().into_option(); |
| 1050 |
self |
| 1051 |
} |
| 1052 |
|
| 1053 |
/// Specifies the number of arguments parsed per occurrence |
| 1054 |
/// |
| 1055 |
/// For example, if you had a `-f <file>` argument where you wanted exactly 3 'files' you would |
| 1056 |
/// set `.num_args(3)`, and this argument wouldn't be satisfied unless the user |
| 1057 |
/// provided 3 and only 3 values. |
| 1058 |
/// |
| 1059 |
/// Users may specify values for arguments in any of the following methods |
| 1060 |
/// |
| 1061 |
/// - Using a space such as `-o value` or `--option value` |
| 1062 |
/// - Using an equals and no space such as `-o=value` or `--option=value` |
| 1063 |
/// - Use a short and no space such as `-ovalue` |
| 1064 |
/// |
| 1065 |
/// <div class="warning"> |
| 1066 |
/// |
| 1067 |
/// **WARNING:** |
| 1068 |
/// |
| 1069 |
/// Setting a variable number of values (e.g. `1..=10`) for an argument without |
| 1070 |
/// other details can be dangerous in some circumstances. Because multiple values are |
| 1071 |
/// allowed, `--option val1 val2 val3` is perfectly valid. Be careful when designing a CLI |
| 1072 |
/// where **positional arguments** or **subcommands** are *also* expected as `clap` will continue |
| 1073 |
/// parsing *values* until one of the following happens: |
| 1074 |
/// |
| 1075 |
/// - It reaches the maximum number of values |
| 1076 |
/// - It reaches a specific number of values |
| 1077 |
/// - It finds another flag or option (i.e. something that starts with a `-`) |
| 1078 |
/// - It reaches the [`Arg::value_terminator`] if set |
| 1079 |
/// |
| 1080 |
/// Alternatively, |
| 1081 |
/// - Use a delimiter between values with [`Arg::value_delimiter`] |
| 1082 |
/// - Require a flag occurrence per value with [`ArgAction::Append`] |
| 1083 |
/// - Require positional arguments to appear after `--` with [`Arg::last`] |
| 1084 |
/// |
| 1085 |
/// </div> |
| 1086 |
/// |
| 1087 |
/// # Examples |
| 1088 |
/// |
| 1089 |
/// Option: |
| 1090 |
/// ```rust |
| 1091 |
/// # use clap_builder as clap; |
| 1092 |
/// # use clap::{Command, Arg}; |
| 1093 |
/// let m = Command::new("prog") |
| 1094 |
/// .arg(Arg::new("mode") |
| 1095 |
/// .long("mode") |
| 1096 |
/// .num_args(1)) |
| 1097 |
/// .get_matches_from(vec![ |
| 1098 |
/// "prog", "--mode", "fast" |
| 1099 |
/// ]); |
| 1100 |
/// |
| 1101 |
/// assert_eq!(m.get_one::<String>("mode").unwrap(), "fast"); |
| 1102 |
/// ``` |
| 1103 |
/// |
| 1104 |
/// Flag/option hybrid (see also [`default_missing_value`][Arg::default_missing_value]) |
| 1105 |
/// ```rust |
| 1106 |
/// # use clap_builder as clap; |
| 1107 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 1108 |
/// let cmd = Command::new("prog") |
| 1109 |
/// .arg(Arg::new("mode") |
| 1110 |
/// .long("mode") |
| 1111 |
/// .default_missing_value("slow") |
| 1112 |
/// .default_value("plaid") |
| 1113 |
/// .num_args(0..=1)); |
| 1114 |
/// |
| 1115 |
/// let m = cmd.clone() |
| 1116 |
/// .get_matches_from(vec![ |
| 1117 |
/// "prog", "--mode", "fast" |
| 1118 |
/// ]); |
| 1119 |
/// assert_eq!(m.get_one::<String>("mode").unwrap(), "fast"); |
| 1120 |
/// |
| 1121 |
/// let m = cmd.clone() |
| 1122 |
/// .get_matches_from(vec![ |
| 1123 |
/// "prog", "--mode", |
| 1124 |
/// ]); |
| 1125 |
/// assert_eq!(m.get_one::<String>("mode").unwrap(), "slow"); |
| 1126 |
/// |
| 1127 |
/// let m = cmd.clone() |
| 1128 |
/// .get_matches_from(vec![ |
| 1129 |
/// "prog", |
| 1130 |
/// ]); |
| 1131 |
/// assert_eq!(m.get_one::<String>("mode").unwrap(), "plaid"); |
| 1132 |
/// ``` |
| 1133 |
/// |
| 1134 |
/// Tuples |
| 1135 |
/// ```rust |
| 1136 |
/// # use clap_builder as clap; |
| 1137 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 1138 |
/// let cmd = Command::new("prog") |
| 1139 |
/// .arg(Arg::new("file") |
| 1140 |
/// .action(ArgAction::Set) |
| 1141 |
/// .num_args(2) |
| 1142 |
/// .short('F')); |
| 1143 |
/// |
| 1144 |
/// let m = cmd.clone() |
| 1145 |
/// .get_matches_from(vec![ |
| 1146 |
/// "prog", "-F", "in-file", "out-file" |
| 1147 |
/// ]); |
| 1148 |
/// assert_eq!( |
| 1149 |
/// m.get_many::<String>("file").unwrap_or_default().map(|v| v.as_str()).collect::<Vec<_>>(), |
| 1150 |
/// vec!["in-file", "out-file"] |
| 1151 |
/// ); |
| 1152 |
/// |
| 1153 |
/// let res = cmd.clone() |
| 1154 |
/// .try_get_matches_from(vec![ |
| 1155 |
/// "prog", "-F", "file1" |
| 1156 |
/// ]); |
| 1157 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::WrongNumberOfValues); |
| 1158 |
/// ``` |
| 1159 |
/// |
| 1160 |
/// A common mistake is to define an option which allows multiple values and a positional |
| 1161 |
/// argument. |
| 1162 |
/// ```rust |
| 1163 |
/// # use clap_builder as clap; |
| 1164 |
/// # use clap::{Command, Arg, ArgAction}; |
| 1165 |
/// let cmd = Command::new("prog") |
| 1166 |
/// .arg(Arg::new("file") |
| 1167 |
/// .action(ArgAction::Set) |
| 1168 |
/// .num_args(0..) |
| 1169 |
/// .short('F')) |
| 1170 |
/// .arg(Arg::new("word")); |
| 1171 |
/// |
| 1172 |
/// let m = cmd.clone().get_matches_from(vec![ |
| 1173 |
/// "prog", "-F", "file1", "file2", "file3", "word" |
| 1174 |
/// ]); |
| 1175 |
/// let files: Vec<_> = m.get_many::<String>("file").unwrap().collect(); |
| 1176 |
/// assert_eq!(files, ["file1", "file2", "file3", "word"]); // wait...what?! |
| 1177 |
/// assert!(!m.contains_id("word")); // but we clearly used word! |
| 1178 |
/// |
| 1179 |
/// // but this works |
| 1180 |
/// let m = cmd.clone().get_matches_from(vec![ |
| 1181 |
/// "prog", "word", "-F", "file1", "file2", "file3", |
| 1182 |
/// ]); |
| 1183 |
/// let files: Vec<_> = m.get_many::<String>("file").unwrap().collect(); |
| 1184 |
/// assert_eq!(files, ["file1", "file2", "file3"]); |
| 1185 |
/// assert_eq!(m.get_one::<String>("word").unwrap(), "word"); |
| 1186 |
/// ``` |
| 1187 |
/// The problem is `clap` doesn't know when to stop parsing values for "file". |
| 1188 |
/// |
| 1189 |
/// A solution for the example above is to limit how many values with a maximum, or specific |
| 1190 |
/// number, or to say [`ArgAction::Append`] is ok, but multiple values are not. |
| 1191 |
/// ```rust |
| 1192 |
/// # use clap_builder as clap; |
| 1193 |
/// # use clap::{Command, Arg, ArgAction}; |
| 1194 |
/// let m = Command::new("prog") |
| 1195 |
/// .arg(Arg::new("file") |
| 1196 |
/// .action(ArgAction::Append) |
| 1197 |
/// .short('F')) |
| 1198 |
/// .arg(Arg::new("word")) |
| 1199 |
/// .get_matches_from(vec![ |
| 1200 |
/// "prog", "-F", "file1", "-F", "file2", "-F", "file3", "word" |
| 1201 |
/// ]); |
| 1202 |
/// |
| 1203 |
/// let files: Vec<_> = m.get_many::<String>("file").unwrap().collect(); |
| 1204 |
/// assert_eq!(files, ["file1", "file2", "file3"]); |
| 1205 |
/// assert_eq!(m.get_one::<String>("word").unwrap(), "word"); |
| 1206 |
/// ``` |
| 1207 |
#[inline] |
| 1208 |
#[must_use] |
| 1209 |
pub fn num_args(mut self, qty: impl IntoResettable<ValueRange>) -> Self { |
| 1210 |
self.num_vals = qty.into_resettable().into_option(); |
| 1211 |
self |
| 1212 |
} |
| 1213 |
|
| 1214 |
#[doc(hidden)] |
| 1215 |
#[cfg_attr( |
| 1216 |
feature = "deprecated", |
| 1217 |
deprecated(since = "4.0.0", note = "Replaced with `Arg::num_args`") |
| 1218 |
)] |
| 1219 |
pub fn number_of_values(self, qty: usize) -> Self { |
| 1220 |
self.num_args(qty) |
| 1221 |
} |
| 1222 |
|
| 1223 |
/// Placeholder for the argument's value in the help message / usage. |
| 1224 |
/// |
| 1225 |
/// This name is cosmetic only; the name is **not** used to access arguments. |
| 1226 |
/// This setting can be very helpful when describing the type of input the user should be |
| 1227 |
/// using, such as `FILE`, `INTERFACE`, etc. Although not required, it's somewhat convention to |
| 1228 |
/// use all capital letters for the value name. |
| 1229 |
/// |
| 1230 |
/// <div class="warning"> |
| 1231 |
/// |
| 1232 |
/// **NOTE:** implicitly sets [`Arg::action(ArgAction::Set)`] |
| 1233 |
/// |
| 1234 |
/// </div> |
| 1235 |
/// |
| 1236 |
/// # Examples |
| 1237 |
/// |
| 1238 |
/// ```rust |
| 1239 |
/// # use clap_builder as clap; |
| 1240 |
/// # use clap::{Command, Arg}; |
| 1241 |
/// Arg::new("cfg") |
| 1242 |
/// .long("config") |
| 1243 |
/// .value_name("FILE") |
| 1244 |
/// # ; |
| 1245 |
/// ``` |
| 1246 |
/// |
| 1247 |
/// ```rust |
| 1248 |
/// # use clap_builder as clap; |
| 1249 |
/// # #[cfg(feature = "help")] { |
| 1250 |
/// # use clap::{Command, Arg}; |
| 1251 |
/// let m = Command::new("prog") |
| 1252 |
/// .arg(Arg::new("config") |
| 1253 |
/// .long("config") |
| 1254 |
/// .value_name("FILE") |
| 1255 |
/// .help("Some help text")) |
| 1256 |
/// .get_matches_from(vec![ |
| 1257 |
/// "prog", "--help" |
| 1258 |
/// ]); |
| 1259 |
/// # } |
| 1260 |
/// ``` |
| 1261 |
/// Running the above program produces the following output |
| 1262 |
/// |
| 1263 |
/// ```text |
| 1264 |
/// valnames |
| 1265 |
/// |
| 1266 |
/// Usage: valnames [OPTIONS] |
| 1267 |
/// |
| 1268 |
/// Options: |
| 1269 |
/// --config <FILE> Some help text |
| 1270 |
/// -h, --help Print help information |
| 1271 |
/// -V, --version Print version information |
| 1272 |
/// ``` |
| 1273 |
/// [positional]: Arg::index() |
| 1274 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 1275 |
#[inline] |
| 1276 |
#[must_use] |
| 1277 |
pub fn value_name(mut self, name: impl IntoResettable<Str>) -> Self { |
| 1278 |
if let Some(name) = name.into_resettable().into_option() { |
| 1279 |
self.value_names([name]) |
| 1280 |
} else { |
| 1281 |
self.val_names.clear(); |
| 1282 |
self |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
/// Placeholders for the argument's values in the help message / usage. |
| 1287 |
/// |
| 1288 |
/// These names are cosmetic only, used for help and usage strings only. The names are **not** |
| 1289 |
/// used to access arguments. The values of the arguments are accessed in numeric order (i.e. |
| 1290 |
/// if you specify two names `one` and `two` `one` will be the first matched value, `two` will |
| 1291 |
/// be the second). |
| 1292 |
/// |
| 1293 |
/// This setting can be very helpful when describing the type of input the user should be |
| 1294 |
/// using, such as `FILE`, `INTERFACE`, etc. Although not required, it's somewhat convention to |
| 1295 |
/// use all capital letters for the value name. |
| 1296 |
/// |
| 1297 |
/// <div class="warning"> |
| 1298 |
/// |
| 1299 |
/// **TIP:** It may help to use [`Arg::next_line_help(true)`] if there are long, or |
| 1300 |
/// multiple value names in order to not throw off the help text alignment of all options. |
| 1301 |
/// |
| 1302 |
/// </div> |
| 1303 |
/// |
| 1304 |
/// <div class="warning"> |
| 1305 |
/// |
| 1306 |
/// **NOTE:** implicitly sets [`Arg::action(ArgAction::Set)`] and [`Arg::num_args(1..)`]. |
| 1307 |
/// |
| 1308 |
/// </div> |
| 1309 |
/// |
| 1310 |
/// # Examples |
| 1311 |
/// |
| 1312 |
/// ```rust |
| 1313 |
/// # use clap_builder as clap; |
| 1314 |
/// # use clap::{Command, Arg}; |
| 1315 |
/// Arg::new("speed") |
| 1316 |
/// .short('s') |
| 1317 |
/// .value_names(["fast", "slow"]); |
| 1318 |
/// ``` |
| 1319 |
/// |
| 1320 |
/// ```rust |
| 1321 |
/// # use clap_builder as clap; |
| 1322 |
/// # #[cfg(feature = "help")] { |
| 1323 |
/// # use clap::{Command, Arg}; |
| 1324 |
/// let m = Command::new("prog") |
| 1325 |
/// .arg(Arg::new("io") |
| 1326 |
/// .long("io-files") |
| 1327 |
/// .value_names(["INFILE", "OUTFILE"])) |
| 1328 |
/// .get_matches_from(vec![ |
| 1329 |
/// "prog", "--help" |
| 1330 |
/// ]); |
| 1331 |
/// # } |
| 1332 |
/// ``` |
| 1333 |
/// |
| 1334 |
/// Running the above program produces the following output |
| 1335 |
/// |
| 1336 |
/// ```text |
| 1337 |
/// valnames |
| 1338 |
/// |
| 1339 |
/// Usage: valnames [OPTIONS] |
| 1340 |
/// |
| 1341 |
/// Options: |
| 1342 |
/// -h, --help Print help information |
| 1343 |
/// --io-files <INFILE> <OUTFILE> Some help text |
| 1344 |
/// -V, --version Print version information |
| 1345 |
/// ``` |
| 1346 |
/// [`Arg::next_line_help(true)`]: Arg::next_line_help() |
| 1347 |
/// [`Arg::num_args`]: Arg::num_args() |
| 1348 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 1349 |
/// [`Arg::num_args(1..)`]: Arg::num_args() |
| 1350 |
#[must_use] |
| 1351 |
pub fn value_names(mut self, names: impl IntoIterator<Item = impl Into<Str>>) -> Self { |
| 1352 |
self.val_names = names.into_iter().map(|s| s.into()).collect(); |
| 1353 |
self |
| 1354 |
} |
| 1355 |
|
| 1356 |
/// Provide the shell a hint about how to complete this argument. |
| 1357 |
/// |
| 1358 |
/// See [`ValueHint`] for more information. |
| 1359 |
/// |
| 1360 |
/// <div class="warning"> |
| 1361 |
/// |
| 1362 |
/// **NOTE:** implicitly sets [`Arg::action(ArgAction::Set)`][ArgAction::Set]. |
| 1363 |
/// |
| 1364 |
/// </div> |
| 1365 |
/// |
| 1366 |
/// For example, to take a username as argument: |
| 1367 |
/// |
| 1368 |
/// ```rust |
| 1369 |
/// # use clap_builder as clap; |
| 1370 |
/// # use clap::{Arg, ValueHint}; |
| 1371 |
/// Arg::new("user") |
| 1372 |
/// .short('u') |
| 1373 |
/// .long("user") |
| 1374 |
/// .value_hint(ValueHint::Username); |
| 1375 |
/// ``` |
| 1376 |
/// |
| 1377 |
/// To take a full command line and its arguments (for example, when writing a command wrapper): |
| 1378 |
/// |
| 1379 |
/// ```rust |
| 1380 |
/// # use clap_builder as clap; |
| 1381 |
/// # use clap::{Command, Arg, ValueHint, ArgAction}; |
| 1382 |
/// Command::new("prog") |
| 1383 |
/// .trailing_var_arg(true) |
| 1384 |
/// .arg( |
| 1385 |
/// Arg::new("command") |
| 1386 |
/// .action(ArgAction::Set) |
| 1387 |
/// .num_args(1..) |
| 1388 |
/// .value_hint(ValueHint::CommandWithArguments) |
| 1389 |
/// ); |
| 1390 |
/// ``` |
| 1391 |
#[must_use] |
| 1392 |
pub fn value_hint(mut self, value_hint: impl IntoResettable<ValueHint>) -> Self { |
| 1393 |
// HACK: we should use `Self::add` and `Self::remove` to type-check that `ArgExt` is used |
| 1394 |
match value_hint.into_resettable().into_option() { |
| 1395 |
Some(value_hint) => { |
| 1396 |
self.ext.set(value_hint); |
| 1397 |
} |
| 1398 |
None => { |
| 1399 |
self.ext.remove::<ValueHint>(); |
| 1400 |
} |
| 1401 |
} |
| 1402 |
self |
| 1403 |
} |
| 1404 |
|
| 1405 |
/// Match values against [`PossibleValuesParser`][crate::builder::PossibleValuesParser] without matching case. |
| 1406 |
/// |
| 1407 |
/// When other arguments are conditionally required based on the |
| 1408 |
/// value of a case-insensitive argument, the equality check done |
| 1409 |
/// by [`Arg::required_if_eq`], [`Arg::required_if_eq_any`], or |
| 1410 |
/// [`Arg::required_if_eq_all`] is case-insensitive. |
| 1411 |
/// |
| 1412 |
/// |
| 1413 |
/// <div class="warning"> |
| 1414 |
/// |
| 1415 |
/// **NOTE:** Setting this requires [taking values][Arg::num_args] |
| 1416 |
/// |
| 1417 |
/// </div> |
| 1418 |
/// |
| 1419 |
/// <div class="warning"> |
| 1420 |
/// |
| 1421 |
/// **NOTE:** To do unicode case folding, enable the `unicode` feature flag. |
| 1422 |
/// |
| 1423 |
/// </div> |
| 1424 |
/// |
| 1425 |
/// # Examples |
| 1426 |
/// |
| 1427 |
/// ```rust |
| 1428 |
/// # use clap_builder as clap; |
| 1429 |
/// # use clap::{Command, Arg, ArgAction}; |
| 1430 |
/// let m = Command::new("pv") |
| 1431 |
/// .arg(Arg::new("option") |
| 1432 |
/// .long("option") |
| 1433 |
/// .action(ArgAction::Set) |
| 1434 |
/// .ignore_case(true) |
| 1435 |
/// .value_parser(["test123"])) |
| 1436 |
/// .get_matches_from(vec![ |
| 1437 |
/// "pv", "--option", "TeSt123", |
| 1438 |
/// ]); |
| 1439 |
/// |
| 1440 |
/// assert!(m.get_one::<String>("option").unwrap().eq_ignore_ascii_case("test123")); |
| 1441 |
/// ``` |
| 1442 |
/// |
| 1443 |
/// This setting also works when multiple values can be defined: |
| 1444 |
/// |
| 1445 |
/// ```rust |
| 1446 |
/// # use clap_builder as clap; |
| 1447 |
/// # use clap::{Command, Arg, ArgAction}; |
| 1448 |
/// let m = Command::new("pv") |
| 1449 |
/// .arg(Arg::new("option") |
| 1450 |
/// .short('o') |
| 1451 |
/// .long("option") |
| 1452 |
/// .action(ArgAction::Set) |
| 1453 |
/// .ignore_case(true) |
| 1454 |
/// .num_args(1..) |
| 1455 |
/// .value_parser(["test123", "test321"])) |
| 1456 |
/// .get_matches_from(vec![ |
| 1457 |
/// "pv", "--option", "TeSt123", "teST123", "tESt321" |
| 1458 |
/// ]); |
| 1459 |
/// |
| 1460 |
/// let matched_vals = m.get_many::<String>("option").unwrap().collect::<Vec<_>>(); |
| 1461 |
/// assert_eq!(&*matched_vals, &["TeSt123", "teST123", "tESt321"]); |
| 1462 |
/// ``` |
| 1463 |
#[inline] |
| 1464 |
#[must_use] |
| 1465 |
pub fn ignore_case(self, yes: bool) -> Self { |
| 1466 |
if yes { |
| 1467 |
self.setting(ArgSettings::IgnoreCase) |
| 1468 |
} else { |
| 1469 |
self.unset_setting(ArgSettings::IgnoreCase) |
| 1470 |
} |
| 1471 |
} |
| 1472 |
|
| 1473 |
/// Allows values which start with a leading hyphen (`-`) |
| 1474 |
/// |
| 1475 |
/// To limit values to just numbers, see |
| 1476 |
/// [`allow_negative_numbers`][Arg::allow_negative_numbers]. |
| 1477 |
/// |
| 1478 |
/// See also [`trailing_var_arg`][Arg::trailing_var_arg]. |
| 1479 |
/// |
| 1480 |
/// <div class="warning"> |
| 1481 |
/// |
| 1482 |
/// **NOTE:** Setting this requires [taking values][Arg::num_args] |
| 1483 |
/// |
| 1484 |
/// </div> |
| 1485 |
/// |
| 1486 |
/// <div class="warning"> |
| 1487 |
/// |
| 1488 |
/// **WARNING:** Prior arguments with `allow_hyphen_values(true)` get precedence over known |
| 1489 |
/// flags but known flags get precedence over the next possible positional argument with |
| 1490 |
/// `allow_hyphen_values(true)`. When combined with [`Arg::num_args(..)`][Arg::num_args], |
| 1491 |
/// [`Arg::value_terminator`] is one way to ensure processing stops. |
| 1492 |
/// |
| 1493 |
/// </div> |
| 1494 |
/// |
| 1495 |
/// <div class="warning"> |
| 1496 |
/// |
| 1497 |
/// **WARNING**: Take caution when using this setting combined with another argument using |
| 1498 |
/// [`Arg::num_args`], as this becomes ambiguous `$ prog --arg -- -- val`. All |
| 1499 |
/// three `--, --, val` will be values when the user may have thought the second `--` would |
| 1500 |
/// constitute the normal, "Only positional args follow" idiom. |
| 1501 |
/// |
| 1502 |
/// </div> |
| 1503 |
/// |
| 1504 |
/// # Examples |
| 1505 |
/// |
| 1506 |
/// ```rust |
| 1507 |
/// # use clap_builder as clap; |
| 1508 |
/// # use clap::{Command, Arg, ArgAction}; |
| 1509 |
/// let m = Command::new("prog") |
| 1510 |
/// .arg(Arg::new("pat") |
| 1511 |
/// .action(ArgAction::Set) |
| 1512 |
/// .allow_hyphen_values(true) |
| 1513 |
/// .long("pattern")) |
| 1514 |
/// .get_matches_from(vec![ |
| 1515 |
/// "prog", "--pattern", "-file" |
| 1516 |
/// ]); |
| 1517 |
/// |
| 1518 |
/// assert_eq!(m.get_one::<String>("pat").unwrap(), "-file"); |
| 1519 |
/// ``` |
| 1520 |
/// |
| 1521 |
/// Not setting `Arg::allow_hyphen_values(true)` and supplying a value which starts with a |
| 1522 |
/// hyphen is an error. |
| 1523 |
/// |
| 1524 |
/// ```rust |
| 1525 |
/// # use clap_builder as clap; |
| 1526 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 1527 |
/// let res = Command::new("prog") |
| 1528 |
/// .arg(Arg::new("pat") |
| 1529 |
/// .action(ArgAction::Set) |
| 1530 |
/// .long("pattern")) |
| 1531 |
/// .try_get_matches_from(vec![ |
| 1532 |
/// "prog", "--pattern", "-file" |
| 1533 |
/// ]); |
| 1534 |
/// |
| 1535 |
/// assert!(res.is_err()); |
| 1536 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument); |
| 1537 |
/// ``` |
| 1538 |
/// [`Arg::num_args(1)`]: Arg::num_args() |
| 1539 |
#[inline] |
| 1540 |
#[must_use] |
| 1541 |
pub fn allow_hyphen_values(self, yes: bool) -> Self { |
| 1542 |
if yes { |
| 1543 |
self.setting(ArgSettings::AllowHyphenValues) |
| 1544 |
} else { |
| 1545 |
self.unset_setting(ArgSettings::AllowHyphenValues) |
| 1546 |
} |
| 1547 |
} |
| 1548 |
|
| 1549 |
/// Allows negative numbers to pass as values. |
| 1550 |
/// |
| 1551 |
/// This is similar to [`Arg::allow_hyphen_values`] except that it only allows numbers, |
| 1552 |
/// all other undefined leading hyphens will fail to parse. |
| 1553 |
/// |
| 1554 |
/// <div class="warning"> |
| 1555 |
/// |
| 1556 |
/// **NOTE:** Setting this requires [taking values][Arg::num_args] |
| 1557 |
/// |
| 1558 |
/// </div> |
| 1559 |
/// |
| 1560 |
/// # Examples |
| 1561 |
/// |
| 1562 |
/// ```rust |
| 1563 |
/// # use clap_builder as clap; |
| 1564 |
/// # use clap::{Command, Arg}; |
| 1565 |
/// let res = Command::new("myprog") |
| 1566 |
/// .arg(Arg::new("num").allow_negative_numbers(true)) |
| 1567 |
/// .try_get_matches_from(vec![ |
| 1568 |
/// "myprog", "-20" |
| 1569 |
/// ]); |
| 1570 |
/// assert!(res.is_ok()); |
| 1571 |
/// let m = res.unwrap(); |
| 1572 |
/// assert_eq!(m.get_one::<String>("num").unwrap(), "-20"); |
| 1573 |
/// ``` |
| 1574 |
#[inline] |
| 1575 |
pub fn allow_negative_numbers(self, yes: bool) -> Self { |
| 1576 |
if yes { |
| 1577 |
self.setting(ArgSettings::AllowNegativeNumbers) |
| 1578 |
} else { |
| 1579 |
self.unset_setting(ArgSettings::AllowNegativeNumbers) |
| 1580 |
} |
| 1581 |
} |
| 1582 |
|
| 1583 |
/// Requires that options use the `--option=val` syntax |
| 1584 |
/// |
| 1585 |
/// i.e. an equals between the option and associated value. |
| 1586 |
/// |
| 1587 |
/// <div class="warning"> |
| 1588 |
/// |
| 1589 |
/// **NOTE:** Setting this requires [taking values][Arg::num_args] |
| 1590 |
/// |
| 1591 |
/// </div> |
| 1592 |
/// |
| 1593 |
/// # Examples |
| 1594 |
/// |
| 1595 |
/// Setting `require_equals` requires that the option have an equals sign between |
| 1596 |
/// it and the associated value. |
| 1597 |
/// |
| 1598 |
/// ```rust |
| 1599 |
/// # use clap_builder as clap; |
| 1600 |
/// # use clap::{Command, Arg, ArgAction}; |
| 1601 |
/// let res = Command::new("prog") |
| 1602 |
/// .arg(Arg::new("cfg") |
| 1603 |
/// .action(ArgAction::Set) |
| 1604 |
/// .require_equals(true) |
| 1605 |
/// .long("config")) |
| 1606 |
/// .try_get_matches_from(vec![ |
| 1607 |
/// "prog", "--config=file.conf" |
| 1608 |
/// ]); |
| 1609 |
/// |
| 1610 |
/// assert!(res.is_ok()); |
| 1611 |
/// ``` |
| 1612 |
/// |
| 1613 |
/// Setting `require_equals` and *not* supplying the equals will cause an |
| 1614 |
/// error. |
| 1615 |
/// |
| 1616 |
/// ```rust |
| 1617 |
/// # use clap_builder as clap; |
| 1618 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 1619 |
/// let res = Command::new("prog") |
| 1620 |
/// .arg(Arg::new("cfg") |
| 1621 |
/// .action(ArgAction::Set) |
| 1622 |
/// .require_equals(true) |
| 1623 |
/// .long("config")) |
| 1624 |
/// .try_get_matches_from(vec![ |
| 1625 |
/// "prog", "--config", "file.conf" |
| 1626 |
/// ]); |
| 1627 |
/// |
| 1628 |
/// assert!(res.is_err()); |
| 1629 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::NoEquals); |
| 1630 |
/// ``` |
| 1631 |
#[inline] |
| 1632 |
#[must_use] |
| 1633 |
pub fn require_equals(self, yes: bool) -> Self { |
| 1634 |
if yes { |
| 1635 |
self.setting(ArgSettings::RequireEquals) |
| 1636 |
} else { |
| 1637 |
self.unset_setting(ArgSettings::RequireEquals) |
| 1638 |
} |
| 1639 |
} |
| 1640 |
|
| 1641 |
#[doc(hidden)] |
| 1642 |
#[cfg_attr( |
| 1643 |
feature = "deprecated", |
| 1644 |
deprecated(since = "4.0.0", note = "Replaced with `Arg::value_delimiter`") |
| 1645 |
)] |
| 1646 |
pub fn use_value_delimiter(mut self, yes: bool) -> Self { |
| 1647 |
if yes { |
| 1648 |
self.val_delim.get_or_insert(','); |
| 1649 |
} else { |
| 1650 |
self.val_delim = None; |
| 1651 |
} |
| 1652 |
self |
| 1653 |
} |
| 1654 |
|
| 1655 |
/// Allow grouping of multiple values via a delimiter. |
| 1656 |
/// |
| 1657 |
/// i.e. allow values (`val1,val2,val3`) to be parsed as three values (`val1`, `val2`, |
| 1658 |
/// and `val3`) instead of one value (`val1,val2,val3`). |
| 1659 |
/// |
| 1660 |
/// See also [`Command::dont_delimit_trailing_values`][crate::Command::dont_delimit_trailing_values]. |
| 1661 |
/// |
| 1662 |
/// # Examples |
| 1663 |
/// |
| 1664 |
/// ```rust |
| 1665 |
/// # use clap_builder as clap; |
| 1666 |
/// # use clap::{Command, Arg}; |
| 1667 |
/// let m = Command::new("prog") |
| 1668 |
/// .arg(Arg::new("config") |
| 1669 |
/// .short('c') |
| 1670 |
/// .long("config") |
| 1671 |
/// .value_delimiter(',')) |
| 1672 |
/// .get_matches_from(vec![ |
| 1673 |
/// "prog", "--config=val1,val2,val3" |
| 1674 |
/// ]); |
| 1675 |
/// |
| 1676 |
/// assert_eq!(m.get_many::<String>("config").unwrap().collect::<Vec<_>>(), ["val1", "val2", "val3"]) |
| 1677 |
/// ``` |
| 1678 |
/// [`Arg::value_delimiter(',')`]: Arg::value_delimiter() |
| 1679 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 1680 |
#[inline] |
| 1681 |
#[must_use] |
| 1682 |
pub fn value_delimiter(mut self, d: impl IntoResettable<char>) -> Self { |
| 1683 |
self.val_delim = d.into_resettable().into_option(); |
| 1684 |
self |
| 1685 |
} |
| 1686 |
|
| 1687 |
/// Sentinel to **stop** parsing multiple values of a given argument. |
| 1688 |
/// |
| 1689 |
/// By default when |
| 1690 |
/// one sets [`num_args(1..)`] on an argument, clap will continue parsing values for that |
| 1691 |
/// argument until it reaches another valid argument, or one of the other more specific settings |
| 1692 |
/// for multiple values is used (such as [`num_args`]). |
| 1693 |
/// |
| 1694 |
/// <div class="warning"> |
| 1695 |
/// |
| 1696 |
/// **NOTE:** This setting only applies to [options] and [positional arguments] |
| 1697 |
/// |
| 1698 |
/// </div> |
| 1699 |
/// |
| 1700 |
/// <div class="warning"> |
| 1701 |
/// |
| 1702 |
/// **NOTE:** When the terminator is passed in on the command line, it is **not** stored as one |
| 1703 |
/// of the values |
| 1704 |
/// |
| 1705 |
/// </div> |
| 1706 |
/// |
| 1707 |
/// # Examples |
| 1708 |
/// |
| 1709 |
/// ```rust |
| 1710 |
/// # use clap_builder as clap; |
| 1711 |
/// # use clap::{Command, Arg, ArgAction}; |
| 1712 |
/// Arg::new("vals") |
| 1713 |
/// .action(ArgAction::Set) |
| 1714 |
/// .num_args(1..) |
| 1715 |
/// .value_terminator(";") |
| 1716 |
/// # ; |
| 1717 |
/// ``` |
| 1718 |
/// |
| 1719 |
/// The following example uses two arguments, a sequence of commands, and the location in which |
| 1720 |
/// to perform them |
| 1721 |
/// |
| 1722 |
/// ```rust |
| 1723 |
/// # use clap_builder as clap; |
| 1724 |
/// # use clap::{Command, Arg, ArgAction}; |
| 1725 |
/// let m = Command::new("prog") |
| 1726 |
/// .arg(Arg::new("cmds") |
| 1727 |
/// .action(ArgAction::Set) |
| 1728 |
/// .num_args(1..) |
| 1729 |
/// .allow_hyphen_values(true) |
| 1730 |
/// .value_terminator(";")) |
| 1731 |
/// .arg(Arg::new("location")) |
| 1732 |
/// .get_matches_from(vec![ |
| 1733 |
/// "prog", "find", "-type", "f", "-name", "special", ";", "/home/clap" |
| 1734 |
/// ]); |
| 1735 |
/// let cmds: Vec<_> = m.get_many::<String>("cmds").unwrap().collect(); |
| 1736 |
/// assert_eq!(&cmds, &["find", "-type", "f", "-name", "special"]); |
| 1737 |
/// assert_eq!(m.get_one::<String>("location").unwrap(), "/home/clap"); |
| 1738 |
/// ``` |
| 1739 |
/// [options]: Arg::action |
| 1740 |
/// [positional arguments]: Arg::index() |
| 1741 |
/// [`num_args(1..)`]: Arg::num_args() |
| 1742 |
/// [`num_args`]: Arg::num_args() |
| 1743 |
#[inline] |
| 1744 |
#[must_use] |
| 1745 |
pub fn value_terminator(mut self, term: impl IntoResettable<Str>) -> Self { |
| 1746 |
self.terminator = term.into_resettable().into_option(); |
| 1747 |
self |
| 1748 |
} |
| 1749 |
|
| 1750 |
/// Consume all following arguments. |
| 1751 |
/// |
| 1752 |
/// Do not parse them individually, but rather pass them in entirety. |
| 1753 |
/// |
| 1754 |
/// It is worth noting that setting this requires all values to come after a `--` to indicate |
| 1755 |
/// they should all be captured. For example: |
| 1756 |
/// |
| 1757 |
/// ```text |
| 1758 |
/// --foo something -- -v -v -v -b -b -b --baz -q -u -x |
| 1759 |
/// ``` |
| 1760 |
/// |
| 1761 |
/// Will result in everything after `--` to be considered one raw argument. This behavior |
| 1762 |
/// may not be exactly what you are expecting and using [`Arg::trailing_var_arg`] |
| 1763 |
/// may be more appropriate. |
| 1764 |
/// |
| 1765 |
/// <div class="warning"> |
| 1766 |
/// |
| 1767 |
/// **NOTE:** Implicitly sets [`Arg::action(ArgAction::Set)`], [`Arg::num_args(1..)`], |
| 1768 |
/// [`Arg::allow_hyphen_values(true)`], and [`Arg::last(true)`] when set to `true`. |
| 1769 |
/// |
| 1770 |
/// </div> |
| 1771 |
/// |
| 1772 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 1773 |
/// [`Arg::num_args(1..)`]: Arg::num_args() |
| 1774 |
/// [`Arg::allow_hyphen_values(true)`]: Arg::allow_hyphen_values() |
| 1775 |
/// [`Arg::last(true)`]: Arg::last() |
| 1776 |
#[inline] |
| 1777 |
#[must_use] |
| 1778 |
pub fn raw(mut self, yes: bool) -> Self { |
| 1779 |
if yes { |
| 1780 |
self.num_vals.get_or_insert_with(|| (1..).into()); |
| 1781 |
} |
| 1782 |
self.allow_hyphen_values(yes).last(yes) |
| 1783 |
} |
| 1784 |
|
| 1785 |
/// Value for the argument when not present. |
| 1786 |
/// |
| 1787 |
/// Like with command-line values, this will be split by [`Arg::value_delimiter`]. |
| 1788 |
/// |
| 1789 |
/// <div class="warning"> |
| 1790 |
/// |
| 1791 |
/// **NOTE:** If the user *does not* use this argument at runtime [`ArgMatches::contains_id`] will |
| 1792 |
/// still return `true`. If you wish to determine whether the argument was used at runtime or |
| 1793 |
/// not, consider [`ArgMatches::value_source`][crate::ArgMatches::value_source]. |
| 1794 |
/// |
| 1795 |
/// </div> |
| 1796 |
/// |
| 1797 |
/// <div class="warning"> |
| 1798 |
/// |
| 1799 |
/// **NOTE:** This setting is perfectly compatible with [`Arg::default_value_if`] but slightly |
| 1800 |
/// different. `Arg::default_value` *only* takes effect when the user has not provided this arg |
| 1801 |
/// at runtime. `Arg::default_value_if` however only takes effect when the user has not provided |
| 1802 |
/// a value at runtime **and** these other conditions are met as well. If you have set |
| 1803 |
/// `Arg::default_value` and `Arg::default_value_if`, and the user **did not** provide this arg |
| 1804 |
/// at runtime, nor were the conditions met for `Arg::default_value_if`, the `Arg::default_value` |
| 1805 |
/// will be applied. |
| 1806 |
/// |
| 1807 |
/// </div> |
| 1808 |
/// |
| 1809 |
/// # Examples |
| 1810 |
/// |
| 1811 |
/// First we use the default value without providing any value at runtime. |
| 1812 |
/// |
| 1813 |
/// ```rust |
| 1814 |
/// # use clap_builder as clap; |
| 1815 |
/// # use clap::{Command, Arg, parser::ValueSource}; |
| 1816 |
/// let m = Command::new("prog") |
| 1817 |
/// .arg(Arg::new("opt") |
| 1818 |
/// .long("myopt") |
| 1819 |
/// .default_value("myval")) |
| 1820 |
/// .get_matches_from(vec![ |
| 1821 |
/// "prog" |
| 1822 |
/// ]); |
| 1823 |
/// |
| 1824 |
/// assert_eq!(m.get_one::<String>("opt").unwrap(), "myval"); |
| 1825 |
/// assert!(m.contains_id("opt")); |
| 1826 |
/// assert_eq!(m.value_source("opt"), Some(ValueSource::DefaultValue)); |
| 1827 |
/// ``` |
| 1828 |
/// |
| 1829 |
/// Next we provide a value at runtime to override the default. |
| 1830 |
/// |
| 1831 |
/// ```rust |
| 1832 |
/// # use clap_builder as clap; |
| 1833 |
/// # use clap::{Command, Arg, parser::ValueSource}; |
| 1834 |
/// let m = Command::new("prog") |
| 1835 |
/// .arg(Arg::new("opt") |
| 1836 |
/// .long("myopt") |
| 1837 |
/// .default_value("myval")) |
| 1838 |
/// .get_matches_from(vec![ |
| 1839 |
/// "prog", "--myopt=non_default" |
| 1840 |
/// ]); |
| 1841 |
/// |
| 1842 |
/// assert_eq!(m.get_one::<String>("opt").unwrap(), "non_default"); |
| 1843 |
/// assert!(m.contains_id("opt")); |
| 1844 |
/// assert_eq!(m.value_source("opt"), Some(ValueSource::CommandLine)); |
| 1845 |
/// ``` |
| 1846 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 1847 |
/// [`ArgMatches::contains_id`]: crate::ArgMatches::contains_id() |
| 1848 |
/// [`Arg::default_value_if`]: Arg::default_value_if() |
| 1849 |
#[inline] |
| 1850 |
#[must_use] |
| 1851 |
pub fn default_value(mut self, val: impl IntoResettable<OsStr>) -> Self { |
| 1852 |
if let Some(val) = val.into_resettable().into_option() { |
| 1853 |
self.default_values([val]) |
| 1854 |
} else { |
| 1855 |
self.default_vals.clear(); |
| 1856 |
self |
| 1857 |
} |
| 1858 |
} |
| 1859 |
|
| 1860 |
#[inline] |
| 1861 |
#[must_use] |
| 1862 |
#[doc(hidden)] |
| 1863 |
#[cfg_attr( |
| 1864 |
feature = "deprecated", |
| 1865 |
deprecated(since = "4.0.0", note = "Replaced with `Arg::default_value`") |
| 1866 |
)] |
| 1867 |
pub fn default_value_os(self, val: impl Into<OsStr>) -> Self { |
| 1868 |
self.default_values([val]) |
| 1869 |
} |
| 1870 |
|
| 1871 |
/// Value for the argument when not present. |
| 1872 |
/// |
| 1873 |
/// See [`Arg::default_value`]. |
| 1874 |
/// |
| 1875 |
/// [`Arg::default_value`]: Arg::default_value() |
| 1876 |
#[inline] |
| 1877 |
#[must_use] |
| 1878 |
pub fn default_values(mut self, vals: impl IntoIterator<Item = impl Into<OsStr>>) -> Self { |
| 1879 |
self.default_vals = vals.into_iter().map(|s| s.into()).collect(); |
| 1880 |
self |
| 1881 |
} |
| 1882 |
|
| 1883 |
#[inline] |
| 1884 |
#[must_use] |
| 1885 |
#[doc(hidden)] |
| 1886 |
#[cfg_attr( |
| 1887 |
feature = "deprecated", |
| 1888 |
deprecated(since = "4.0.0", note = "Replaced with `Arg::default_values`") |
| 1889 |
)] |
| 1890 |
pub fn default_values_os(self, vals: impl IntoIterator<Item = impl Into<OsStr>>) -> Self { |
| 1891 |
self.default_values(vals) |
| 1892 |
} |
| 1893 |
|
| 1894 |
/// Value for the argument when the flag is present but no value is specified. |
| 1895 |
/// |
| 1896 |
/// This configuration option is often used to give the user a shortcut and allow them to |
| 1897 |
/// efficiently specify an option argument without requiring an explicitly value. The `--color` |
| 1898 |
/// argument is a common example. By supplying a default, such as `default_missing_value("always")`, |
| 1899 |
/// the user can quickly just add `--color` to the command line to produce the desired color output. |
| 1900 |
/// |
| 1901 |
/// Like with command-line values, this will be split by [`Arg::value_delimiter`]. |
| 1902 |
/// |
| 1903 |
/// <div class="warning"> |
| 1904 |
/// |
| 1905 |
/// **NOTE:** using this configuration option requires the use of the |
| 1906 |
/// [`.num_args(0..N)`][Arg::num_args] and the |
| 1907 |
/// [`.require_equals(true)`][Arg::require_equals] configuration option. These are required in |
| 1908 |
/// order to unambiguously determine what, if any, value was supplied for the argument. |
| 1909 |
/// |
| 1910 |
/// </div> |
| 1911 |
/// |
| 1912 |
/// # Examples |
| 1913 |
/// |
| 1914 |
/// For POSIX style `--color`: |
| 1915 |
/// ```rust |
| 1916 |
/// # use clap_builder as clap; |
| 1917 |
/// # use clap::{Command, Arg, parser::ValueSource}; |
| 1918 |
/// fn cli() -> Command { |
| 1919 |
/// Command::new("prog") |
| 1920 |
/// .arg(Arg::new("color").long("color") |
| 1921 |
/// .value_name("WHEN") |
| 1922 |
/// .value_parser(["always", "auto", "never"]) |
| 1923 |
/// .default_value("auto") |
| 1924 |
/// .num_args(0..=1) |
| 1925 |
/// .require_equals(true) |
| 1926 |
/// .default_missing_value("always") |
| 1927 |
/// .help("Specify WHEN to colorize output.") |
| 1928 |
/// ) |
| 1929 |
/// } |
| 1930 |
/// |
| 1931 |
/// // first, we'll provide no arguments |
| 1932 |
/// let m = cli().get_matches_from(vec![ |
| 1933 |
/// "prog" |
| 1934 |
/// ]); |
| 1935 |
/// assert_eq!(m.get_one::<String>("color").unwrap(), "auto"); |
| 1936 |
/// assert_eq!(m.value_source("color"), Some(ValueSource::DefaultValue)); |
| 1937 |
/// |
| 1938 |
/// // next, we'll provide a runtime value to override the default (as usually done). |
| 1939 |
/// let m = cli().get_matches_from(vec![ |
| 1940 |
/// "prog", "--color=never" |
| 1941 |
/// ]); |
| 1942 |
/// assert_eq!(m.get_one::<String>("color").unwrap(), "never"); |
| 1943 |
/// assert_eq!(m.value_source("color"), Some(ValueSource::CommandLine)); |
| 1944 |
/// |
| 1945 |
/// // finally, we will use the shortcut and only provide the argument without a value. |
| 1946 |
/// let m = cli().get_matches_from(vec![ |
| 1947 |
/// "prog", "--color" |
| 1948 |
/// ]); |
| 1949 |
/// assert_eq!(m.get_one::<String>("color").unwrap(), "always"); |
| 1950 |
/// assert_eq!(m.value_source("color"), Some(ValueSource::CommandLine)); |
| 1951 |
/// ``` |
| 1952 |
/// |
| 1953 |
/// For bool literals: |
| 1954 |
/// ```rust |
| 1955 |
/// # use clap_builder as clap; |
| 1956 |
/// # use clap::{Command, Arg, parser::ValueSource, value_parser}; |
| 1957 |
/// fn cli() -> Command { |
| 1958 |
/// Command::new("prog") |
| 1959 |
/// .arg(Arg::new("create").long("create") |
| 1960 |
/// .value_name("BOOL") |
| 1961 |
/// .value_parser(value_parser!(bool)) |
| 1962 |
/// .num_args(0..=1) |
| 1963 |
/// .require_equals(true) |
| 1964 |
/// .default_missing_value("true") |
| 1965 |
/// ) |
| 1966 |
/// } |
| 1967 |
/// |
| 1968 |
/// // first, we'll provide no arguments |
| 1969 |
/// let m = cli().get_matches_from(vec![ |
| 1970 |
/// "prog" |
| 1971 |
/// ]); |
| 1972 |
/// assert_eq!(m.get_one::<bool>("create").copied(), None); |
| 1973 |
/// |
| 1974 |
/// // next, we'll provide a runtime value to override the default (as usually done). |
| 1975 |
/// let m = cli().get_matches_from(vec![ |
| 1976 |
/// "prog", "--create=false" |
| 1977 |
/// ]); |
| 1978 |
/// assert_eq!(m.get_one::<bool>("create").copied(), Some(false)); |
| 1979 |
/// assert_eq!(m.value_source("create"), Some(ValueSource::CommandLine)); |
| 1980 |
/// |
| 1981 |
/// // finally, we will use the shortcut and only provide the argument without a value. |
| 1982 |
/// let m = cli().get_matches_from(vec![ |
| 1983 |
/// "prog", "--create" |
| 1984 |
/// ]); |
| 1985 |
/// assert_eq!(m.get_one::<bool>("create").copied(), Some(true)); |
| 1986 |
/// assert_eq!(m.value_source("create"), Some(ValueSource::CommandLine)); |
| 1987 |
/// ``` |
| 1988 |
/// |
| 1989 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 1990 |
/// [`Arg::default_value`]: Arg::default_value() |
| 1991 |
#[inline] |
| 1992 |
#[must_use] |
| 1993 |
pub fn default_missing_value(mut self, val: impl IntoResettable<OsStr>) -> Self { |
| 1994 |
if let Some(val) = val.into_resettable().into_option() { |
| 1995 |
self.default_missing_values_os([val]) |
| 1996 |
} else { |
| 1997 |
self.default_missing_vals.clear(); |
| 1998 |
self |
| 1999 |
} |
| 2000 |
} |
| 2001 |
|
| 2002 |
/// Value for the argument when the flag is present but no value is specified. |
| 2003 |
/// |
| 2004 |
/// See [`Arg::default_missing_value`]. |
| 2005 |
/// |
| 2006 |
/// [`Arg::default_missing_value`]: Arg::default_missing_value() |
| 2007 |
/// [`OsStr`]: std::ffi::OsStr |
| 2008 |
#[inline] |
| 2009 |
#[must_use] |
| 2010 |
pub fn default_missing_value_os(self, val: impl Into<OsStr>) -> Self { |
| 2011 |
self.default_missing_values_os([val]) |
| 2012 |
} |
| 2013 |
|
| 2014 |
/// Value for the argument when the flag is present but no value is specified. |
| 2015 |
/// |
| 2016 |
/// See [`Arg::default_missing_value`]. |
| 2017 |
/// |
| 2018 |
/// [`Arg::default_missing_value`]: Arg::default_missing_value() |
| 2019 |
#[inline] |
| 2020 |
#[must_use] |
| 2021 |
pub fn default_missing_values(self, vals: impl IntoIterator<Item = impl Into<OsStr>>) -> Self { |
| 2022 |
self.default_missing_values_os(vals) |
| 2023 |
} |
| 2024 |
|
| 2025 |
/// Value for the argument when the flag is present but no value is specified. |
| 2026 |
/// |
| 2027 |
/// See [`Arg::default_missing_values`]. |
| 2028 |
/// |
| 2029 |
/// [`Arg::default_missing_values`]: Arg::default_missing_values() |
| 2030 |
/// [`OsStr`]: std::ffi::OsStr |
| 2031 |
#[inline] |
| 2032 |
#[must_use] |
| 2033 |
pub fn default_missing_values_os( |
| 2034 |
mut self, |
| 2035 |
vals: impl IntoIterator<Item = impl Into<OsStr>>, |
| 2036 |
) -> Self { |
| 2037 |
self.default_missing_vals = vals.into_iter().map(|s| s.into()).collect(); |
| 2038 |
self |
| 2039 |
} |
| 2040 |
|
| 2041 |
/// Read from `name` environment variable when argument is not present. |
| 2042 |
/// |
| 2043 |
/// If it is not present in the environment, then default |
| 2044 |
/// rules will apply. |
| 2045 |
/// |
| 2046 |
/// If user sets the argument in the environment: |
| 2047 |
/// - When [`Arg::action(ArgAction::Set)`] is not set, the flag is considered raised. |
| 2048 |
/// - When [`Arg::action(ArgAction::Set)`] is set, |
| 2049 |
/// [`ArgMatches::get_one`][crate::ArgMatches::get_one] will |
| 2050 |
/// return value of the environment variable. |
| 2051 |
/// |
| 2052 |
/// If user doesn't set the argument in the environment: |
| 2053 |
/// - When [`Arg::action(ArgAction::Set)`] is not set, the flag is considered off. |
| 2054 |
/// - When [`Arg::action(ArgAction::Set)`] is set, |
| 2055 |
/// [`ArgMatches::get_one`][crate::ArgMatches::get_one] will |
| 2056 |
/// return the default specified. |
| 2057 |
/// |
| 2058 |
/// Like with command-line values, this will be split by [`Arg::value_delimiter`]. |
| 2059 |
/// |
| 2060 |
/// # Examples |
| 2061 |
/// |
| 2062 |
/// In this example, we show the variable coming from the environment: |
| 2063 |
/// |
| 2064 |
/// ```rust |
| 2065 |
/// # use clap_builder as clap; |
| 2066 |
/// # use std::env; |
| 2067 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2068 |
/// # unsafe { |
| 2069 |
/// env::set_var("MY_FLAG", "env"); |
| 2070 |
/// # } |
| 2071 |
/// |
| 2072 |
/// let m = Command::new("prog") |
| 2073 |
/// .arg(Arg::new("flag") |
| 2074 |
/// .long("flag") |
| 2075 |
/// .env("MY_FLAG") |
| 2076 |
/// .action(ArgAction::Set)) |
| 2077 |
/// .get_matches_from(vec![ |
| 2078 |
/// "prog" |
| 2079 |
/// ]); |
| 2080 |
/// |
| 2081 |
/// assert_eq!(m.get_one::<String>("flag").unwrap(), "env"); |
| 2082 |
/// ``` |
| 2083 |
/// |
| 2084 |
/// In this example, because `prog` is a flag that accepts an optional, case-insensitive |
| 2085 |
/// boolean literal. |
| 2086 |
/// |
| 2087 |
/// Note that the value parser controls how flags are parsed. In this case we've selected |
| 2088 |
/// [`FalseyValueParser`][crate::builder::FalseyValueParser]. A `false` literal is `n`, `no`, |
| 2089 |
/// `f`, `false`, `off` or `0`. An absent environment variable will also be considered as |
| 2090 |
/// `false`. Anything else will considered as `true`. |
| 2091 |
/// |
| 2092 |
/// ```rust |
| 2093 |
/// # use clap_builder as clap; |
| 2094 |
/// # use std::env; |
| 2095 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2096 |
/// # use clap::builder::FalseyValueParser; |
| 2097 |
/// |
| 2098 |
/// # unsafe { |
| 2099 |
/// env::set_var("TRUE_FLAG", "true"); |
| 2100 |
/// env::set_var("FALSE_FLAG", "0"); |
| 2101 |
/// # } |
| 2102 |
/// |
| 2103 |
/// let m = Command::new("prog") |
| 2104 |
/// .arg(Arg::new("true_flag") |
| 2105 |
/// .long("true_flag") |
| 2106 |
/// .action(ArgAction::SetTrue) |
| 2107 |
/// .value_parser(FalseyValueParser::new()) |
| 2108 |
/// .env("TRUE_FLAG")) |
| 2109 |
/// .arg(Arg::new("false_flag") |
| 2110 |
/// .long("false_flag") |
| 2111 |
/// .action(ArgAction::SetTrue) |
| 2112 |
/// .value_parser(FalseyValueParser::new()) |
| 2113 |
/// .env("FALSE_FLAG")) |
| 2114 |
/// .arg(Arg::new("absent_flag") |
| 2115 |
/// .long("absent_flag") |
| 2116 |
/// .action(ArgAction::SetTrue) |
| 2117 |
/// .value_parser(FalseyValueParser::new()) |
| 2118 |
/// .env("ABSENT_FLAG")) |
| 2119 |
/// .get_matches_from(vec![ |
| 2120 |
/// "prog" |
| 2121 |
/// ]); |
| 2122 |
/// |
| 2123 |
/// assert!(m.get_flag("true_flag")); |
| 2124 |
/// assert!(!m.get_flag("false_flag")); |
| 2125 |
/// assert!(!m.get_flag("absent_flag")); |
| 2126 |
/// ``` |
| 2127 |
/// |
| 2128 |
/// In this example, we show the variable coming from an option on the CLI: |
| 2129 |
/// |
| 2130 |
/// ```rust |
| 2131 |
/// # use clap_builder as clap; |
| 2132 |
/// # use std::env; |
| 2133 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2134 |
/// |
| 2135 |
/// # unsafe { |
| 2136 |
/// env::set_var("MY_FLAG", "env"); |
| 2137 |
/// # } |
| 2138 |
/// |
| 2139 |
/// let m = Command::new("prog") |
| 2140 |
/// .arg(Arg::new("flag") |
| 2141 |
/// .long("flag") |
| 2142 |
/// .env("MY_FLAG") |
| 2143 |
/// .action(ArgAction::Set)) |
| 2144 |
/// .get_matches_from(vec![ |
| 2145 |
/// "prog", "--flag", "opt" |
| 2146 |
/// ]); |
| 2147 |
/// |
| 2148 |
/// assert_eq!(m.get_one::<String>("flag").unwrap(), "opt"); |
| 2149 |
/// ``` |
| 2150 |
/// |
| 2151 |
/// In this example, we show the variable coming from the environment even with the |
| 2152 |
/// presence of a default: |
| 2153 |
/// |
| 2154 |
/// ```rust |
| 2155 |
/// # use clap_builder as clap; |
| 2156 |
/// # use std::env; |
| 2157 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2158 |
/// |
| 2159 |
/// # unsafe { |
| 2160 |
/// env::set_var("MY_FLAG", "env"); |
| 2161 |
/// # } |
| 2162 |
/// |
| 2163 |
/// let m = Command::new("prog") |
| 2164 |
/// .arg(Arg::new("flag") |
| 2165 |
/// .long("flag") |
| 2166 |
/// .env("MY_FLAG") |
| 2167 |
/// .action(ArgAction::Set) |
| 2168 |
/// .default_value("default")) |
| 2169 |
/// .get_matches_from(vec![ |
| 2170 |
/// "prog" |
| 2171 |
/// ]); |
| 2172 |
/// |
| 2173 |
/// assert_eq!(m.get_one::<String>("flag").unwrap(), "env"); |
| 2174 |
/// ``` |
| 2175 |
/// |
| 2176 |
/// In this example, we show the use of multiple values in a single environment variable: |
| 2177 |
/// |
| 2178 |
/// ```rust |
| 2179 |
/// # use clap_builder as clap; |
| 2180 |
/// # use std::env; |
| 2181 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2182 |
/// |
| 2183 |
/// # unsafe { |
| 2184 |
/// env::set_var("MY_FLAG_MULTI", "env1,env2"); |
| 2185 |
/// # } |
| 2186 |
/// |
| 2187 |
/// let m = Command::new("prog") |
| 2188 |
/// .arg(Arg::new("flag") |
| 2189 |
/// .long("flag") |
| 2190 |
/// .env("MY_FLAG_MULTI") |
| 2191 |
/// .action(ArgAction::Set) |
| 2192 |
/// .num_args(1..) |
| 2193 |
/// .value_delimiter(',')) |
| 2194 |
/// .get_matches_from(vec![ |
| 2195 |
/// "prog" |
| 2196 |
/// ]); |
| 2197 |
/// |
| 2198 |
/// assert_eq!(m.get_many::<String>("flag").unwrap().collect::<Vec<_>>(), vec!["env1", "env2"]); |
| 2199 |
/// ``` |
| 2200 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 2201 |
/// [`Arg::value_delimiter(',')`]: Arg::value_delimiter() |
| 2202 |
#[cfg(feature = "env")] |
| 2203 |
#[inline] |
| 2204 |
#[must_use] |
| 2205 |
pub fn env(mut self, name: impl IntoResettable<OsStr>) -> Self { |
| 2206 |
if let Some(name) = name.into_resettable().into_option() { |
| 2207 |
let value = env::var_os(&name); |
| 2208 |
self.env = Some((name, value)); |
| 2209 |
} else { |
| 2210 |
self.env = None; |
| 2211 |
} |
| 2212 |
self |
| 2213 |
} |
| 2214 |
|
| 2215 |
#[cfg(feature = "env")] |
| 2216 |
#[doc(hidden)] |
| 2217 |
#[cfg_attr( |
| 2218 |
feature = "deprecated", |
| 2219 |
deprecated(since = "4.0.0", note = "Replaced with `Arg::env`") |
| 2220 |
)] |
| 2221 |
pub fn env_os(self, name: impl Into<OsStr>) -> Self { |
| 2222 |
self.env(name) |
| 2223 |
} |
| 2224 |
} |
| 2225 |
|
| 2226 |
/// # Help |
| 2227 |
impl Arg { |
| 2228 |
/// Sets the description of the argument for short help (`-h`). |
| 2229 |
/// |
| 2230 |
/// Typically, this is a short (one line) description of the arg. |
| 2231 |
/// |
| 2232 |
/// If [`Arg::long_help`] is not specified, this message will be displayed for `--help`. |
| 2233 |
/// |
| 2234 |
/// <div class="warning"> |
| 2235 |
/// |
| 2236 |
/// **NOTE:** Only `Arg::help` is used in completion script generation in order to be concise |
| 2237 |
/// |
| 2238 |
/// </div> |
| 2239 |
/// |
| 2240 |
/// # Examples |
| 2241 |
/// |
| 2242 |
/// Any valid UTF-8 is allowed in the help text. The one exception is when one wishes to |
| 2243 |
/// include a newline in the help text and have the following text be properly aligned with all |
| 2244 |
/// the other help text. |
| 2245 |
/// |
| 2246 |
/// Setting `help` displays a short message to the side of the argument when the user passes |
| 2247 |
/// `-h` or `--help` (by default). |
| 2248 |
/// |
| 2249 |
/// ```rust |
| 2250 |
/// # #[cfg(feature = "help")] { |
| 2251 |
/// # use clap_builder as clap; |
| 2252 |
/// # use clap::{Command, Arg}; |
| 2253 |
/// let m = Command::new("prog") |
| 2254 |
/// .arg(Arg::new("cfg") |
| 2255 |
/// .long("config") |
| 2256 |
/// .help("Some help text describing the --config arg")) |
| 2257 |
/// .get_matches_from(vec![ |
| 2258 |
/// "prog", "--help" |
| 2259 |
/// ]); |
| 2260 |
/// # } |
| 2261 |
/// ``` |
| 2262 |
/// |
| 2263 |
/// The above example displays |
| 2264 |
/// |
| 2265 |
/// ```notrust |
| 2266 |
/// helptest |
| 2267 |
/// |
| 2268 |
/// Usage: helptest [OPTIONS] |
| 2269 |
/// |
| 2270 |
/// Options: |
| 2271 |
/// --config Some help text describing the --config arg |
| 2272 |
/// -h, --help Print help information |
| 2273 |
/// -V, --version Print version information |
| 2274 |
/// ``` |
| 2275 |
/// [`Arg::long_help`]: Arg::long_help() |
| 2276 |
#[inline] |
| 2277 |
#[must_use] |
| 2278 |
pub fn help(mut self, h: impl IntoResettable<StyledStr>) -> Self { |
| 2279 |
self.help = h.into_resettable().into_option(); |
| 2280 |
self |
| 2281 |
} |
| 2282 |
|
| 2283 |
/// Sets the description of the argument for long help (`--help`). |
| 2284 |
/// |
| 2285 |
/// Typically this a more detailed (multi-line) message |
| 2286 |
/// that describes the arg. |
| 2287 |
/// |
| 2288 |
/// If [`Arg::help`] is not specified, this message will be displayed for `-h`. |
| 2289 |
/// |
| 2290 |
/// <div class="warning"> |
| 2291 |
/// |
| 2292 |
/// **NOTE:** Only [`Arg::help`] is used in completion script generation in order to be concise |
| 2293 |
/// |
| 2294 |
/// </div> |
| 2295 |
/// |
| 2296 |
/// # Examples |
| 2297 |
/// |
| 2298 |
/// Any valid UTF-8 is allowed in the help text. The one exception is when one wishes to |
| 2299 |
/// include a newline in the help text and have the following text be properly aligned with all |
| 2300 |
/// the other help text. |
| 2301 |
/// |
| 2302 |
/// Setting `help` displays a short message to the side of the argument when the user passes |
| 2303 |
/// `-h` or `--help` (by default). |
| 2304 |
/// |
| 2305 |
/// ```rust |
| 2306 |
/// # #[cfg(feature = "help")] { |
| 2307 |
/// # use clap_builder as clap; |
| 2308 |
/// # use clap::{Command, Arg}; |
| 2309 |
/// let m = Command::new("prog") |
| 2310 |
/// .arg(Arg::new("cfg") |
| 2311 |
/// .long("config") |
| 2312 |
/// .long_help( |
| 2313 |
/// "The config file used by the myprog must be in JSON format |
| 2314 |
/// with only valid keys and may not contain other nonsense |
| 2315 |
/// that cannot be read by this program. Obviously I'm going on |
| 2316 |
/// and on, so I'll stop now.")) |
| 2317 |
/// .get_matches_from(vec![ |
| 2318 |
/// "prog", "--help" |
| 2319 |
/// ]); |
| 2320 |
/// # } |
| 2321 |
/// ``` |
| 2322 |
/// |
| 2323 |
/// The above example displays |
| 2324 |
/// |
| 2325 |
/// ```text |
| 2326 |
/// prog |
| 2327 |
/// |
| 2328 |
/// Usage: prog [OPTIONS] |
| 2329 |
/// |
| 2330 |
/// Options: |
| 2331 |
/// --config |
| 2332 |
/// The config file used by the myprog must be in JSON format |
| 2333 |
/// with only valid keys and may not contain other nonsense |
| 2334 |
/// that cannot be read by this program. Obviously I'm going on |
| 2335 |
/// and on, so I'll stop now. |
| 2336 |
/// |
| 2337 |
/// -h, --help |
| 2338 |
/// Print help information |
| 2339 |
/// |
| 2340 |
/// -V, --version |
| 2341 |
/// Print version information |
| 2342 |
/// ``` |
| 2343 |
/// [`Arg::help`]: Arg::help() |
| 2344 |
#[inline] |
| 2345 |
#[must_use] |
| 2346 |
pub fn long_help(mut self, h: impl IntoResettable<StyledStr>) -> Self { |
| 2347 |
self.long_help = h.into_resettable().into_option(); |
| 2348 |
self |
| 2349 |
} |
| 2350 |
|
| 2351 |
/// Allows custom ordering of args within the help message. |
| 2352 |
/// |
| 2353 |
/// `Arg`s with a lower value will be displayed first in the help message. |
| 2354 |
/// Those with the same display order will be sorted. |
| 2355 |
/// |
| 2356 |
/// `Arg`s are automatically assigned a display order based on the order they are added to the |
| 2357 |
/// [`Command`][crate::Command]. |
| 2358 |
/// Overriding this is helpful when the order arguments are added in isn't the same as the |
| 2359 |
/// display order, whether in one-off cases or to automatically sort arguments. |
| 2360 |
/// |
| 2361 |
/// To change, see [`Command::next_display_order`][crate::Command::next_display_order]. |
| 2362 |
/// |
| 2363 |
/// <div class="warning"> |
| 2364 |
/// |
| 2365 |
/// **NOTE:** This setting is ignored for [positional arguments] which are always displayed in |
| 2366 |
/// [index] order. |
| 2367 |
/// |
| 2368 |
/// </div> |
| 2369 |
/// |
| 2370 |
/// # Examples |
| 2371 |
/// |
| 2372 |
/// ```rust |
| 2373 |
/// # #[cfg(feature = "help")] { |
| 2374 |
/// # use clap_builder as clap; |
| 2375 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2376 |
/// let m = Command::new("prog") |
| 2377 |
/// .arg(Arg::new("boat") |
| 2378 |
/// .short('b') |
| 2379 |
/// .long("boat") |
| 2380 |
/// .action(ArgAction::Set) |
| 2381 |
/// .display_order(0) // Sort |
| 2382 |
/// .help("Some help and text")) |
| 2383 |
/// .arg(Arg::new("airplane") |
| 2384 |
/// .short('a') |
| 2385 |
/// .long("airplane") |
| 2386 |
/// .action(ArgAction::Set) |
| 2387 |
/// .display_order(0) // Sort |
| 2388 |
/// .help("I should be first!")) |
| 2389 |
/// .arg(Arg::new("custom-help") |
| 2390 |
/// .short('?') |
| 2391 |
/// .action(ArgAction::Help) |
| 2392 |
/// .display_order(100) // Don't sort |
| 2393 |
/// .help("Alt help")) |
| 2394 |
/// .get_matches_from(vec![ |
| 2395 |
/// "prog", "--help" |
| 2396 |
/// ]); |
| 2397 |
/// # } |
| 2398 |
/// ``` |
| 2399 |
/// |
| 2400 |
/// The above example displays the following help message |
| 2401 |
/// |
| 2402 |
/// ```text |
| 2403 |
/// cust-ord |
| 2404 |
/// |
| 2405 |
/// Usage: cust-ord [OPTIONS] |
| 2406 |
/// |
| 2407 |
/// Options: |
| 2408 |
/// -a, --airplane <airplane> I should be first! |
| 2409 |
/// -b, --boat <boar> Some help and text |
| 2410 |
/// -h, --help Print help information |
| 2411 |
/// -? Alt help |
| 2412 |
/// ``` |
| 2413 |
/// [positional arguments]: Arg::index() |
| 2414 |
/// [index]: Arg::index() |
| 2415 |
#[inline] |
| 2416 |
#[must_use] |
| 2417 |
pub fn display_order(mut self, ord: impl IntoResettable<usize>) -> Self { |
| 2418 |
self.disp_ord = ord.into_resettable().into_option(); |
| 2419 |
self |
| 2420 |
} |
| 2421 |
|
| 2422 |
/// Override the `--help` section this appears in. |
| 2423 |
/// |
| 2424 |
/// For more on the default help heading, see |
| 2425 |
/// [`Command::next_help_heading`][crate::Command::next_help_heading]. |
| 2426 |
#[inline] |
| 2427 |
#[must_use] |
| 2428 |
pub fn help_heading(mut self, heading: impl IntoResettable<Str>) -> Self { |
| 2429 |
self.help_heading = Some(heading.into_resettable().into_option()); |
| 2430 |
self |
| 2431 |
} |
| 2432 |
|
| 2433 |
/// Render the [help][Arg::help] on the line after the argument. |
| 2434 |
/// |
| 2435 |
/// This can be helpful for arguments with very long or complex help messages. |
| 2436 |
/// This can also be helpful for arguments with very long flag names, or many/long value names. |
| 2437 |
/// |
| 2438 |
/// <div class="warning"> |
| 2439 |
/// |
| 2440 |
/// **NOTE:** To apply this setting to all arguments and subcommands, consider using |
| 2441 |
/// [`crate::Command::next_line_help`] |
| 2442 |
/// |
| 2443 |
/// </div> |
| 2444 |
/// |
| 2445 |
/// # Examples |
| 2446 |
/// |
| 2447 |
/// ```rust |
| 2448 |
/// # #[cfg(feature = "help")] { |
| 2449 |
/// # use clap_builder as clap; |
| 2450 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2451 |
/// let m = Command::new("prog") |
| 2452 |
/// .arg(Arg::new("opt") |
| 2453 |
/// .long("long-option-flag") |
| 2454 |
/// .short('o') |
| 2455 |
/// .action(ArgAction::Set) |
| 2456 |
/// .next_line_help(true) |
| 2457 |
/// .value_names(["value1", "value2"]) |
| 2458 |
/// .help("Some really long help and complex\n\ |
| 2459 |
/// help that makes more sense to be\n\ |
| 2460 |
/// on a line after the option")) |
| 2461 |
/// .get_matches_from(vec![ |
| 2462 |
/// "prog", "--help" |
| 2463 |
/// ]); |
| 2464 |
/// # } |
| 2465 |
/// ``` |
| 2466 |
/// |
| 2467 |
/// The above example displays the following help message |
| 2468 |
/// |
| 2469 |
/// ```text |
| 2470 |
/// nlh |
| 2471 |
/// |
| 2472 |
/// Usage: nlh [OPTIONS] |
| 2473 |
/// |
| 2474 |
/// Options: |
| 2475 |
/// -h, --help Print help information |
| 2476 |
/// -V, --version Print version information |
| 2477 |
/// -o, --long-option-flag <value1> <value2> |
| 2478 |
/// Some really long help and complex |
| 2479 |
/// help that makes more sense to be |
| 2480 |
/// on a line after the option |
| 2481 |
/// ``` |
| 2482 |
#[inline] |
| 2483 |
#[must_use] |
| 2484 |
pub fn next_line_help(self, yes: bool) -> Self { |
| 2485 |
if yes { |
| 2486 |
self.setting(ArgSettings::NextLineHelp) |
| 2487 |
} else { |
| 2488 |
self.unset_setting(ArgSettings::NextLineHelp) |
| 2489 |
} |
| 2490 |
} |
| 2491 |
|
| 2492 |
/// Do not display the argument in help message. |
| 2493 |
/// |
| 2494 |
/// <div class="warning"> |
| 2495 |
/// |
| 2496 |
/// **NOTE:** This does **not** hide the argument from usage strings on error |
| 2497 |
/// |
| 2498 |
/// </div> |
| 2499 |
/// |
| 2500 |
/// # Examples |
| 2501 |
/// |
| 2502 |
/// Setting `Hidden` will hide the argument when displaying help text |
| 2503 |
/// |
| 2504 |
/// ```rust |
| 2505 |
/// # #[cfg(feature = "help")] { |
| 2506 |
/// # use clap_builder as clap; |
| 2507 |
/// # use clap::{Command, Arg}; |
| 2508 |
/// let m = Command::new("prog") |
| 2509 |
/// .arg(Arg::new("cfg") |
| 2510 |
/// .long("config") |
| 2511 |
/// .hide(true) |
| 2512 |
/// .help("Some help text describing the --config arg")) |
| 2513 |
/// .get_matches_from(vec![ |
| 2514 |
/// "prog", "--help" |
| 2515 |
/// ]); |
| 2516 |
/// # } |
| 2517 |
/// ``` |
| 2518 |
/// |
| 2519 |
/// The above example displays |
| 2520 |
/// |
| 2521 |
/// ```text |
| 2522 |
/// helptest |
| 2523 |
/// |
| 2524 |
/// Usage: helptest [OPTIONS] |
| 2525 |
/// |
| 2526 |
/// Options: |
| 2527 |
/// -h, --help Print help information |
| 2528 |
/// -V, --version Print version information |
| 2529 |
/// ``` |
| 2530 |
#[inline] |
| 2531 |
#[must_use] |
| 2532 |
pub fn hide(self, yes: bool) -> Self { |
| 2533 |
if yes { |
| 2534 |
self.setting(ArgSettings::Hidden) |
| 2535 |
} else { |
| 2536 |
self.unset_setting(ArgSettings::Hidden) |
| 2537 |
} |
| 2538 |
} |
| 2539 |
|
| 2540 |
/// Do not display the [possible values][crate::builder::ValueParser::possible_values] in the help message. |
| 2541 |
/// |
| 2542 |
/// This is useful for args with many values, or ones which are explained elsewhere in the |
| 2543 |
/// help text. |
| 2544 |
/// |
| 2545 |
/// To set this for all arguments, see |
| 2546 |
/// [`Command::hide_possible_values`][crate::Command::hide_possible_values]. |
| 2547 |
/// |
| 2548 |
/// <div class="warning"> |
| 2549 |
/// |
| 2550 |
/// **NOTE:** Setting this requires [taking values][Arg::num_args] |
| 2551 |
/// |
| 2552 |
/// </div> |
| 2553 |
/// |
| 2554 |
/// # Examples |
| 2555 |
/// |
| 2556 |
/// ```rust |
| 2557 |
/// # use clap_builder as clap; |
| 2558 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2559 |
/// let m = Command::new("prog") |
| 2560 |
/// .arg(Arg::new("mode") |
| 2561 |
/// .long("mode") |
| 2562 |
/// .value_parser(["fast", "slow"]) |
| 2563 |
/// .action(ArgAction::Set) |
| 2564 |
/// .hide_possible_values(true)); |
| 2565 |
/// ``` |
| 2566 |
/// If we were to run the above program with `--help` the `[values: fast, slow]` portion of |
| 2567 |
/// the help text would be omitted. |
| 2568 |
#[inline] |
| 2569 |
#[must_use] |
| 2570 |
pub fn hide_possible_values(self, yes: bool) -> Self { |
| 2571 |
if yes { |
| 2572 |
self.setting(ArgSettings::HidePossibleValues) |
| 2573 |
} else { |
| 2574 |
self.unset_setting(ArgSettings::HidePossibleValues) |
| 2575 |
} |
| 2576 |
} |
| 2577 |
|
| 2578 |
/// Do not display the default value of the argument in the help message. |
| 2579 |
/// |
| 2580 |
/// This is useful when default behavior of an arg is explained elsewhere in the help text. |
| 2581 |
/// |
| 2582 |
/// <div class="warning"> |
| 2583 |
/// |
| 2584 |
/// **NOTE:** Setting this requires [taking values][Arg::num_args] |
| 2585 |
/// |
| 2586 |
/// </div> |
| 2587 |
/// |
| 2588 |
/// # Examples |
| 2589 |
/// |
| 2590 |
/// ```rust |
| 2591 |
/// # use clap_builder as clap; |
| 2592 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2593 |
/// let m = Command::new("connect") |
| 2594 |
/// .arg(Arg::new("host") |
| 2595 |
/// .long("host") |
| 2596 |
/// .default_value("localhost") |
| 2597 |
/// .action(ArgAction::Set) |
| 2598 |
/// .hide_default_value(true)); |
| 2599 |
/// |
| 2600 |
/// ``` |
| 2601 |
/// |
| 2602 |
/// If we were to run the above program with `--help` the `[default: localhost]` portion of |
| 2603 |
/// the help text would be omitted. |
| 2604 |
#[inline] |
| 2605 |
#[must_use] |
| 2606 |
pub fn hide_default_value(self, yes: bool) -> Self { |
| 2607 |
if yes { |
| 2608 |
self.setting(ArgSettings::HideDefaultValue) |
| 2609 |
} else { |
| 2610 |
self.unset_setting(ArgSettings::HideDefaultValue) |
| 2611 |
} |
| 2612 |
} |
| 2613 |
|
| 2614 |
/// Do not display in help the environment variable name. |
| 2615 |
/// |
| 2616 |
/// This is useful when the variable option is explained elsewhere in the help text. |
| 2617 |
/// |
| 2618 |
/// # Examples |
| 2619 |
/// |
| 2620 |
/// ```rust |
| 2621 |
/// # use clap_builder as clap; |
| 2622 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2623 |
/// let m = Command::new("prog") |
| 2624 |
/// .arg(Arg::new("mode") |
| 2625 |
/// .long("mode") |
| 2626 |
/// .env("MODE") |
| 2627 |
/// .action(ArgAction::Set) |
| 2628 |
/// .hide_env(true)); |
| 2629 |
/// ``` |
| 2630 |
/// |
| 2631 |
/// If we were to run the above program with `--help` the `[env: MODE]` portion of the help |
| 2632 |
/// text would be omitted. |
| 2633 |
#[cfg(feature = "env")] |
| 2634 |
#[inline] |
| 2635 |
#[must_use] |
| 2636 |
pub fn hide_env(self, yes: bool) -> Self { |
| 2637 |
if yes { |
| 2638 |
self.setting(ArgSettings::HideEnv) |
| 2639 |
} else { |
| 2640 |
self.unset_setting(ArgSettings::HideEnv) |
| 2641 |
} |
| 2642 |
} |
| 2643 |
|
| 2644 |
/// Do not display in help any values inside the associated ENV variables for the argument. |
| 2645 |
/// |
| 2646 |
/// This is useful when ENV vars contain sensitive values. |
| 2647 |
/// |
| 2648 |
/// # Examples |
| 2649 |
/// |
| 2650 |
/// ```rust |
| 2651 |
/// # use clap_builder as clap; |
| 2652 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2653 |
/// let m = Command::new("connect") |
| 2654 |
/// .arg(Arg::new("host") |
| 2655 |
/// .long("host") |
| 2656 |
/// .env("CONNECT") |
| 2657 |
/// .action(ArgAction::Set) |
| 2658 |
/// .hide_env_values(true)); |
| 2659 |
/// |
| 2660 |
/// ``` |
| 2661 |
/// |
| 2662 |
/// If we were to run the above program with `$ CONNECT=super_secret connect --help` the |
| 2663 |
/// `[default: CONNECT=super_secret]` portion of the help text would be omitted. |
| 2664 |
#[cfg(feature = "env")] |
| 2665 |
#[inline] |
| 2666 |
#[must_use] |
| 2667 |
pub fn hide_env_values(self, yes: bool) -> Self { |
| 2668 |
if yes { |
| 2669 |
self.setting(ArgSettings::HideEnvValues) |
| 2670 |
} else { |
| 2671 |
self.unset_setting(ArgSettings::HideEnvValues) |
| 2672 |
} |
| 2673 |
} |
| 2674 |
|
| 2675 |
/// Hides an argument from short help (`-h`). |
| 2676 |
/// |
| 2677 |
/// <div class="warning"> |
| 2678 |
/// |
| 2679 |
/// **NOTE:** This does **not** hide the argument from usage strings on error |
| 2680 |
/// |
| 2681 |
/// </div> |
| 2682 |
/// |
| 2683 |
/// <div class="warning"> |
| 2684 |
/// |
| 2685 |
/// **NOTE:** Setting this option will cause next-line-help output style to be used |
| 2686 |
/// when long help (`--help`) is called. |
| 2687 |
/// |
| 2688 |
/// </div> |
| 2689 |
/// |
| 2690 |
/// # Examples |
| 2691 |
/// |
| 2692 |
/// ```rust |
| 2693 |
/// # use clap_builder as clap; |
| 2694 |
/// # use clap::{Command, Arg}; |
| 2695 |
/// Arg::new("debug") |
| 2696 |
/// .hide_short_help(true); |
| 2697 |
/// ``` |
| 2698 |
/// |
| 2699 |
/// Setting `hide_short_help(true)` will hide the argument when displaying short help text |
| 2700 |
/// |
| 2701 |
/// ```rust |
| 2702 |
/// # #[cfg(feature = "help")] { |
| 2703 |
/// # use clap_builder as clap; |
| 2704 |
/// # use clap::{Command, Arg}; |
| 2705 |
/// let m = Command::new("prog") |
| 2706 |
/// .arg(Arg::new("cfg") |
| 2707 |
/// .long("config") |
| 2708 |
/// .hide_short_help(true) |
| 2709 |
/// .help("Some help text describing the --config arg")) |
| 2710 |
/// .get_matches_from(vec![ |
| 2711 |
/// "prog", "-h" |
| 2712 |
/// ]); |
| 2713 |
/// # } |
| 2714 |
/// ``` |
| 2715 |
/// |
| 2716 |
/// The above example displays |
| 2717 |
/// |
| 2718 |
/// ```text |
| 2719 |
/// helptest |
| 2720 |
/// |
| 2721 |
/// Usage: helptest [OPTIONS] |
| 2722 |
/// |
| 2723 |
/// Options: |
| 2724 |
/// -h, --help Print help information |
| 2725 |
/// -V, --version Print version information |
| 2726 |
/// ``` |
| 2727 |
/// |
| 2728 |
/// However, when --help is called |
| 2729 |
/// |
| 2730 |
/// ```rust |
| 2731 |
/// # #[cfg(feature = "help")] { |
| 2732 |
/// # use clap_builder as clap; |
| 2733 |
/// # use clap::{Command, Arg}; |
| 2734 |
/// let m = Command::new("prog") |
| 2735 |
/// .arg(Arg::new("cfg") |
| 2736 |
/// .long("config") |
| 2737 |
/// .hide_short_help(true) |
| 2738 |
/// .help("Some help text describing the --config arg")) |
| 2739 |
/// .get_matches_from(vec![ |
| 2740 |
/// "prog", "--help" |
| 2741 |
/// ]); |
| 2742 |
/// # } |
| 2743 |
/// ``` |
| 2744 |
/// |
| 2745 |
/// Then the following would be displayed |
| 2746 |
/// |
| 2747 |
/// ```text |
| 2748 |
/// helptest |
| 2749 |
/// |
| 2750 |
/// Usage: helptest [OPTIONS] |
| 2751 |
/// |
| 2752 |
/// Options: |
| 2753 |
/// --config Some help text describing the --config arg |
| 2754 |
/// -h, --help Print help information |
| 2755 |
/// -V, --version Print version information |
| 2756 |
/// ``` |
| 2757 |
#[inline] |
| 2758 |
#[must_use] |
| 2759 |
pub fn hide_short_help(self, yes: bool) -> Self { |
| 2760 |
if yes { |
| 2761 |
self.setting(ArgSettings::HiddenShortHelp) |
| 2762 |
} else { |
| 2763 |
self.unset_setting(ArgSettings::HiddenShortHelp) |
| 2764 |
} |
| 2765 |
} |
| 2766 |
|
| 2767 |
/// Hides an argument from long help (`--help`). |
| 2768 |
/// |
| 2769 |
/// <div class="warning"> |
| 2770 |
/// |
| 2771 |
/// **NOTE:** This does **not** hide the argument from usage strings on error |
| 2772 |
/// |
| 2773 |
/// </div> |
| 2774 |
/// |
| 2775 |
/// <div class="warning"> |
| 2776 |
/// |
| 2777 |
/// **NOTE:** Setting this option will cause next-line-help output style to be used |
| 2778 |
/// when long help (`--help`) is called. |
| 2779 |
/// |
| 2780 |
/// </div> |
| 2781 |
/// |
| 2782 |
/// # Examples |
| 2783 |
/// |
| 2784 |
/// Setting `hide_long_help(true)` will hide the argument when displaying long help text |
| 2785 |
/// |
| 2786 |
/// ```rust |
| 2787 |
/// # #[cfg(feature = "help")] { |
| 2788 |
/// # use clap_builder as clap; |
| 2789 |
/// # use clap::{Command, Arg}; |
| 2790 |
/// let m = Command::new("prog") |
| 2791 |
/// .arg(Arg::new("cfg") |
| 2792 |
/// .long("config") |
| 2793 |
/// .hide_long_help(true) |
| 2794 |
/// .help("Some help text describing the --config arg")) |
| 2795 |
/// .get_matches_from(vec![ |
| 2796 |
/// "prog", "--help" |
| 2797 |
/// ]); |
| 2798 |
/// # } |
| 2799 |
/// ``` |
| 2800 |
/// |
| 2801 |
/// The above example displays |
| 2802 |
/// |
| 2803 |
/// ```text |
| 2804 |
/// helptest |
| 2805 |
/// |
| 2806 |
/// Usage: helptest [OPTIONS] |
| 2807 |
/// |
| 2808 |
/// Options: |
| 2809 |
/// -h, --help Print help information |
| 2810 |
/// -V, --version Print version information |
| 2811 |
/// ``` |
| 2812 |
/// |
| 2813 |
/// However, when -h is called |
| 2814 |
/// |
| 2815 |
/// ```rust |
| 2816 |
/// # #[cfg(feature = "help")] { |
| 2817 |
/// # use clap_builder as clap; |
| 2818 |
/// # use clap::{Command, Arg}; |
| 2819 |
/// let m = Command::new("prog") |
| 2820 |
/// .arg(Arg::new("cfg") |
| 2821 |
/// .long("config") |
| 2822 |
/// .hide_long_help(true) |
| 2823 |
/// .help("Some help text describing the --config arg")) |
| 2824 |
/// .get_matches_from(vec![ |
| 2825 |
/// "prog", "-h" |
| 2826 |
/// ]); |
| 2827 |
/// # } |
| 2828 |
/// ``` |
| 2829 |
/// |
| 2830 |
/// Then the following would be displayed |
| 2831 |
/// |
| 2832 |
/// ```text |
| 2833 |
/// helptest |
| 2834 |
/// |
| 2835 |
/// Usage: helptest [OPTIONS] |
| 2836 |
/// |
| 2837 |
/// OPTIONS: |
| 2838 |
/// --config Some help text describing the --config arg |
| 2839 |
/// -h, --help Print help information |
| 2840 |
/// -V, --version Print version information |
| 2841 |
/// ``` |
| 2842 |
#[inline] |
| 2843 |
#[must_use] |
| 2844 |
pub fn hide_long_help(self, yes: bool) -> Self { |
| 2845 |
if yes { |
| 2846 |
self.setting(ArgSettings::HiddenLongHelp) |
| 2847 |
} else { |
| 2848 |
self.unset_setting(ArgSettings::HiddenLongHelp) |
| 2849 |
} |
| 2850 |
} |
| 2851 |
} |
| 2852 |
|
| 2853 |
/// # Advanced Argument Relations |
| 2854 |
impl Arg { |
| 2855 |
/// The name of the [`ArgGroup`] the argument belongs to. |
| 2856 |
/// |
| 2857 |
/// # Examples |
| 2858 |
/// |
| 2859 |
/// ```rust |
| 2860 |
/// # use clap_builder as clap; |
| 2861 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2862 |
/// Arg::new("debug") |
| 2863 |
/// .long("debug") |
| 2864 |
/// .action(ArgAction::SetTrue) |
| 2865 |
/// .group("mode") |
| 2866 |
/// # ; |
| 2867 |
/// ``` |
| 2868 |
/// |
| 2869 |
/// Multiple arguments can be a member of a single group and then the group checked as if it |
| 2870 |
/// was one of said arguments. |
| 2871 |
/// |
| 2872 |
/// ```rust |
| 2873 |
/// # use clap_builder as clap; |
| 2874 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2875 |
/// let m = Command::new("prog") |
| 2876 |
/// .arg(Arg::new("debug") |
| 2877 |
/// .long("debug") |
| 2878 |
/// .action(ArgAction::SetTrue) |
| 2879 |
/// .group("mode")) |
| 2880 |
/// .arg(Arg::new("verbose") |
| 2881 |
/// .long("verbose") |
| 2882 |
/// .action(ArgAction::SetTrue) |
| 2883 |
/// .group("mode")) |
| 2884 |
/// .get_matches_from(vec![ |
| 2885 |
/// "prog", "--debug" |
| 2886 |
/// ]); |
| 2887 |
/// assert!(m.contains_id("mode")); |
| 2888 |
/// ``` |
| 2889 |
/// |
| 2890 |
/// [`ArgGroup`]: crate::ArgGroup |
| 2891 |
#[must_use] |
| 2892 |
pub fn group(mut self, group_id: impl IntoResettable<Id>) -> Self { |
| 2893 |
if let Some(group_id) = group_id.into_resettable().into_option() { |
| 2894 |
self.groups.push(group_id); |
| 2895 |
} else { |
| 2896 |
self.groups.clear(); |
| 2897 |
} |
| 2898 |
self |
| 2899 |
} |
| 2900 |
|
| 2901 |
/// The names of [`ArgGroup`]'s the argument belongs to. |
| 2902 |
/// |
| 2903 |
/// # Examples |
| 2904 |
/// |
| 2905 |
/// ```rust |
| 2906 |
/// # use clap_builder as clap; |
| 2907 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2908 |
/// Arg::new("debug") |
| 2909 |
/// .long("debug") |
| 2910 |
/// .action(ArgAction::SetTrue) |
| 2911 |
/// .groups(["mode", "verbosity"]) |
| 2912 |
/// # ; |
| 2913 |
/// ``` |
| 2914 |
/// |
| 2915 |
/// Arguments can be members of multiple groups and then the group checked as if it |
| 2916 |
/// was one of said arguments. |
| 2917 |
/// |
| 2918 |
/// ```rust |
| 2919 |
/// # use clap_builder as clap; |
| 2920 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2921 |
/// let m = Command::new("prog") |
| 2922 |
/// .arg(Arg::new("debug") |
| 2923 |
/// .long("debug") |
| 2924 |
/// .action(ArgAction::SetTrue) |
| 2925 |
/// .groups(["mode", "verbosity"])) |
| 2926 |
/// .arg(Arg::new("verbose") |
| 2927 |
/// .long("verbose") |
| 2928 |
/// .action(ArgAction::SetTrue) |
| 2929 |
/// .groups(["mode", "verbosity"])) |
| 2930 |
/// .get_matches_from(vec![ |
| 2931 |
/// "prog", "--debug" |
| 2932 |
/// ]); |
| 2933 |
/// assert!(m.contains_id("mode")); |
| 2934 |
/// assert!(m.contains_id("verbosity")); |
| 2935 |
/// ``` |
| 2936 |
/// |
| 2937 |
/// [`ArgGroup`]: crate::ArgGroup |
| 2938 |
#[must_use] |
| 2939 |
pub fn groups(mut self, group_ids: impl IntoIterator<Item = impl Into<Id>>) -> Self { |
| 2940 |
self.groups.extend(group_ids.into_iter().map(Into::into)); |
| 2941 |
self |
| 2942 |
} |
| 2943 |
|
| 2944 |
/// Specifies the value of the argument if `arg` has been used at runtime. |
| 2945 |
/// |
| 2946 |
/// If `default` is set to `None`, `default_value` will be removed. |
| 2947 |
/// |
| 2948 |
/// Like with command-line values, this will be split by [`Arg::value_delimiter`]. |
| 2949 |
/// |
| 2950 |
/// <div class="warning"> |
| 2951 |
/// |
| 2952 |
/// **NOTE:** This setting is perfectly compatible with [`Arg::default_value`] but slightly |
| 2953 |
/// different. `Arg::default_value` *only* takes effect when the user has not provided this arg |
| 2954 |
/// at runtime. This setting however only takes effect when the user has not provided a value at |
| 2955 |
/// runtime **and** these other conditions are met as well. If you have set `Arg::default_value` |
| 2956 |
/// and `Arg::default_value_if`, and the user **did not** provide this arg at runtime, nor were |
| 2957 |
/// the conditions met for `Arg::default_value_if`, the `Arg::default_value` will be applied. |
| 2958 |
/// |
| 2959 |
/// </div> |
| 2960 |
/// |
| 2961 |
/// # Examples |
| 2962 |
/// |
| 2963 |
/// First we use the default value only if another arg is present at runtime. |
| 2964 |
/// |
| 2965 |
/// ```rust |
| 2966 |
/// # use clap_builder as clap; |
| 2967 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2968 |
/// # use clap::builder::{ArgPredicate}; |
| 2969 |
/// let m = Command::new("prog") |
| 2970 |
/// .arg(Arg::new("flag") |
| 2971 |
/// .long("flag") |
| 2972 |
/// .action(ArgAction::SetTrue)) |
| 2973 |
/// .arg(Arg::new("other") |
| 2974 |
/// .long("other") |
| 2975 |
/// .default_value_if("flag", ArgPredicate::IsPresent, Some("default"))) |
| 2976 |
/// .get_matches_from(vec![ |
| 2977 |
/// "prog", "--flag" |
| 2978 |
/// ]); |
| 2979 |
/// |
| 2980 |
/// assert_eq!(m.get_one::<String>("other").unwrap(), "default"); |
| 2981 |
/// ``` |
| 2982 |
/// |
| 2983 |
/// Next we run the same test, but without providing `--flag`. |
| 2984 |
/// |
| 2985 |
/// ```rust |
| 2986 |
/// # use clap_builder as clap; |
| 2987 |
/// # use clap::{Command, Arg, ArgAction}; |
| 2988 |
/// let m = Command::new("prog") |
| 2989 |
/// .arg(Arg::new("flag") |
| 2990 |
/// .long("flag") |
| 2991 |
/// .action(ArgAction::SetTrue)) |
| 2992 |
/// .arg(Arg::new("other") |
| 2993 |
/// .long("other") |
| 2994 |
/// .default_value_if("flag", "true", Some("default"))) |
| 2995 |
/// .get_matches_from(vec![ |
| 2996 |
/// "prog" |
| 2997 |
/// ]); |
| 2998 |
/// |
| 2999 |
/// assert_eq!(m.get_one::<String>("other"), None); |
| 3000 |
/// ``` |
| 3001 |
/// |
| 3002 |
/// Now lets only use the default value if `--opt` contains the value `special`. |
| 3003 |
/// |
| 3004 |
/// ```rust |
| 3005 |
/// # use clap_builder as clap; |
| 3006 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3007 |
/// let m = Command::new("prog") |
| 3008 |
/// .arg(Arg::new("opt") |
| 3009 |
/// .action(ArgAction::Set) |
| 3010 |
/// .long("opt")) |
| 3011 |
/// .arg(Arg::new("other") |
| 3012 |
/// .long("other") |
| 3013 |
/// .default_value_if("opt", "special", Some("default"))) |
| 3014 |
/// .get_matches_from(vec![ |
| 3015 |
/// "prog", "--opt", "special" |
| 3016 |
/// ]); |
| 3017 |
/// |
| 3018 |
/// assert_eq!(m.get_one::<String>("other").unwrap(), "default"); |
| 3019 |
/// ``` |
| 3020 |
/// |
| 3021 |
/// We can run the same test and provide any value *other than* `special` and we won't get a |
| 3022 |
/// default value. |
| 3023 |
/// |
| 3024 |
/// ```rust |
| 3025 |
/// # use clap_builder as clap; |
| 3026 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3027 |
/// let m = Command::new("prog") |
| 3028 |
/// .arg(Arg::new("opt") |
| 3029 |
/// .action(ArgAction::Set) |
| 3030 |
/// .long("opt")) |
| 3031 |
/// .arg(Arg::new("other") |
| 3032 |
/// .long("other") |
| 3033 |
/// .default_value_if("opt", "special", Some("default"))) |
| 3034 |
/// .get_matches_from(vec![ |
| 3035 |
/// "prog", "--opt", "hahaha" |
| 3036 |
/// ]); |
| 3037 |
/// |
| 3038 |
/// assert_eq!(m.get_one::<String>("other"), None); |
| 3039 |
/// ``` |
| 3040 |
/// |
| 3041 |
/// If we want to unset the default value for an Arg based on the presence or |
| 3042 |
/// value of some other Arg. |
| 3043 |
/// |
| 3044 |
/// ```rust |
| 3045 |
/// # use clap_builder as clap; |
| 3046 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3047 |
/// let m = Command::new("prog") |
| 3048 |
/// .arg(Arg::new("flag") |
| 3049 |
/// .long("flag") |
| 3050 |
/// .action(ArgAction::SetTrue)) |
| 3051 |
/// .arg(Arg::new("other") |
| 3052 |
/// .long("other") |
| 3053 |
/// .default_value("default") |
| 3054 |
/// .default_value_if("flag", "true", None)) |
| 3055 |
/// .get_matches_from(vec![ |
| 3056 |
/// "prog", "--flag" |
| 3057 |
/// ]); |
| 3058 |
/// |
| 3059 |
/// assert_eq!(m.get_one::<String>("other"), None); |
| 3060 |
/// ``` |
| 3061 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 3062 |
/// [`Arg::default_value`]: Arg::default_value() |
| 3063 |
#[must_use] |
| 3064 |
pub fn default_value_if( |
| 3065 |
mut self, |
| 3066 |
arg_id: impl Into<Id>, |
| 3067 |
predicate: impl Into<ArgPredicate>, |
| 3068 |
default: impl IntoResettable<OsStr>, |
| 3069 |
) -> Self { |
| 3070 |
self.default_vals_ifs.push(( |
| 3071 |
arg_id.into(), |
| 3072 |
predicate.into(), |
| 3073 |
default |
| 3074 |
.into_resettable() |
| 3075 |
.into_option() |
| 3076 |
.map(|os_str| vec![os_str]), |
| 3077 |
)); |
| 3078 |
self |
| 3079 |
} |
| 3080 |
|
| 3081 |
/// Specifies the values of the argument if `arg` has been used at runtime. |
| 3082 |
/// |
| 3083 |
/// See [`Arg::default_value_if`]. |
| 3084 |
/// |
| 3085 |
/// # Examples |
| 3086 |
/// |
| 3087 |
/// ```rust |
| 3088 |
/// use clap_builder::arg; |
| 3089 |
/// use clap_builder::Command; |
| 3090 |
/// use clap_builder::Arg; |
| 3091 |
/// let r = Command::new("df") |
| 3092 |
/// .arg(arg!(--opt <FILE> "some arg")) |
| 3093 |
/// .arg( |
| 3094 |
/// Arg::new("args") |
| 3095 |
/// .long("args") |
| 3096 |
/// .num_args(2) |
| 3097 |
/// .default_values_if("opt", "value", ["df1","df2"]), |
| 3098 |
/// ) |
| 3099 |
/// .try_get_matches_from(vec!["", "--opt", "value"]); |
| 3100 |
/// |
| 3101 |
/// let m = r.unwrap(); |
| 3102 |
/// assert_eq!( |
| 3103 |
/// m.get_many::<String>("args").unwrap().collect::<Vec<_>>(), |
| 3104 |
/// ["df1", "df2"] |
| 3105 |
/// ); |
| 3106 |
/// ``` |
| 3107 |
/// |
| 3108 |
/// [`Arg::default_value_if`]: Arg::default_value_if() |
| 3109 |
#[must_use] |
| 3110 |
pub fn default_values_if( |
| 3111 |
mut self, |
| 3112 |
arg_id: impl Into<Id>, |
| 3113 |
predicate: impl Into<ArgPredicate>, |
| 3114 |
defaults: impl IntoIterator<Item = impl Into<OsStr>>, |
| 3115 |
) -> Self { |
| 3116 |
self.default_vals_ifs.push(( |
| 3117 |
arg_id.into(), |
| 3118 |
predicate.into(), |
| 3119 |
Some(defaults.into_iter().map(|item| item.into()).collect()), |
| 3120 |
)); |
| 3121 |
self |
| 3122 |
} |
| 3123 |
|
| 3124 |
#[must_use] |
| 3125 |
#[doc(hidden)] |
| 3126 |
#[cfg_attr( |
| 3127 |
feature = "deprecated", |
| 3128 |
deprecated(since = "4.0.0", note = "Replaced with `Arg::default_value_if`") |
| 3129 |
)] |
| 3130 |
pub fn default_value_if_os( |
| 3131 |
self, |
| 3132 |
arg_id: impl Into<Id>, |
| 3133 |
predicate: impl Into<ArgPredicate>, |
| 3134 |
default: impl IntoResettable<OsStr>, |
| 3135 |
) -> Self { |
| 3136 |
self.default_value_if(arg_id, predicate, default) |
| 3137 |
} |
| 3138 |
|
| 3139 |
/// Specifies multiple values and conditions in the same manner as [`Arg::default_value_if`]. |
| 3140 |
/// |
| 3141 |
/// The method takes a slice of tuples in the `(arg, predicate, default)` format. |
| 3142 |
/// |
| 3143 |
/// Like with command-line values, this will be split by [`Arg::value_delimiter`]. |
| 3144 |
/// |
| 3145 |
/// <div class="warning"> |
| 3146 |
/// |
| 3147 |
/// **NOTE**: The conditions are stored in order and evaluated in the same order. I.e. the first |
| 3148 |
/// if multiple conditions are true, the first one found will be applied and the ultimate value. |
| 3149 |
/// |
| 3150 |
/// </div> |
| 3151 |
/// |
| 3152 |
/// # Examples |
| 3153 |
/// |
| 3154 |
/// First we use the default value only if another arg is present at runtime. |
| 3155 |
/// |
| 3156 |
/// ```rust |
| 3157 |
/// # use clap_builder as clap; |
| 3158 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3159 |
/// let m = Command::new("prog") |
| 3160 |
/// .arg(Arg::new("flag") |
| 3161 |
/// .long("flag") |
| 3162 |
/// .action(ArgAction::SetTrue)) |
| 3163 |
/// .arg(Arg::new("opt") |
| 3164 |
/// .long("opt") |
| 3165 |
/// .action(ArgAction::Set)) |
| 3166 |
/// .arg(Arg::new("other") |
| 3167 |
/// .long("other") |
| 3168 |
/// .default_value_ifs([ |
| 3169 |
/// ("flag", "true", Some("default")), |
| 3170 |
/// ("opt", "channel", Some("chan")), |
| 3171 |
/// ])) |
| 3172 |
/// .get_matches_from(vec![ |
| 3173 |
/// "prog", "--opt", "channel" |
| 3174 |
/// ]); |
| 3175 |
/// |
| 3176 |
/// assert_eq!(m.get_one::<String>("other").unwrap(), "chan"); |
| 3177 |
/// ``` |
| 3178 |
/// |
| 3179 |
/// Next we run the same test, but without providing `--flag`. |
| 3180 |
/// |
| 3181 |
/// ```rust |
| 3182 |
/// # use clap_builder as clap; |
| 3183 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3184 |
/// let m = Command::new("prog") |
| 3185 |
/// .arg(Arg::new("flag") |
| 3186 |
/// .long("flag") |
| 3187 |
/// .action(ArgAction::SetTrue)) |
| 3188 |
/// .arg(Arg::new("other") |
| 3189 |
/// .long("other") |
| 3190 |
/// .default_value_ifs([ |
| 3191 |
/// ("flag", "true", Some("default")), |
| 3192 |
/// ("opt", "channel", Some("chan")), |
| 3193 |
/// ])) |
| 3194 |
/// .get_matches_from(vec![ |
| 3195 |
/// "prog" |
| 3196 |
/// ]); |
| 3197 |
/// |
| 3198 |
/// assert_eq!(m.get_one::<String>("other"), None); |
| 3199 |
/// ``` |
| 3200 |
/// |
| 3201 |
/// We can also see that these values are applied in order, and if more than one condition is |
| 3202 |
/// true, only the first evaluated "wins" |
| 3203 |
/// |
| 3204 |
/// ```rust |
| 3205 |
/// # use clap_builder as clap; |
| 3206 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3207 |
/// # use clap::builder::ArgPredicate; |
| 3208 |
/// let m = Command::new("prog") |
| 3209 |
/// .arg(Arg::new("flag") |
| 3210 |
/// .long("flag") |
| 3211 |
/// .action(ArgAction::SetTrue)) |
| 3212 |
/// .arg(Arg::new("opt") |
| 3213 |
/// .long("opt") |
| 3214 |
/// .action(ArgAction::Set)) |
| 3215 |
/// .arg(Arg::new("other") |
| 3216 |
/// .long("other") |
| 3217 |
/// .default_value_ifs([ |
| 3218 |
/// ("flag", ArgPredicate::IsPresent, Some("default")), |
| 3219 |
/// ("opt", ArgPredicate::Equals("channel".into()), Some("chan")), |
| 3220 |
/// ])) |
| 3221 |
/// .get_matches_from(vec![ |
| 3222 |
/// "prog", "--opt", "channel", "--flag" |
| 3223 |
/// ]); |
| 3224 |
/// |
| 3225 |
/// assert_eq!(m.get_one::<String>("other").unwrap(), "default"); |
| 3226 |
/// ``` |
| 3227 |
/// [`Arg::action(ArgAction::Set)`]: Arg::action() |
| 3228 |
/// [`Arg::default_value_if`]: Arg::default_value_if() |
| 3229 |
#[must_use] |
| 3230 |
pub fn default_value_ifs( |
| 3231 |
mut self, |
| 3232 |
ifs: impl IntoIterator< |
| 3233 |
Item = ( |
| 3234 |
impl Into<Id>, |
| 3235 |
impl Into<ArgPredicate>, |
| 3236 |
impl IntoResettable<OsStr>, |
| 3237 |
), |
| 3238 |
>, |
| 3239 |
) -> Self { |
| 3240 |
for (arg, predicate, default) in ifs { |
| 3241 |
self = self.default_value_if(arg, predicate, default); |
| 3242 |
} |
| 3243 |
self |
| 3244 |
} |
| 3245 |
|
| 3246 |
/// Specifies multiple values and conditions in the same manner as [`Arg::default_values_if`]. |
| 3247 |
/// |
| 3248 |
/// See [`Arg::default_values_if`]. |
| 3249 |
/// |
| 3250 |
/// [`Arg::default_values_if`]: Arg::default_values_if() |
| 3251 |
#[must_use] |
| 3252 |
pub fn default_values_ifs( |
| 3253 |
mut self, |
| 3254 |
ifs: impl IntoIterator< |
| 3255 |
Item = ( |
| 3256 |
impl Into<Id>, |
| 3257 |
impl Into<ArgPredicate>, |
| 3258 |
impl IntoIterator<Item = impl Into<OsStr>>, |
| 3259 |
), |
| 3260 |
>, |
| 3261 |
) -> Self { |
| 3262 |
for (arg, predicate, default) in ifs { |
| 3263 |
self = self.default_values_if(arg, predicate, default); |
| 3264 |
} |
| 3265 |
self |
| 3266 |
} |
| 3267 |
|
| 3268 |
#[must_use] |
| 3269 |
#[doc(hidden)] |
| 3270 |
#[cfg_attr( |
| 3271 |
feature = "deprecated", |
| 3272 |
deprecated(since = "4.0.0", note = "Replaced with `Arg::default_value_ifs`") |
| 3273 |
)] |
| 3274 |
pub fn default_value_ifs_os( |
| 3275 |
self, |
| 3276 |
ifs: impl IntoIterator< |
| 3277 |
Item = ( |
| 3278 |
impl Into<Id>, |
| 3279 |
impl Into<ArgPredicate>, |
| 3280 |
impl IntoResettable<OsStr>, |
| 3281 |
), |
| 3282 |
>, |
| 3283 |
) -> Self { |
| 3284 |
self.default_value_ifs(ifs) |
| 3285 |
} |
| 3286 |
|
| 3287 |
/// Set this arg as [required] as long as the specified argument is not present at runtime. |
| 3288 |
/// |
| 3289 |
/// <div class="warning"> |
| 3290 |
/// |
| 3291 |
/// **TIP:** Using `Arg::required_unless_present` implies [`Arg::required`] and is therefore not |
| 3292 |
/// mandatory to also set. |
| 3293 |
/// |
| 3294 |
/// </div> |
| 3295 |
/// |
| 3296 |
/// # Examples |
| 3297 |
/// |
| 3298 |
/// ```rust |
| 3299 |
/// # use clap_builder as clap; |
| 3300 |
/// # use clap::Arg; |
| 3301 |
/// Arg::new("config") |
| 3302 |
/// .required_unless_present("debug") |
| 3303 |
/// # ; |
| 3304 |
/// ``` |
| 3305 |
/// |
| 3306 |
/// In the following example, the required argument is *not* provided, |
| 3307 |
/// but it's not an error because the `unless` arg has been supplied. |
| 3308 |
/// |
| 3309 |
/// ```rust |
| 3310 |
/// # use clap_builder as clap; |
| 3311 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3312 |
/// let res = Command::new("prog") |
| 3313 |
/// .arg(Arg::new("cfg") |
| 3314 |
/// .required_unless_present("dbg") |
| 3315 |
/// .action(ArgAction::Set) |
| 3316 |
/// .long("config")) |
| 3317 |
/// .arg(Arg::new("dbg") |
| 3318 |
/// .long("debug") |
| 3319 |
/// .action(ArgAction::SetTrue)) |
| 3320 |
/// .try_get_matches_from(vec![ |
| 3321 |
/// "prog", "--debug" |
| 3322 |
/// ]); |
| 3323 |
/// |
| 3324 |
/// assert!(res.is_ok()); |
| 3325 |
/// ``` |
| 3326 |
/// |
| 3327 |
/// Setting `Arg::required_unless_present(name)` and *not* supplying `name` or this arg is an error. |
| 3328 |
/// |
| 3329 |
/// ```rust |
| 3330 |
/// # use clap_builder as clap; |
| 3331 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3332 |
/// let res = Command::new("prog") |
| 3333 |
/// .arg(Arg::new("cfg") |
| 3334 |
/// .required_unless_present("dbg") |
| 3335 |
/// .action(ArgAction::Set) |
| 3336 |
/// .long("config")) |
| 3337 |
/// .arg(Arg::new("dbg") |
| 3338 |
/// .long("debug")) |
| 3339 |
/// .try_get_matches_from(vec![ |
| 3340 |
/// "prog" |
| 3341 |
/// ]); |
| 3342 |
/// |
| 3343 |
/// assert!(res.is_err()); |
| 3344 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3345 |
/// ``` |
| 3346 |
/// [required]: Arg::required() |
| 3347 |
#[must_use] |
| 3348 |
pub fn required_unless_present(mut self, arg_id: impl IntoResettable<Id>) -> Self { |
| 3349 |
if let Some(arg_id) = arg_id.into_resettable().into_option() { |
| 3350 |
self.r_unless.push(arg_id); |
| 3351 |
} else { |
| 3352 |
self.r_unless.clear(); |
| 3353 |
} |
| 3354 |
self |
| 3355 |
} |
| 3356 |
|
| 3357 |
/// Sets this arg as [required] unless *all* of the specified arguments are present at runtime. |
| 3358 |
/// |
| 3359 |
/// In other words, parsing will succeed only if user either |
| 3360 |
/// * supplies the `self` arg. |
| 3361 |
/// * supplies *all* of the `names` arguments. |
| 3362 |
/// |
| 3363 |
/// <div class="warning"> |
| 3364 |
/// |
| 3365 |
/// **NOTE:** If you wish for this argument to only be required unless *any of* these args are |
| 3366 |
/// present see [`Arg::required_unless_present_any`] |
| 3367 |
/// |
| 3368 |
/// </div> |
| 3369 |
/// |
| 3370 |
/// # Examples |
| 3371 |
/// |
| 3372 |
/// ```rust |
| 3373 |
/// # use clap_builder as clap; |
| 3374 |
/// # use clap::Arg; |
| 3375 |
/// Arg::new("config") |
| 3376 |
/// .required_unless_present_all(["cfg", "dbg"]) |
| 3377 |
/// # ; |
| 3378 |
/// ``` |
| 3379 |
/// |
| 3380 |
/// In the following example, the required argument is *not* provided, but it's not an error |
| 3381 |
/// because *all* of the `names` args have been supplied. |
| 3382 |
/// |
| 3383 |
/// ```rust |
| 3384 |
/// # use clap_builder as clap; |
| 3385 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3386 |
/// let res = Command::new("prog") |
| 3387 |
/// .arg(Arg::new("cfg") |
| 3388 |
/// .required_unless_present_all(["dbg", "infile"]) |
| 3389 |
/// .action(ArgAction::Set) |
| 3390 |
/// .long("config")) |
| 3391 |
/// .arg(Arg::new("dbg") |
| 3392 |
/// .long("debug") |
| 3393 |
/// .action(ArgAction::SetTrue)) |
| 3394 |
/// .arg(Arg::new("infile") |
| 3395 |
/// .short('i') |
| 3396 |
/// .action(ArgAction::Set)) |
| 3397 |
/// .try_get_matches_from(vec![ |
| 3398 |
/// "prog", "--debug", "-i", "file" |
| 3399 |
/// ]); |
| 3400 |
/// |
| 3401 |
/// assert!(res.is_ok()); |
| 3402 |
/// ``` |
| 3403 |
/// |
| 3404 |
/// Setting [`Arg::required_unless_present_all(names)`] and *not* supplying |
| 3405 |
/// either *all* of `unless` args or the `self` arg is an error. |
| 3406 |
/// |
| 3407 |
/// ```rust |
| 3408 |
/// # use clap_builder as clap; |
| 3409 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3410 |
/// let res = Command::new("prog") |
| 3411 |
/// .arg(Arg::new("cfg") |
| 3412 |
/// .required_unless_present_all(["dbg", "infile"]) |
| 3413 |
/// .action(ArgAction::Set) |
| 3414 |
/// .long("config")) |
| 3415 |
/// .arg(Arg::new("dbg") |
| 3416 |
/// .long("debug") |
| 3417 |
/// .action(ArgAction::SetTrue)) |
| 3418 |
/// .arg(Arg::new("infile") |
| 3419 |
/// .short('i') |
| 3420 |
/// .action(ArgAction::Set)) |
| 3421 |
/// .try_get_matches_from(vec![ |
| 3422 |
/// "prog" |
| 3423 |
/// ]); |
| 3424 |
/// |
| 3425 |
/// assert!(res.is_err()); |
| 3426 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3427 |
/// ``` |
| 3428 |
/// [required]: Arg::required() |
| 3429 |
/// [`Arg::required_unless_present_any`]: Arg::required_unless_present_any() |
| 3430 |
/// [`Arg::required_unless_present_all(names)`]: Arg::required_unless_present_all() |
| 3431 |
#[must_use] |
| 3432 |
pub fn required_unless_present_all( |
| 3433 |
mut self, |
| 3434 |
names: impl IntoIterator<Item = impl Into<Id>>, |
| 3435 |
) -> Self { |
| 3436 |
self.r_unless_all.extend(names.into_iter().map(Into::into)); |
| 3437 |
self |
| 3438 |
} |
| 3439 |
|
| 3440 |
/// Sets this arg as [required] unless *any* of the specified arguments are present at runtime. |
| 3441 |
/// |
| 3442 |
/// In other words, parsing will succeed only if user either |
| 3443 |
/// * supplies the `self` arg. |
| 3444 |
/// * supplies *one or more* of the `unless` arguments. |
| 3445 |
/// |
| 3446 |
/// <div class="warning"> |
| 3447 |
/// |
| 3448 |
/// **NOTE:** If you wish for this argument to be required unless *all of* these args are |
| 3449 |
/// present see [`Arg::required_unless_present_all`] |
| 3450 |
/// |
| 3451 |
/// </div> |
| 3452 |
/// |
| 3453 |
/// # Examples |
| 3454 |
/// |
| 3455 |
/// ```rust |
| 3456 |
/// # use clap_builder as clap; |
| 3457 |
/// # use clap::Arg; |
| 3458 |
/// Arg::new("config") |
| 3459 |
/// .required_unless_present_any(["cfg", "dbg"]) |
| 3460 |
/// # ; |
| 3461 |
/// ``` |
| 3462 |
/// |
| 3463 |
/// Setting [`Arg::required_unless_present_any(names)`] requires that the argument be used at runtime |
| 3464 |
/// *unless* *at least one of* the args in `names` are present. In the following example, the |
| 3465 |
/// required argument is *not* provided, but it's not an error because one the `unless` args |
| 3466 |
/// have been supplied. |
| 3467 |
/// |
| 3468 |
/// ```rust |
| 3469 |
/// # use clap_builder as clap; |
| 3470 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3471 |
/// let res = Command::new("prog") |
| 3472 |
/// .arg(Arg::new("cfg") |
| 3473 |
/// .required_unless_present_any(["dbg", "infile"]) |
| 3474 |
/// .action(ArgAction::Set) |
| 3475 |
/// .long("config")) |
| 3476 |
/// .arg(Arg::new("dbg") |
| 3477 |
/// .long("debug") |
| 3478 |
/// .action(ArgAction::SetTrue)) |
| 3479 |
/// .arg(Arg::new("infile") |
| 3480 |
/// .short('i') |
| 3481 |
/// .action(ArgAction::Set)) |
| 3482 |
/// .try_get_matches_from(vec![ |
| 3483 |
/// "prog", "--debug" |
| 3484 |
/// ]); |
| 3485 |
/// |
| 3486 |
/// assert!(res.is_ok()); |
| 3487 |
/// ``` |
| 3488 |
/// |
| 3489 |
/// Setting [`Arg::required_unless_present_any(names)`] and *not* supplying *at least one of* `names` |
| 3490 |
/// or this arg is an error. |
| 3491 |
/// |
| 3492 |
/// ```rust |
| 3493 |
/// # use clap_builder as clap; |
| 3494 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3495 |
/// let res = Command::new("prog") |
| 3496 |
/// .arg(Arg::new("cfg") |
| 3497 |
/// .required_unless_present_any(["dbg", "infile"]) |
| 3498 |
/// .action(ArgAction::Set) |
| 3499 |
/// .long("config")) |
| 3500 |
/// .arg(Arg::new("dbg") |
| 3501 |
/// .long("debug") |
| 3502 |
/// .action(ArgAction::SetTrue)) |
| 3503 |
/// .arg(Arg::new("infile") |
| 3504 |
/// .short('i') |
| 3505 |
/// .action(ArgAction::Set)) |
| 3506 |
/// .try_get_matches_from(vec![ |
| 3507 |
/// "prog" |
| 3508 |
/// ]); |
| 3509 |
/// |
| 3510 |
/// assert!(res.is_err()); |
| 3511 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3512 |
/// ``` |
| 3513 |
/// [required]: Arg::required() |
| 3514 |
/// [`Arg::required_unless_present_any(names)`]: Arg::required_unless_present_any() |
| 3515 |
/// [`Arg::required_unless_present_all`]: Arg::required_unless_present_all() |
| 3516 |
#[must_use] |
| 3517 |
pub fn required_unless_present_any( |
| 3518 |
mut self, |
| 3519 |
names: impl IntoIterator<Item = impl Into<Id>>, |
| 3520 |
) -> Self { |
| 3521 |
self.r_unless.extend(names.into_iter().map(Into::into)); |
| 3522 |
self |
| 3523 |
} |
| 3524 |
|
| 3525 |
/// This argument is [required] only if the specified `arg` is present at runtime and its value |
| 3526 |
/// equals `val`. |
| 3527 |
/// |
| 3528 |
/// # Examples |
| 3529 |
/// |
| 3530 |
/// ```rust |
| 3531 |
/// # use clap_builder as clap; |
| 3532 |
/// # use clap::Arg; |
| 3533 |
/// Arg::new("config") |
| 3534 |
/// .required_if_eq("other_arg", "value") |
| 3535 |
/// # ; |
| 3536 |
/// ``` |
| 3537 |
/// |
| 3538 |
/// ```rust |
| 3539 |
/// # use clap_builder as clap; |
| 3540 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3541 |
/// let res = Command::new("prog") |
| 3542 |
/// .arg(Arg::new("cfg") |
| 3543 |
/// .action(ArgAction::Set) |
| 3544 |
/// .required_if_eq("other", "special") |
| 3545 |
/// .long("config")) |
| 3546 |
/// .arg(Arg::new("other") |
| 3547 |
/// .long("other") |
| 3548 |
/// .action(ArgAction::Set)) |
| 3549 |
/// .try_get_matches_from(vec![ |
| 3550 |
/// "prog", "--other", "not-special" |
| 3551 |
/// ]); |
| 3552 |
/// |
| 3553 |
/// assert!(res.is_ok()); // We didn't use --other=special, so "cfg" wasn't required |
| 3554 |
/// |
| 3555 |
/// let res = Command::new("prog") |
| 3556 |
/// .arg(Arg::new("cfg") |
| 3557 |
/// .action(ArgAction::Set) |
| 3558 |
/// .required_if_eq("other", "special") |
| 3559 |
/// .long("config")) |
| 3560 |
/// .arg(Arg::new("other") |
| 3561 |
/// .long("other") |
| 3562 |
/// .action(ArgAction::Set)) |
| 3563 |
/// .try_get_matches_from(vec![ |
| 3564 |
/// "prog", "--other", "special" |
| 3565 |
/// ]); |
| 3566 |
/// |
| 3567 |
/// // We did use --other=special so "cfg" had become required but was missing. |
| 3568 |
/// assert!(res.is_err()); |
| 3569 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3570 |
/// |
| 3571 |
/// let res = Command::new("prog") |
| 3572 |
/// .arg(Arg::new("cfg") |
| 3573 |
/// .action(ArgAction::Set) |
| 3574 |
/// .required_if_eq("other", "special") |
| 3575 |
/// .long("config")) |
| 3576 |
/// .arg(Arg::new("other") |
| 3577 |
/// .long("other") |
| 3578 |
/// .action(ArgAction::Set)) |
| 3579 |
/// .try_get_matches_from(vec![ |
| 3580 |
/// "prog", "--other", "SPECIAL" |
| 3581 |
/// ]); |
| 3582 |
/// |
| 3583 |
/// // By default, the comparison is case-sensitive, so "cfg" wasn't required |
| 3584 |
/// assert!(res.is_ok()); |
| 3585 |
/// |
| 3586 |
/// let res = Command::new("prog") |
| 3587 |
/// .arg(Arg::new("cfg") |
| 3588 |
/// .action(ArgAction::Set) |
| 3589 |
/// .required_if_eq("other", "special") |
| 3590 |
/// .long("config")) |
| 3591 |
/// .arg(Arg::new("other") |
| 3592 |
/// .long("other") |
| 3593 |
/// .ignore_case(true) |
| 3594 |
/// .action(ArgAction::Set)) |
| 3595 |
/// .try_get_matches_from(vec![ |
| 3596 |
/// "prog", "--other", "SPECIAL" |
| 3597 |
/// ]); |
| 3598 |
/// |
| 3599 |
/// // However, case-insensitive comparisons can be enabled. This typically occurs when using Arg::possible_values(). |
| 3600 |
/// assert!(res.is_err()); |
| 3601 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3602 |
/// ``` |
| 3603 |
/// [`Arg::requires(name)`]: Arg::requires() |
| 3604 |
/// [Conflicting]: Arg::conflicts_with() |
| 3605 |
/// [required]: Arg::required() |
| 3606 |
#[must_use] |
| 3607 |
pub fn required_if_eq(mut self, arg_id: impl Into<Id>, val: impl Into<OsStr>) -> Self { |
| 3608 |
self.r_ifs.push((arg_id.into(), val.into())); |
| 3609 |
self |
| 3610 |
} |
| 3611 |
|
| 3612 |
/// Specify this argument is [required] based on multiple conditions. |
| 3613 |
/// |
| 3614 |
/// The conditions are set up in a `(arg, val)` style tuple. The requirement will only become |
| 3615 |
/// valid if one of the specified `arg`'s value equals its corresponding `val`. |
| 3616 |
/// |
| 3617 |
/// # Examples |
| 3618 |
/// |
| 3619 |
/// ```rust |
| 3620 |
/// # use clap_builder as clap; |
| 3621 |
/// # use clap::Arg; |
| 3622 |
/// Arg::new("config") |
| 3623 |
/// .required_if_eq_any([ |
| 3624 |
/// ("extra", "val"), |
| 3625 |
/// ("option", "spec") |
| 3626 |
/// ]) |
| 3627 |
/// # ; |
| 3628 |
/// ``` |
| 3629 |
/// |
| 3630 |
/// Setting `Arg::required_if_eq_any([(arg, val)])` makes this arg required if any of the `arg`s |
| 3631 |
/// are used at runtime and it's corresponding value is equal to `val`. If the `arg`'s value is |
| 3632 |
/// anything other than `val`, this argument isn't required. |
| 3633 |
/// |
| 3634 |
/// ```rust |
| 3635 |
/// # use clap_builder as clap; |
| 3636 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3637 |
/// let res = Command::new("prog") |
| 3638 |
/// .arg(Arg::new("cfg") |
| 3639 |
/// .required_if_eq_any([ |
| 3640 |
/// ("extra", "val"), |
| 3641 |
/// ("option", "spec") |
| 3642 |
/// ]) |
| 3643 |
/// .action(ArgAction::Set) |
| 3644 |
/// .long("config")) |
| 3645 |
/// .arg(Arg::new("extra") |
| 3646 |
/// .action(ArgAction::Set) |
| 3647 |
/// .long("extra")) |
| 3648 |
/// .arg(Arg::new("option") |
| 3649 |
/// .action(ArgAction::Set) |
| 3650 |
/// .long("option")) |
| 3651 |
/// .try_get_matches_from(vec![ |
| 3652 |
/// "prog", "--option", "other" |
| 3653 |
/// ]); |
| 3654 |
/// |
| 3655 |
/// assert!(res.is_ok()); // We didn't use --option=spec, or --extra=val so "cfg" isn't required |
| 3656 |
/// ``` |
| 3657 |
/// |
| 3658 |
/// Setting `Arg::required_if_eq_any([(arg, val)])` and having any of the `arg`s used with its |
| 3659 |
/// value of `val` but *not* using this arg is an error. |
| 3660 |
/// |
| 3661 |
/// ```rust |
| 3662 |
/// # use clap_builder as clap; |
| 3663 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3664 |
/// let res = Command::new("prog") |
| 3665 |
/// .arg(Arg::new("cfg") |
| 3666 |
/// .required_if_eq_any([ |
| 3667 |
/// ("extra", "val"), |
| 3668 |
/// ("option", "spec") |
| 3669 |
/// ]) |
| 3670 |
/// .action(ArgAction::Set) |
| 3671 |
/// .long("config")) |
| 3672 |
/// .arg(Arg::new("extra") |
| 3673 |
/// .action(ArgAction::Set) |
| 3674 |
/// .long("extra")) |
| 3675 |
/// .arg(Arg::new("option") |
| 3676 |
/// .action(ArgAction::Set) |
| 3677 |
/// .long("option")) |
| 3678 |
/// .try_get_matches_from(vec![ |
| 3679 |
/// "prog", "--option", "spec" |
| 3680 |
/// ]); |
| 3681 |
/// |
| 3682 |
/// assert!(res.is_err()); |
| 3683 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3684 |
/// ``` |
| 3685 |
/// [`Arg::requires(name)`]: Arg::requires() |
| 3686 |
/// [Conflicting]: Arg::conflicts_with() |
| 3687 |
/// [required]: Arg::required() |
| 3688 |
#[must_use] |
| 3689 |
pub fn required_if_eq_any( |
| 3690 |
mut self, |
| 3691 |
ifs: impl IntoIterator<Item = (impl Into<Id>, impl Into<OsStr>)>, |
| 3692 |
) -> Self { |
| 3693 |
self.r_ifs |
| 3694 |
.extend(ifs.into_iter().map(|(id, val)| (id.into(), val.into()))); |
| 3695 |
self |
| 3696 |
} |
| 3697 |
|
| 3698 |
/// Specify this argument is [required] based on multiple conditions. |
| 3699 |
/// |
| 3700 |
/// The conditions are set up in a `(arg, val)` style tuple. The requirement will only become |
| 3701 |
/// valid if every one of the specified `arg`'s value equals its corresponding `val`. |
| 3702 |
/// |
| 3703 |
/// # Examples |
| 3704 |
/// |
| 3705 |
/// ```rust |
| 3706 |
/// # use clap_builder as clap; |
| 3707 |
/// # use clap::Arg; |
| 3708 |
/// Arg::new("config") |
| 3709 |
/// .required_if_eq_all([ |
| 3710 |
/// ("extra", "val"), |
| 3711 |
/// ("option", "spec") |
| 3712 |
/// ]) |
| 3713 |
/// # ; |
| 3714 |
/// ``` |
| 3715 |
/// |
| 3716 |
/// Setting `Arg::required_if_eq_all([(arg, val)])` makes this arg required if all of the `arg`s |
| 3717 |
/// are used at runtime and every value is equal to its corresponding `val`. If the `arg`'s value is |
| 3718 |
/// anything other than `val`, this argument isn't required. |
| 3719 |
/// |
| 3720 |
/// ```rust |
| 3721 |
/// # use clap_builder as clap; |
| 3722 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3723 |
/// let res = Command::new("prog") |
| 3724 |
/// .arg(Arg::new("cfg") |
| 3725 |
/// .required_if_eq_all([ |
| 3726 |
/// ("extra", "val"), |
| 3727 |
/// ("option", "spec") |
| 3728 |
/// ]) |
| 3729 |
/// .action(ArgAction::Set) |
| 3730 |
/// .long("config")) |
| 3731 |
/// .arg(Arg::new("extra") |
| 3732 |
/// .action(ArgAction::Set) |
| 3733 |
/// .long("extra")) |
| 3734 |
/// .arg(Arg::new("option") |
| 3735 |
/// .action(ArgAction::Set) |
| 3736 |
/// .long("option")) |
| 3737 |
/// .try_get_matches_from(vec![ |
| 3738 |
/// "prog", "--option", "spec" |
| 3739 |
/// ]); |
| 3740 |
/// |
| 3741 |
/// assert!(res.is_ok()); // We didn't use --option=spec --extra=val so "cfg" isn't required |
| 3742 |
/// ``` |
| 3743 |
/// |
| 3744 |
/// Setting `Arg::required_if_eq_all([(arg, val)])` and having all of the `arg`s used with its |
| 3745 |
/// value of `val` but *not* using this arg is an error. |
| 3746 |
/// |
| 3747 |
/// ```rust |
| 3748 |
/// # use clap_builder as clap; |
| 3749 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3750 |
/// let res = Command::new("prog") |
| 3751 |
/// .arg(Arg::new("cfg") |
| 3752 |
/// .required_if_eq_all([ |
| 3753 |
/// ("extra", "val"), |
| 3754 |
/// ("option", "spec") |
| 3755 |
/// ]) |
| 3756 |
/// .action(ArgAction::Set) |
| 3757 |
/// .long("config")) |
| 3758 |
/// .arg(Arg::new("extra") |
| 3759 |
/// .action(ArgAction::Set) |
| 3760 |
/// .long("extra")) |
| 3761 |
/// .arg(Arg::new("option") |
| 3762 |
/// .action(ArgAction::Set) |
| 3763 |
/// .long("option")) |
| 3764 |
/// .try_get_matches_from(vec![ |
| 3765 |
/// "prog", "--extra", "val", "--option", "spec" |
| 3766 |
/// ]); |
| 3767 |
/// |
| 3768 |
/// assert!(res.is_err()); |
| 3769 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3770 |
/// ``` |
| 3771 |
/// [required]: Arg::required() |
| 3772 |
#[must_use] |
| 3773 |
pub fn required_if_eq_all( |
| 3774 |
mut self, |
| 3775 |
ifs: impl IntoIterator<Item = (impl Into<Id>, impl Into<OsStr>)>, |
| 3776 |
) -> Self { |
| 3777 |
self.r_ifs_all |
| 3778 |
.extend(ifs.into_iter().map(|(id, val)| (id.into(), val.into()))); |
| 3779 |
self |
| 3780 |
} |
| 3781 |
|
| 3782 |
/// Require another argument if this arg matches the [`ArgPredicate`] |
| 3783 |
/// |
| 3784 |
/// This method takes `value, another_arg` pair. At runtime, clap will check |
| 3785 |
/// if this arg (`self`) matches the [`ArgPredicate`]. |
| 3786 |
/// If it does, `another_arg` will be marked as required. |
| 3787 |
/// |
| 3788 |
/// # Examples |
| 3789 |
/// |
| 3790 |
/// ```rust |
| 3791 |
/// # use clap_builder as clap; |
| 3792 |
/// # use clap::Arg; |
| 3793 |
/// Arg::new("config") |
| 3794 |
/// .requires_if("val", "arg") |
| 3795 |
/// # ; |
| 3796 |
/// ``` |
| 3797 |
/// |
| 3798 |
/// Setting `Arg::requires_if(val, arg)` requires that the `arg` be used at runtime if the |
| 3799 |
/// defining argument's value is equal to `val`. If the defining argument is anything other than |
| 3800 |
/// `val`, the other argument isn't required. |
| 3801 |
/// |
| 3802 |
/// ```rust |
| 3803 |
/// # use clap_builder as clap; |
| 3804 |
/// # use clap::{Command, Arg, ArgAction}; |
| 3805 |
/// let res = Command::new("prog") |
| 3806 |
/// .arg(Arg::new("cfg") |
| 3807 |
/// .action(ArgAction::Set) |
| 3808 |
/// .requires_if("my.cfg", "other") |
| 3809 |
/// .long("config")) |
| 3810 |
/// .arg(Arg::new("other")) |
| 3811 |
/// .try_get_matches_from(vec![ |
| 3812 |
/// "prog", "--config", "some.cfg" |
| 3813 |
/// ]); |
| 3814 |
/// |
| 3815 |
/// assert!(res.is_ok()); // We didn't use --config=my.cfg, so other wasn't required |
| 3816 |
/// ``` |
| 3817 |
/// |
| 3818 |
/// Setting `Arg::requires_if(val, arg)` and setting the value to `val` but *not* supplying |
| 3819 |
/// `arg` is an error. |
| 3820 |
/// |
| 3821 |
/// ```rust |
| 3822 |
/// # use clap_builder as clap; |
| 3823 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3824 |
/// let res = Command::new("prog") |
| 3825 |
/// .arg(Arg::new("cfg") |
| 3826 |
/// .action(ArgAction::Set) |
| 3827 |
/// .requires_if("my.cfg", "input") |
| 3828 |
/// .long("config")) |
| 3829 |
/// .arg(Arg::new("input")) |
| 3830 |
/// .try_get_matches_from(vec![ |
| 3831 |
/// "prog", "--config", "my.cfg" |
| 3832 |
/// ]); |
| 3833 |
/// |
| 3834 |
/// assert!(res.is_err()); |
| 3835 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3836 |
/// ``` |
| 3837 |
/// [`Arg::requires(name)`]: Arg::requires() |
| 3838 |
/// [Conflicting]: Arg::conflicts_with() |
| 3839 |
/// [override]: Arg::overrides_with() |
| 3840 |
#[must_use] |
| 3841 |
pub fn requires_if(mut self, val: impl Into<ArgPredicate>, arg_id: impl Into<Id>) -> Self { |
| 3842 |
self.requires.push((val.into(), arg_id.into())); |
| 3843 |
self |
| 3844 |
} |
| 3845 |
|
| 3846 |
/// Allows multiple conditional requirements. |
| 3847 |
/// |
| 3848 |
/// The requirement will only become valid if this arg's value matches the |
| 3849 |
/// [`ArgPredicate`]. |
| 3850 |
/// |
| 3851 |
/// # Examples |
| 3852 |
/// |
| 3853 |
/// ```rust |
| 3854 |
/// # use clap_builder as clap; |
| 3855 |
/// # use clap::Arg; |
| 3856 |
/// Arg::new("config") |
| 3857 |
/// .requires_ifs([ |
| 3858 |
/// ("val", "arg"), |
| 3859 |
/// ("other_val", "arg2"), |
| 3860 |
/// ]) |
| 3861 |
/// # ; |
| 3862 |
/// ``` |
| 3863 |
/// |
| 3864 |
/// Setting `Arg::requires_ifs(["val", "arg"])` requires that the `arg` be used at runtime if the |
| 3865 |
/// defining argument's value is equal to `val`. If the defining argument's value is anything other |
| 3866 |
/// than `val`, `arg` isn't required. |
| 3867 |
/// |
| 3868 |
/// ```rust |
| 3869 |
/// # use clap_builder as clap; |
| 3870 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3871 |
/// let res = Command::new("prog") |
| 3872 |
/// .arg(Arg::new("cfg") |
| 3873 |
/// .action(ArgAction::Set) |
| 3874 |
/// .requires_ifs([ |
| 3875 |
/// ("special.conf", "opt"), |
| 3876 |
/// ("other.conf", "other"), |
| 3877 |
/// ]) |
| 3878 |
/// .long("config")) |
| 3879 |
/// .arg(Arg::new("opt") |
| 3880 |
/// .long("option") |
| 3881 |
/// .action(ArgAction::Set)) |
| 3882 |
/// .arg(Arg::new("other")) |
| 3883 |
/// .try_get_matches_from(vec![ |
| 3884 |
/// "prog", "--config", "special.conf" |
| 3885 |
/// ]); |
| 3886 |
/// |
| 3887 |
/// assert!(res.is_err()); // We used --config=special.conf so --option <val> is required |
| 3888 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3889 |
/// ``` |
| 3890 |
/// |
| 3891 |
/// Setting `Arg::requires_ifs` with [`ArgPredicate::IsPresent`] and *not* supplying all the |
| 3892 |
/// arguments is an error. |
| 3893 |
/// |
| 3894 |
/// ```rust |
| 3895 |
/// # use clap_builder as clap; |
| 3896 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction, builder::ArgPredicate}; |
| 3897 |
/// let res = Command::new("prog") |
| 3898 |
/// .arg(Arg::new("cfg") |
| 3899 |
/// .action(ArgAction::Set) |
| 3900 |
/// .requires_ifs([ |
| 3901 |
/// (ArgPredicate::IsPresent, "input"), |
| 3902 |
/// (ArgPredicate::IsPresent, "output"), |
| 3903 |
/// ]) |
| 3904 |
/// .long("config")) |
| 3905 |
/// .arg(Arg::new("input")) |
| 3906 |
/// .arg(Arg::new("output")) |
| 3907 |
/// .try_get_matches_from(vec![ |
| 3908 |
/// "prog", "--config", "file.conf", "in.txt" |
| 3909 |
/// ]); |
| 3910 |
/// |
| 3911 |
/// assert!(res.is_err()); |
| 3912 |
/// // We didn't use output |
| 3913 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); |
| 3914 |
/// ``` |
| 3915 |
/// |
| 3916 |
/// [`Arg::requires(name)`]: Arg::requires() |
| 3917 |
/// [Conflicting]: Arg::conflicts_with() |
| 3918 |
/// [override]: Arg::overrides_with() |
| 3919 |
#[must_use] |
| 3920 |
pub fn requires_ifs( |
| 3921 |
mut self, |
| 3922 |
ifs: impl IntoIterator<Item = (impl Into<ArgPredicate>, impl Into<Id>)>, |
| 3923 |
) -> Self { |
| 3924 |
self.requires |
| 3925 |
.extend(ifs.into_iter().map(|(val, arg)| (val.into(), arg.into()))); |
| 3926 |
self |
| 3927 |
} |
| 3928 |
|
| 3929 |
#[doc(hidden)] |
| 3930 |
#[cfg_attr( |
| 3931 |
feature = "deprecated", |
| 3932 |
deprecated(since = "4.0.0", note = "Replaced with `Arg::requires_ifs`") |
| 3933 |
)] |
| 3934 |
pub fn requires_all(self, ids: impl IntoIterator<Item = impl Into<Id>>) -> Self { |
| 3935 |
self.requires_ifs(ids.into_iter().map(|id| (ArgPredicate::IsPresent, id))) |
| 3936 |
} |
| 3937 |
|
| 3938 |
/// This argument is mutually exclusive with the specified argument. |
| 3939 |
/// |
| 3940 |
/// <div class="warning"> |
| 3941 |
/// |
| 3942 |
/// **NOTE:** Conflicting rules take precedence over being required by default. Conflict rules |
| 3943 |
/// only need to be set for one of the two arguments, they do not need to be set for each. |
| 3944 |
/// |
| 3945 |
/// </div> |
| 3946 |
/// |
| 3947 |
/// <div class="warning"> |
| 3948 |
/// |
| 3949 |
/// **NOTE:** Defining a conflict is two-way, but does *not* need to defined for both arguments |
| 3950 |
/// (i.e. if A conflicts with B, defining `A.conflicts_with(B)` is sufficient. You do not |
| 3951 |
/// need to also do `B.conflicts_with(A)`) |
| 3952 |
/// |
| 3953 |
/// </div> |
| 3954 |
/// |
| 3955 |
/// <div class="warning"> |
| 3956 |
/// |
| 3957 |
/// **NOTE:** [`Arg::conflicts_with_all(names)`] allows specifying an argument which conflicts with more than one argument. |
| 3958 |
/// |
| 3959 |
/// </div> |
| 3960 |
/// |
| 3961 |
/// <div class="warning"> |
| 3962 |
/// |
| 3963 |
/// **NOTE** [`Arg::exclusive(true)`] allows specifying an argument which conflicts with every other argument. |
| 3964 |
/// |
| 3965 |
/// </div> |
| 3966 |
/// |
| 3967 |
/// <div class="warning"> |
| 3968 |
/// |
| 3969 |
/// **NOTE:** All arguments implicitly conflict with themselves. |
| 3970 |
/// |
| 3971 |
/// </div> |
| 3972 |
/// |
| 3973 |
/// # Examples |
| 3974 |
/// |
| 3975 |
/// ```rust |
| 3976 |
/// # use clap_builder as clap; |
| 3977 |
/// # use clap::Arg; |
| 3978 |
/// Arg::new("config") |
| 3979 |
/// .conflicts_with("debug") |
| 3980 |
/// # ; |
| 3981 |
/// ``` |
| 3982 |
/// |
| 3983 |
/// Setting conflicting argument, and having both arguments present at runtime is an error. |
| 3984 |
/// |
| 3985 |
/// ```rust |
| 3986 |
/// # use clap_builder as clap; |
| 3987 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 3988 |
/// let res = Command::new("prog") |
| 3989 |
/// .arg(Arg::new("cfg") |
| 3990 |
/// .action(ArgAction::Set) |
| 3991 |
/// .conflicts_with("debug") |
| 3992 |
/// .long("config")) |
| 3993 |
/// .arg(Arg::new("debug") |
| 3994 |
/// .long("debug") |
| 3995 |
/// .action(ArgAction::SetTrue)) |
| 3996 |
/// .try_get_matches_from(vec![ |
| 3997 |
/// "prog", "--debug", "--config", "file.conf" |
| 3998 |
/// ]); |
| 3999 |
/// |
| 4000 |
/// assert!(res.is_err()); |
| 4001 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::ArgumentConflict); |
| 4002 |
/// ``` |
| 4003 |
/// |
| 4004 |
/// [`Arg::conflicts_with_all(names)`]: Arg::conflicts_with_all() |
| 4005 |
/// [`Arg::exclusive(true)`]: Arg::exclusive() |
| 4006 |
#[must_use] |
| 4007 |
pub fn conflicts_with(mut self, arg_id: impl IntoResettable<Id>) -> Self { |
| 4008 |
if let Some(arg_id) = arg_id.into_resettable().into_option() { |
| 4009 |
self.conflicts.push(arg_id); |
| 4010 |
} else { |
| 4011 |
self.conflicts.clear(); |
| 4012 |
} |
| 4013 |
self |
| 4014 |
} |
| 4015 |
|
| 4016 |
/// This argument is mutually exclusive with the specified arguments. |
| 4017 |
/// |
| 4018 |
/// See [`Arg::conflicts_with`]. |
| 4019 |
/// |
| 4020 |
/// <div class="warning"> |
| 4021 |
/// |
| 4022 |
/// **NOTE:** Conflicting rules take precedence over being required by default. Conflict rules |
| 4023 |
/// only need to be set for one of the two arguments, they do not need to be set for each. |
| 4024 |
/// |
| 4025 |
/// </div> |
| 4026 |
/// |
| 4027 |
/// <div class="warning"> |
| 4028 |
/// |
| 4029 |
/// **NOTE:** Defining a conflict is two-way, but does *not* need to defined for both arguments |
| 4030 |
/// (i.e. if A conflicts with B, defining `A.conflicts_with(B)` is sufficient. You do not need |
| 4031 |
/// need to also do `B.conflicts_with(A)`) |
| 4032 |
/// |
| 4033 |
/// </div> |
| 4034 |
/// |
| 4035 |
/// <div class="warning"> |
| 4036 |
/// |
| 4037 |
/// **NOTE:** [`Arg::exclusive(true)`] allows specifying an argument which conflicts with every other argument. |
| 4038 |
/// |
| 4039 |
/// </div> |
| 4040 |
/// |
| 4041 |
/// # Examples |
| 4042 |
/// |
| 4043 |
/// ```rust |
| 4044 |
/// # use clap_builder as clap; |
| 4045 |
/// # use clap::Arg; |
| 4046 |
/// Arg::new("config") |
| 4047 |
/// .conflicts_with_all(["debug", "input"]) |
| 4048 |
/// # ; |
| 4049 |
/// ``` |
| 4050 |
/// |
| 4051 |
/// Setting conflicting argument, and having any of the arguments present at runtime with a |
| 4052 |
/// conflicting argument is an error. |
| 4053 |
/// |
| 4054 |
/// ```rust |
| 4055 |
/// # use clap_builder as clap; |
| 4056 |
/// # use clap::{Command, Arg, error::ErrorKind, ArgAction}; |
| 4057 |
/// let res = Command::new("prog") |
| 4058 |
/// .arg(Arg::new("cfg") |
| 4059 |
/// .action(ArgAction::Set) |
| 4060 |
/// .conflicts_with_all(["debug", "input"]) |
| 4061 |
/// .long("config")) |
| 4062 |
/// .arg(Arg::new("debug") |
| 4063 |
/// .long("debug")) |
| 4064 |
/// .arg(Arg::new("input")) |
| 4065 |
/// .try_get_matches_from(vec![ |
| 4066 |
/// "prog", "--config", "file.conf", "file.txt" |
| 4067 |
/// ]); |
| 4068 |
/// |
| 4069 |
/// assert!(res.is_err()); |
| 4070 |
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::ArgumentConflict); |
| 4071 |
/// ``` |
| 4072 |
/// [`Arg::conflicts_with`]: Arg::conflicts_with() |
| 4073 |
/// [`Arg::exclusive(true)`]: Arg::exclusive() |
| 4074 |
#[must_use] |
| 4075 |
pub fn conflicts_with_all(mut self, names: impl IntoIterator<Item = impl Into<Id>>) -> Self { |
| 4076 |
self.conflicts.extend(names.into_iter().map(Into::into)); |
| 4077 |
self |
| 4078 |
} |
| 4079 |
|
| 4080 |
/// Sets an overridable argument. |
| 4081 |
/// |
| 4082 |
/// i.e. this argument and the following argument |
| 4083 |
/// will override each other in POSIX style (whichever argument was specified at runtime |
| 4084 |
/// **last** "wins") |
| 4085 |
/// |
| 4086 |
/// <div class="warning"> |
| 4087 |
/// |
| 4088 |
/// **NOTE:** When an argument is overridden it is essentially as if it never was used, any |
| 4089 |
/// conflicts, requirements, etc. are evaluated **after** all "overrides" have been removed |
| 4090 |
/// |
| 4091 |
/// </div> |
| 4092 |
/// |
| 4093 |
/// <div class="warning"> |
| 4094 |
/// |
| 4095 |
/// **NOTE:** Overriding an argument implies they [conflict][Arg::conflicts_with`]. |
| 4096 |
/// |
| 4097 |
/// </div> |
| 4098 |
/// |
| 4099 |
/// # Examples |
| 4100 |
/// |
| 4101 |
/// ```rust |
| 4102 |
/// # use clap_builder as clap; |
| 4103 |
/// # use clap::{Command, arg}; |
| 4104 |
/// let m = Command::new("prog") |
| 4105 |
/// .arg(arg!(-f --flag "some flag") |
| 4106 |
/// .conflicts_with("debug")) |
| 4107 |
/// .arg(arg!(-d --debug "other flag")) |
| 4108 |
/// .arg(arg!(-c --color "third flag") |
| 4109 |
/// .overrides_with("flag")) |
| 4110 |
/// .get_matches_from(vec![ |
| 4111 |
/// "prog", "-f", "-d", "-c"]); |
| 4112 |
/// // ^~~~~~~~~~~~^~~~~ flag is overridden by color |
| 4113 |
/// |
| 4114 |
/// assert!(m.get_flag("color")); |
| 4115 |
/// assert!(m.get_flag("debug")); // even though flag conflicts with debug, it's as if flag |
| 4116 |
/// // was never used because it was overridden with color |
| 4117 |
/// assert!(!m.get_flag("flag")); |
| 4118 |
/// ``` |
| 4119 |
#[must_use] |
| 4120 |
pub fn overrides_with(mut self, arg_id: impl IntoResettable<Id>) -> Self { |
| 4121 |
if let Some(arg_id) = arg_id.into_resettable().into_option() { |
| 4122 |
self.overrides.push(arg_id); |
| 4123 |
} else { |
| 4124 |
self.overrides.clear(); |
| 4125 |
} |
| 4126 |
self |
| 4127 |
} |
| 4128 |
|
| 4129 |
/// Sets multiple mutually overridable arguments by name. |
| 4130 |
/// |
| 4131 |
/// i.e. this argument and the following argument will override each other in POSIX style |
| 4132 |
/// (whichever argument was specified at runtime **last** "wins") |
| 4133 |
/// |
| 4134 |
/// <div class="warning"> |
| 4135 |
/// |
| 4136 |
/// **NOTE:** When an argument is overridden it is essentially as if it never was used, any |
| 4137 |
/// conflicts, requirements, etc. are evaluated **after** all "overrides" have been removed |
| 4138 |
/// |
| 4139 |
/// </div> |
| 4140 |
/// |
| 4141 |
/// <div class="warning"> |
| 4142 |
/// |
| 4143 |
/// **NOTE:** Overriding an argument implies they [conflict][Arg::conflicts_with_all`]. |
| 4144 |
/// |
| 4145 |
/// </div> |
| 4146 |
/// |
| 4147 |
/// # Examples |
| 4148 |
/// |
| 4149 |
/// ```rust |
| 4150 |
/// # use clap_builder as clap; |
| 4151 |
/// # use clap::{Command, arg}; |
| 4152 |
/// let m = Command::new("prog") |
| 4153 |
/// .arg(arg!(-f --flag "some flag") |
| 4154 |
/// .conflicts_with("color")) |
| 4155 |
/// .arg(arg!(-d --debug "other flag")) |
| 4156 |
/// .arg(arg!(-c --color "third flag") |
| 4157 |
/// .overrides_with_all(["flag", "debug"])) |
| 4158 |
/// .get_matches_from(vec![ |
| 4159 |
/// "prog", "-f", "-d", "-c"]); |
| 4160 |
/// // ^~~~~~^~~~~~~~~ flag and debug are overridden by color |
| 4161 |
/// |
| 4162 |
/// assert!(m.get_flag("color")); // even though flag conflicts with color, it's as if flag |
| 4163 |
/// // and debug were never used because they were overridden |
| 4164 |
/// // with color |
| 4165 |
/// assert!(!m.get_flag("debug")); |
| 4166 |
/// assert!(!m.get_flag("flag")); |
| 4167 |
/// ``` |
| 4168 |
#[must_use] |
| 4169 |
pub fn overrides_with_all(mut self, names: impl IntoIterator<Item = impl Into<Id>>) -> Self { |
| 4170 |
self.overrides.extend(names.into_iter().map(Into::into)); |
| 4171 |
self |
| 4172 |
} |
| 4173 |
} |
| 4174 |
|
| 4175 |
/// # Reflection |
| 4176 |
impl Arg { |
| 4177 |
/// Get the name of the argument |
| 4178 |
#[inline] |
| 4179 |
pub fn get_id(&self) -> &Id { |
| 4180 |
&self.id |
| 4181 |
} |
| 4182 |
|
| 4183 |
/// Get the help specified for this argument, if any |
| 4184 |
#[inline] |
| 4185 |
pub fn get_help(&self) -> Option<&StyledStr> { |
| 4186 |
self.help.as_ref() |
| 4187 |
} |
| 4188 |
|
| 4189 |
/// Get the long help specified for this argument, if any |
| 4190 |
/// |
| 4191 |
/// # Examples |
| 4192 |
/// |
| 4193 |
/// ```rust |
| 4194 |
/// # use clap_builder as clap; |
| 4195 |
/// # use clap::Arg; |
| 4196 |
/// let arg = Arg::new("foo").long_help("long help"); |
| 4197 |
/// assert_eq!(Some("long help".to_owned()), arg.get_long_help().map(|s| s.to_string())); |
| 4198 |
/// ``` |
| 4199 |
/// |
| 4200 |
#[inline] |
| 4201 |
pub fn get_long_help(&self) -> Option<&StyledStr> { |
| 4202 |
self.long_help.as_ref() |
| 4203 |
} |
| 4204 |
|
| 4205 |
/// Get the placement within help |
| 4206 |
#[inline] |
| 4207 |
pub fn get_display_order(&self) -> usize { |
| 4208 |
self.disp_ord.unwrap_or(999) |
| 4209 |
} |
| 4210 |
|
| 4211 |
/// Get the help heading specified for this argument, if any |
| 4212 |
#[inline] |
| 4213 |
pub fn get_help_heading(&self) -> Option<&str> { |
| 4214 |
self.help_heading |
| 4215 |
.as_ref() |
| 4216 |
.map(|s| s.as_deref()) |
| 4217 |
.unwrap_or_default() |
| 4218 |
} |
| 4219 |
|
| 4220 |
/// Get the short option name for this argument, if any |
| 4221 |
#[inline] |
| 4222 |
pub fn get_short(&self) -> Option<char> { |
| 4223 |
self.short |
| 4224 |
} |
| 4225 |
|
| 4226 |
/// Get visible short aliases for this argument, if any |
| 4227 |
#[inline] |
| 4228 |
pub fn get_visible_short_aliases(&self) -> Option<Vec<char>> { |
| 4229 |
if self.short_aliases.is_empty() { |
| 4230 |
None |
| 4231 |
} else { |
| 4232 |
Some( |
| 4233 |
self.short_aliases |
| 4234 |
.iter() |
| 4235 |
.filter_map(|(c, v)| if *v { Some(c) } else { None }) |
| 4236 |
.copied() |
| 4237 |
.collect(), |
| 4238 |
) |
| 4239 |
} |
| 4240 |
} |
| 4241 |
|
| 4242 |
/// Get *all* short aliases for this argument, if any, both visible and hidden. |
| 4243 |
#[inline] |
| 4244 |
pub fn get_all_short_aliases(&self) -> Option<Vec<char>> { |
| 4245 |
if self.short_aliases.is_empty() { |
| 4246 |
None |
| 4247 |
} else { |
| 4248 |
Some(self.short_aliases.iter().map(|(s, _)| s).copied().collect()) |
| 4249 |
} |
| 4250 |
} |
| 4251 |
|
| 4252 |
/// Get the short option name and its visible aliases, if any |
| 4253 |
#[inline] |
| 4254 |
pub fn get_short_and_visible_aliases(&self) -> Option<Vec<char>> { |
| 4255 |
let mut shorts = vec![self.short?]; |
| 4256 |
if let Some(aliases) = self.get_visible_short_aliases() { |
| 4257 |
shorts.extend(aliases); |
| 4258 |
} |
| 4259 |
Some(shorts) |
| 4260 |
} |
| 4261 |
|
| 4262 |
/// Get the long option name for this argument, if any |
| 4263 |
#[inline] |
| 4264 |
pub fn get_long(&self) -> Option<&str> { |
| 4265 |
self.long.as_deref() |
| 4266 |
} |
| 4267 |
|
| 4268 |
/// Get visible aliases for this argument, if any |
| 4269 |
#[inline] |
| 4270 |
pub fn get_visible_aliases(&self) -> Option<Vec<&str>> { |
| 4271 |
if self.aliases.is_empty() { |
| 4272 |
None |
| 4273 |
} else { |
| 4274 |
Some( |
| 4275 |
self.aliases |
| 4276 |
.iter() |
| 4277 |
.filter_map(|(s, v)| if *v { Some(s.as_str()) } else { None }) |
| 4278 |
.collect(), |
| 4279 |
) |
| 4280 |
} |
| 4281 |
} |
| 4282 |
|
| 4283 |
/// Get *all* aliases for this argument, if any, both visible and hidden. |
| 4284 |
#[inline] |
| 4285 |
pub fn get_all_aliases(&self) -> Option<Vec<&str>> { |
| 4286 |
if self.aliases.is_empty() { |
| 4287 |
None |
| 4288 |
} else { |
| 4289 |
Some(self.aliases.iter().map(|(s, _)| s.as_str()).collect()) |
| 4290 |
} |
| 4291 |
} |
| 4292 |
|
| 4293 |
/// Get the long option name and its visible aliases, if any |
| 4294 |
#[inline] |
| 4295 |
pub fn get_long_and_visible_aliases(&self) -> Option<Vec<&str>> { |
| 4296 |
let mut longs = vec![self.get_long()?]; |
| 4297 |
if let Some(aliases) = self.get_visible_aliases() { |
| 4298 |
longs.extend(aliases); |
| 4299 |
} |
| 4300 |
Some(longs) |
| 4301 |
} |
| 4302 |
|
| 4303 |
/// Get hidden aliases for this argument, if any |
| 4304 |
#[inline] |
| 4305 |
pub fn get_aliases(&self) -> Option<Vec<&str>> { |
| 4306 |
if self.aliases.is_empty() { |
| 4307 |
None |
| 4308 |
} else { |
| 4309 |
Some( |
| 4310 |
self.aliases |
| 4311 |
.iter() |
| 4312 |
.filter_map(|(s, v)| if !*v { Some(s.as_str()) } else { None }) |
| 4313 |
.collect(), |
| 4314 |
) |
| 4315 |
} |
| 4316 |
} |
| 4317 |
|
| 4318 |
/// Get the names of possible values for this argument. Only useful for user |
| 4319 |
/// facing applications, such as building help messages or man files |
| 4320 |
pub fn get_possible_values(&self) -> Vec<PossibleValue> { |
| 4321 |
if !self.is_takes_value_set() { |
| 4322 |
vec![] |
| 4323 |
} else { |
| 4324 |
self.get_value_parser() |
| 4325 |
.possible_values() |
| 4326 |
.map(|pvs| pvs.collect()) |
| 4327 |
.unwrap_or_default() |
| 4328 |
} |
| 4329 |
} |
| 4330 |
|
| 4331 |
/// Get the names of values for this argument. |
| 4332 |
#[inline] |
| 4333 |
pub fn get_value_names(&self) -> Option<&[Str]> { |
| 4334 |
if self.val_names.is_empty() { |
| 4335 |
None |
| 4336 |
} else { |
| 4337 |
Some(&self.val_names) |
| 4338 |
} |
| 4339 |
} |
| 4340 |
|
| 4341 |
/// Get the number of values for this argument. |
| 4342 |
#[inline] |
| 4343 |
pub fn get_num_args(&self) -> Option<ValueRange> { |
| 4344 |
self.num_vals |
| 4345 |
} |
| 4346 |
|
| 4347 |
#[inline] |
| 4348 |
pub(crate) fn get_min_vals(&self) -> usize { |
| 4349 |
self.get_num_args().expect(INTERNAL_ERROR_MSG).min_values() |
| 4350 |
} |
| 4351 |
|
| 4352 |
/// Get the delimiter between multiple values |
| 4353 |
#[inline] |
| 4354 |
pub fn get_value_delimiter(&self) -> Option<char> { |
| 4355 |
self.val_delim |
| 4356 |
} |
| 4357 |
|
| 4358 |
/// Get the value terminator for this argument. The `value_terminator` is a value |
| 4359 |
/// that terminates parsing of multi-valued arguments. |
| 4360 |
#[inline] |
| 4361 |
pub fn get_value_terminator(&self) -> Option<&Str> { |
| 4362 |
self.terminator.as_ref() |
| 4363 |
} |
| 4364 |
|
| 4365 |
/// Get the index of this argument, if any |
| 4366 |
#[inline] |
| 4367 |
pub fn get_index(&self) -> Option<usize> { |
| 4368 |
self.index |
| 4369 |
} |
| 4370 |
|
| 4371 |
/// Get the value hint of this argument |
| 4372 |
pub fn get_value_hint(&self) -> ValueHint { |
| 4373 |
// HACK: we should use `Self::add` and `Self::remove` to type-check that `ArgExt` is used |
| 4374 |
self.ext.get::<ValueHint>().copied().unwrap_or_else(|| { |
| 4375 |
if self.is_takes_value_set() { |
| 4376 |
let type_id = self.get_value_parser().type_id(); |
| 4377 |
if type_id == AnyValueId::of::<std::path::PathBuf>() { |
| 4378 |
ValueHint::AnyPath |
| 4379 |
} else { |
| 4380 |
ValueHint::default() |
| 4381 |
} |
| 4382 |
} else { |
| 4383 |
ValueHint::default() |
| 4384 |
} |
| 4385 |
}) |
| 4386 |
} |
| 4387 |
|
| 4388 |
/// Get the environment variable name specified for this argument, if any |
| 4389 |
/// |
| 4390 |
/// # Examples |
| 4391 |
/// |
| 4392 |
/// ```rust |
| 4393 |
/// # use clap_builder as clap; |
| 4394 |
/// # use std::ffi::OsStr; |
| 4395 |
/// # use clap::Arg; |
| 4396 |
/// let arg = Arg::new("foo").env("ENVIRONMENT"); |
| 4397 |
/// assert_eq!(arg.get_env(), Some(OsStr::new("ENVIRONMENT"))); |
| 4398 |
/// ``` |
| 4399 |
#[cfg(feature = "env")] |
| 4400 |
pub fn get_env(&self) -> Option<&std::ffi::OsStr> { |
| 4401 |
self.env.as_ref().map(|x| x.0.as_os_str()) |
| 4402 |
} |
| 4403 |
|
| 4404 |
/// Get the default values specified for this argument, if any |
| 4405 |
/// |
| 4406 |
/// # Examples |
| 4407 |
/// |
| 4408 |
/// ```rust |
| 4409 |
/// # use clap_builder as clap; |
| 4410 |
/// # use clap::Arg; |
| 4411 |
/// let arg = Arg::new("foo").default_value("default value"); |
| 4412 |
/// assert_eq!(arg.get_default_values(), &["default value"]); |
| 4413 |
/// ``` |
| 4414 |
pub fn get_default_values(&self) -> &[OsStr] { |
| 4415 |
&self.default_vals |
| 4416 |
} |
| 4417 |
|
| 4418 |
/// Checks whether this argument is a positional or not. |
| 4419 |
/// |
| 4420 |
/// # Examples |
| 4421 |
/// |
| 4422 |
/// ```rust |
| 4423 |
/// # use clap_builder as clap; |
| 4424 |
/// # use clap::Arg; |
| 4425 |
/// let arg = Arg::new("foo"); |
| 4426 |
/// assert_eq!(arg.is_positional(), true); |
| 4427 |
/// |
| 4428 |
/// let arg = Arg::new("foo").long("foo"); |
| 4429 |
/// assert_eq!(arg.is_positional(), false); |
| 4430 |
/// ``` |
| 4431 |
pub fn is_positional(&self) -> bool { |
| 4432 |
self.get_long().is_none() && self.get_short().is_none() |
| 4433 |
} |
| 4434 |
|
| 4435 |
/// Reports whether [`Arg::required`] is set |
| 4436 |
pub fn is_required_set(&self) -> bool { |
| 4437 |
self.is_set(ArgSettings::Required) |
| 4438 |
} |
| 4439 |
|
| 4440 |
pub(crate) fn is_multiple_values_set(&self) -> bool { |
| 4441 |
self.get_num_args().unwrap_or_default().is_multiple() |
| 4442 |
} |
| 4443 |
|
| 4444 |
pub(crate) fn is_takes_value_set(&self) -> bool { |
| 4445 |
self.get_num_args() |
| 4446 |
.unwrap_or_else(|| 1.into()) |
| 4447 |
.takes_values() |
| 4448 |
} |
| 4449 |
|
| 4450 |
/// Report whether [`Arg::allow_hyphen_values`] is set |
| 4451 |
pub fn is_allow_hyphen_values_set(&self) -> bool { |
| 4452 |
self.is_set(ArgSettings::AllowHyphenValues) |
| 4453 |
} |
| 4454 |
|
| 4455 |
/// Report whether [`Arg::allow_negative_numbers`] is set |
| 4456 |
pub fn is_allow_negative_numbers_set(&self) -> bool { |
| 4457 |
self.is_set(ArgSettings::AllowNegativeNumbers) |
| 4458 |
} |
| 4459 |
|
| 4460 |
/// Behavior when parsing the argument |
| 4461 |
pub fn get_action(&self) -> &ArgAction { |
| 4462 |
const DEFAULT: ArgAction = ArgAction::Set; |
| 4463 |
self.action.as_ref().unwrap_or(&DEFAULT) |
| 4464 |
} |
| 4465 |
|
| 4466 |
/// Configured parser for argument values |
| 4467 |
/// |
| 4468 |
/// # Example |
| 4469 |
/// |
| 4470 |
/// ```rust |
| 4471 |
/// # use clap_builder as clap; |
| 4472 |
/// let cmd = clap::Command::new("raw") |
| 4473 |
/// .arg( |
| 4474 |
/// clap::Arg::new("port") |
| 4475 |
/// .value_parser(clap::value_parser!(usize)) |
| 4476 |
/// ); |
| 4477 |
/// let value_parser = cmd.get_arguments() |
| 4478 |
/// .find(|a| a.get_id() == "port").unwrap() |
| 4479 |
/// .get_value_parser(); |
| 4480 |
/// println!("{value_parser:?}"); |
| 4481 |
/// ``` |
| 4482 |
pub fn get_value_parser(&self) -> &super::ValueParser { |
| 4483 |
if let Some(value_parser) = self.value_parser.as_ref() { |
| 4484 |
value_parser |
| 4485 |
} else { |
| 4486 |
static DEFAULT: super::ValueParser = super::ValueParser::string(); |
| 4487 |
&DEFAULT |
| 4488 |
} |
| 4489 |
} |
| 4490 |
|
| 4491 |
/// Report whether [`Arg::global`] is set |
| 4492 |
pub fn is_global_set(&self) -> bool { |
| 4493 |
self.is_set(ArgSettings::Global) |
| 4494 |
} |
| 4495 |
|
| 4496 |
/// Report whether [`Arg::next_line_help`] is set |
| 4497 |
pub fn is_next_line_help_set(&self) -> bool { |
| 4498 |
self.is_set(ArgSettings::NextLineHelp) |
| 4499 |
} |
| 4500 |
|
| 4501 |
/// Report whether [`Arg::hide`] is set |
| 4502 |
pub fn is_hide_set(&self) -> bool { |
| 4503 |
self.is_set(ArgSettings::Hidden) |
| 4504 |
} |
| 4505 |
|
| 4506 |
/// Report whether [`Arg::hide_default_value`] is set |
| 4507 |
pub fn is_hide_default_value_set(&self) -> bool { |
| 4508 |
self.is_set(ArgSettings::HideDefaultValue) |
| 4509 |
} |
| 4510 |
|
| 4511 |
/// Report whether [`Arg::hide_possible_values`] is set |
| 4512 |
pub fn is_hide_possible_values_set(&self) -> bool { |
| 4513 |
self.is_set(ArgSettings::HidePossibleValues) |
| 4514 |
} |
| 4515 |
|
| 4516 |
/// Report whether [`Arg::hide_env`] is set |
| 4517 |
#[cfg(feature = "env")] |
| 4518 |
pub fn is_hide_env_set(&self) -> bool { |
| 4519 |
self.is_set(ArgSettings::HideEnv) |
| 4520 |
} |
| 4521 |
|
| 4522 |
/// Report whether [`Arg::hide_env_values`] is set |
| 4523 |
#[cfg(feature = "env")] |
| 4524 |
pub fn is_hide_env_values_set(&self) -> bool { |
| 4525 |
self.is_set(ArgSettings::HideEnvValues) |
| 4526 |
} |
| 4527 |
|
| 4528 |
/// Report whether [`Arg::hide_short_help`] is set |
| 4529 |
pub fn is_hide_short_help_set(&self) -> bool { |
| 4530 |
self.is_set(ArgSettings::HiddenShortHelp) |
| 4531 |
} |
| 4532 |
|
| 4533 |
/// Report whether [`Arg::hide_long_help`] is set |
| 4534 |
pub fn is_hide_long_help_set(&self) -> bool { |
| 4535 |
self.is_set(ArgSettings::HiddenLongHelp) |
| 4536 |
} |
| 4537 |
|
| 4538 |
/// Report whether [`Arg::require_equals`] is set |
| 4539 |
pub fn is_require_equals_set(&self) -> bool { |
| 4540 |
self.is_set(ArgSettings::RequireEquals) |
| 4541 |
} |
| 4542 |
|
| 4543 |
/// Reports whether [`Arg::exclusive`] is set |
| 4544 |
pub fn is_exclusive_set(&self) -> bool { |
| 4545 |
self.is_set(ArgSettings::Exclusive) |
| 4546 |
} |
| 4547 |
|
| 4548 |
/// Report whether [`Arg::trailing_var_arg`] is set |
| 4549 |
pub fn is_trailing_var_arg_set(&self) -> bool { |
| 4550 |
self.is_set(ArgSettings::TrailingVarArg) |
| 4551 |
} |
| 4552 |
|
| 4553 |
/// Reports whether [`Arg::last`] is set |
| 4554 |
pub fn is_last_set(&self) -> bool { |
| 4555 |
self.is_set(ArgSettings::Last) |
| 4556 |
} |
| 4557 |
|
| 4558 |
/// Reports whether [`Arg::ignore_case`] is set |
| 4559 |
pub fn is_ignore_case_set(&self) -> bool { |
| 4560 |
self.is_set(ArgSettings::IgnoreCase) |
| 4561 |
} |
| 4562 |
|
| 4563 |
/// Access an [`ArgExt`] |
| 4564 |
#[cfg(feature = "unstable-ext")] |
| 4565 |
pub fn get<T: ArgExt + Extension>(&self) -> Option<&T> { |
| 4566 |
self.ext.get::<T>() |
| 4567 |
} |
| 4568 |
|
| 4569 |
/// Remove an [`ArgExt`] |
| 4570 |
#[cfg(feature = "unstable-ext")] |
| 4571 |
pub fn remove<T: ArgExt + Extension>(mut self) -> Option<T> { |
| 4572 |
self.ext.remove::<T>() |
| 4573 |
} |
| 4574 |
} |
| 4575 |
|
| 4576 |
/// # Internally used only |
| 4577 |
impl Arg { |
| 4578 |
pub(crate) fn _build(&mut self) { |
| 4579 |
if self.action.is_none() { |
| 4580 |
if self.num_vals == Some(ValueRange::EMPTY) { |
| 4581 |
let action = ArgAction::SetTrue; |
| 4582 |
self.action = Some(action); |
| 4583 |
} else { |
| 4584 |
let action = |
| 4585 |
if self.is_positional() && self.num_vals.unwrap_or_default().is_unbounded() { |
| 4586 |
// Allow collecting arguments interleaved with flags |
| 4587 |
// |
| 4588 |
// Bounded values are probably a group and the user should explicitly opt-in to |
| 4589 |
// Append |
| 4590 |
ArgAction::Append |
| 4591 |
} else { |
| 4592 |
ArgAction::Set |
| 4593 |
}; |
| 4594 |
self.action = Some(action); |
| 4595 |
} |
| 4596 |
} |
| 4597 |
if let Some(action) = self.action.as_ref() { |
| 4598 |
if let Some(default_value) = action.default_value() { |
| 4599 |
if self.default_vals.is_empty() { |
| 4600 |
self.default_vals = vec![default_value.into()]; |
| 4601 |
} |
| 4602 |
} |
| 4603 |
if let Some(default_value) = action.default_missing_value() { |
| 4604 |
if self.default_missing_vals.is_empty() { |
| 4605 |
self.default_missing_vals = vec![default_value.into()]; |
| 4606 |
} |
| 4607 |
} |
| 4608 |
} |
| 4609 |
|
| 4610 |
if self.value_parser.is_none() { |
| 4611 |
if let Some(default) = self.action.as_ref().and_then(|a| a.default_value_parser()) { |
| 4612 |
self.value_parser = Some(default); |
| 4613 |
} else { |
| 4614 |
self.value_parser = Some(super::ValueParser::string()); |
| 4615 |
} |
| 4616 |
} |
| 4617 |
|
| 4618 |
let val_names_len = self.val_names.len(); |
| 4619 |
if val_names_len > 1 { |
| 4620 |
self.num_vals.get_or_insert(val_names_len.into()); |
| 4621 |
} else { |
| 4622 |
let nargs = self.get_action().default_num_args(); |
| 4623 |
self.num_vals.get_or_insert(nargs); |
| 4624 |
} |
| 4625 |
} |
| 4626 |
|
| 4627 |
// Used for positionals when printing |
| 4628 |
pub(crate) fn name_no_brackets(&self) -> String { |
| 4629 |
debug!("Arg::name_no_brackets:{}", self.get_id()); |
| 4630 |
let delim = " "; |
| 4631 |
if !self.val_names.is_empty() { |
| 4632 |
debug!("Arg::name_no_brackets: val_names={:#?}", self.val_names); |
| 4633 |
|
| 4634 |
if self.val_names.len() > 1 { |
| 4635 |
self.val_names |
| 4636 |
.iter() |
| 4637 |
.map(|n| format!("<{n}>")) |
| 4638 |
.collect::<Vec<_>>() |
| 4639 |
.join(delim) |
| 4640 |
} else { |
| 4641 |
self.val_names |
| 4642 |
.first() |
| 4643 |
.expect(INTERNAL_ERROR_MSG) |
| 4644 |
.as_str() |
| 4645 |
.to_owned() |
| 4646 |
} |
| 4647 |
} else { |
| 4648 |
debug!("Arg::name_no_brackets: just name"); |
| 4649 |
self.get_id().as_str().to_owned() |
| 4650 |
} |
| 4651 |
} |
| 4652 |
|
| 4653 |
pub(crate) fn stylized(&self, styles: &Styles, required: Option<bool>) -> StyledStr { |
| 4654 |
use std::fmt::Write as _; |
| 4655 |
let literal = styles.get_literal(); |
| 4656 |
|
| 4657 |
let mut styled = StyledStr::new(); |
| 4658 |
// Write the name such --long or -l |
| 4659 |
if let Some(l) = self.get_long() { |
| 4660 |
let _ = write!(styled, "{literal}--{l}{literal:#}",); |
| 4661 |
} else if let Some(s) = self.get_short() { |
| 4662 |
let _ = write!(styled, "{literal}-{s}{literal:#}"); |
| 4663 |
} |
| 4664 |
styled.push_styled(&self.stylize_arg_suffix(styles, required)); |
| 4665 |
styled |
| 4666 |
} |
| 4667 |
|
| 4668 |
pub(crate) fn stylize_arg_suffix(&self, styles: &Styles, required: Option<bool>) -> StyledStr { |
| 4669 |
use std::fmt::Write as _; |
| 4670 |
let literal = styles.get_literal(); |
| 4671 |
let placeholder = styles.get_placeholder(); |
| 4672 |
let mut styled = StyledStr::new(); |
| 4673 |
|
| 4674 |
let mut need_closing_bracket = false; |
| 4675 |
if self.is_takes_value_set() && !self.is_positional() { |
| 4676 |
let is_optional_val = self.get_min_vals() == 0; |
| 4677 |
let (style, start) = if self.is_require_equals_set() { |
| 4678 |
if is_optional_val { |
| 4679 |
need_closing_bracket = true; |
| 4680 |
(placeholder, "[=") |
| 4681 |
} else { |
| 4682 |
(literal, "=") |
| 4683 |
} |
| 4684 |
} else if is_optional_val { |
| 4685 |
need_closing_bracket = true; |
| 4686 |
(placeholder, " [") |
| 4687 |
} else { |
| 4688 |
(placeholder, " ") |
| 4689 |
}; |
| 4690 |
let _ = write!(styled, "{style}{start}{style:#}"); |
| 4691 |
} |
| 4692 |
if self.is_takes_value_set() || self.is_positional() { |
| 4693 |
let required = required.unwrap_or_else(|| self.is_required_set()); |
| 4694 |
let arg_val = self.render_arg_val(required); |
| 4695 |
let _ = write!(styled, "{placeholder}{arg_val}{placeholder:#}",); |
| 4696 |
} else if matches!(*self.get_action(), ArgAction::Count) { |
| 4697 |
let _ = write!(styled, "{placeholder}...{placeholder:#}",); |
| 4698 |
} |
| 4699 |
if need_closing_bracket { |
| 4700 |
let _ = write!(styled, "{placeholder}]{placeholder:#}",); |
| 4701 |
} |
| 4702 |
|
| 4703 |
styled |
| 4704 |
} |
| 4705 |
|
| 4706 |
/// Write the values such as `<name1> <name2>` |
| 4707 |
fn render_arg_val(&self, required: bool) -> String { |
| 4708 |
let mut rendered = String::new(); |
| 4709 |
|
| 4710 |
let num_vals = self.get_num_args().unwrap_or_else(|| 1.into()); |
| 4711 |
|
| 4712 |
let mut val_names = if self.val_names.is_empty() { |
| 4713 |
vec![self.id.as_internal_str().to_owned()] |
| 4714 |
} else { |
| 4715 |
self.val_names.clone() |
| 4716 |
}; |
| 4717 |
if val_names.len() == 1 { |
| 4718 |
let min = num_vals.min_values().max(1); |
| 4719 |
let val_name = val_names.pop().unwrap(); |
| 4720 |
val_names = vec![val_name; min]; |
| 4721 |
} |
| 4722 |
|
| 4723 |
debug_assert!(self.is_takes_value_set()); |
| 4724 |
let min_vals = num_vals.min_values(); |
| 4725 |
for (n, val_name) in val_names.iter().enumerate() { |
| 4726 |
let is_optional_val = min_vals == 0; |
| 4727 |
let is_past_min = min_vals <= n; |
| 4728 |
let is_optional = if self.is_positional() { |
| 4729 |
!required || is_past_min |
| 4730 |
} else { |
| 4731 |
// The caller already brackets an optional value; avoid `[[name]]` |
| 4732 |
!is_optional_val && is_past_min |
| 4733 |
}; |
| 4734 |
let arg_name = if is_optional { |
| 4735 |
format!("[{val_name}]") |
| 4736 |
} else { |
| 4737 |
format!("<{val_name}>") |
| 4738 |
}; |
| 4739 |
|
| 4740 |
if n != 0 { |
| 4741 |
rendered.push(' '); |
| 4742 |
} |
| 4743 |
rendered.push_str(&arg_name); |
| 4744 |
} |
| 4745 |
|
| 4746 |
let mut extra_values = false; |
| 4747 |
extra_values |= val_names.len() < num_vals.max_values(); |
| 4748 |
if self.is_positional() && matches!(*self.get_action(), ArgAction::Append) { |
| 4749 |
extra_values = true; |
| 4750 |
} |
| 4751 |
if extra_values { |
| 4752 |
rendered.push_str("..."); |
| 4753 |
} |
| 4754 |
|
| 4755 |
rendered |
| 4756 |
} |
| 4757 |
|
| 4758 |
/// Either multiple values or occurrences |
| 4759 |
pub(crate) fn is_multiple(&self) -> bool { |
| 4760 |
self.is_multiple_values_set() || matches!(*self.get_action(), ArgAction::Append) |
| 4761 |
} |
| 4762 |
} |
| 4763 |
|
| 4764 |
impl From<&'_ Self> for Arg { |
| 4765 |
fn from(a: &Self) -> Self { |
| 4766 |
a.clone() |
| 4767 |
} |
| 4768 |
} |
| 4769 |
|
| 4770 |
impl PartialEq for Arg { |
| 4771 |
fn eq(&self, other: &Self) -> bool { |
| 4772 |
self.get_id() == other.get_id() |
| 4773 |
} |
| 4774 |
} |
| 4775 |
|
| 4776 |
impl PartialOrd for Arg { |
| 4777 |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 4778 |
Some(self.cmp(other)) |
| 4779 |
} |
| 4780 |
} |
| 4781 |
|
| 4782 |
impl Ord for Arg { |
| 4783 |
fn cmp(&self, other: &Self) -> Ordering { |
| 4784 |
self.get_id().cmp(other.get_id()) |
| 4785 |
} |
| 4786 |
} |
| 4787 |
|
| 4788 |
impl Eq for Arg {} |
| 4789 |
|
| 4790 |
impl Display for Arg { |
| 4791 |
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 4792 |
let plain = Styles::plain(); |
| 4793 |
self.stylized(&plain, None).fmt(f) |
| 4794 |
} |
| 4795 |
} |
| 4796 |
|
| 4797 |
impl fmt::Debug for Arg { |
| 4798 |
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { |
| 4799 |
let mut ds = f.debug_struct("Arg"); |
| 4800 |
|
| 4801 |
#[allow(unused_mut)] |
| 4802 |
let mut ds = ds |
| 4803 |
.field("id", &self.id) |
| 4804 |
.field("help", &self.help) |
| 4805 |
.field("long_help", &self.long_help) |
| 4806 |
.field("action", &self.action) |
| 4807 |
.field("value_parser", &self.value_parser) |
| 4808 |
.field("conflicts", &self.conflicts) |
| 4809 |
.field("settings", &self.settings) |
| 4810 |
.field("overrides", &self.overrides) |
| 4811 |
.field("groups", &self.groups) |
| 4812 |
.field("requires", &self.requires) |
| 4813 |
.field("r_ifs", &self.r_ifs) |
| 4814 |
.field("r_unless", &self.r_unless) |
| 4815 |
.field("short", &self.short) |
| 4816 |
.field("long", &self.long) |
| 4817 |
.field("aliases", &self.aliases) |
| 4818 |
.field("short_aliases", &self.short_aliases) |
| 4819 |
.field("disp_ord", &self.disp_ord) |
| 4820 |
.field("val_names", &self.val_names) |
| 4821 |
.field("num_vals", &self.num_vals) |
| 4822 |
.field("val_delim", &self.val_delim) |
| 4823 |
.field("default_vals", &self.default_vals) |
| 4824 |
.field("default_vals_ifs", &self.default_vals_ifs) |
| 4825 |
.field("terminator", &self.terminator) |
| 4826 |
.field("index", &self.index) |
| 4827 |
.field("help_heading", &self.help_heading) |
| 4828 |
.field("default_missing_vals", &self.default_missing_vals) |
| 4829 |
.field("ext", &self.ext); |
| 4830 |
|
| 4831 |
#[cfg(feature = "env")] |
| 4832 |
{ |
| 4833 |
ds = ds.field("env", &self.env); |
| 4834 |
} |
| 4835 |
|
| 4836 |
ds.finish() |
| 4837 |
} |
| 4838 |
} |
| 4839 |
|
| 4840 |
/// User-provided data that can be attached to an [`Arg`] |
| 4841 |
#[cfg(feature = "unstable-ext")] |
| 4842 |
pub trait ArgExt: Extension {} |
| 4843 |
|
| 4844 |
// Flags |
| 4845 |
#[cfg(test)] |
| 4846 |
mod test { |
| 4847 |
use super::Arg; |
| 4848 |
use super::ArgAction; |
| 4849 |
|
| 4850 |
#[test] |
| 4851 |
fn flag_display_long() { |
| 4852 |
let mut f = Arg::new("flg").long("flag").action(ArgAction::SetTrue); |
| 4853 |
f._build(); |
| 4854 |
|
| 4855 |
assert_eq!(f.to_string(), "--flag"); |
| 4856 |
} |
| 4857 |
|
| 4858 |
#[test] |
| 4859 |
fn flag_display_short() { |
| 4860 |
let mut f2 = Arg::new("flg").short('f').action(ArgAction::SetTrue); |
| 4861 |
f2._build(); |
| 4862 |
|
| 4863 |
assert_eq!(f2.to_string(), "-f"); |
| 4864 |
} |
| 4865 |
|
| 4866 |
#[test] |
| 4867 |
fn flag_display_count() { |
| 4868 |
let mut f2 = Arg::new("flg").long("flag").action(ArgAction::Count); |
| 4869 |
f2._build(); |
| 4870 |
|
| 4871 |
assert_eq!(f2.to_string(), "--flag..."); |
| 4872 |
} |
| 4873 |
|
| 4874 |
#[test] |
| 4875 |
fn flag_display_single_alias() { |
| 4876 |
let mut f = Arg::new("flg") |
| 4877 |
.long("flag") |
| 4878 |
.visible_alias("als") |
| 4879 |
.action(ArgAction::SetTrue); |
| 4880 |
f._build(); |
| 4881 |
|
| 4882 |
assert_eq!(f.to_string(), "--flag"); |
| 4883 |
} |
| 4884 |
|
| 4885 |
#[test] |
| 4886 |
fn flag_display_multiple_aliases() { |
| 4887 |
let mut f = Arg::new("flg").short('f').action(ArgAction::SetTrue); |
| 4888 |
f.aliases = vec![ |
| 4889 |
("alias_not_visible".into(), false), |
| 4890 |
("f2".into(), true), |
| 4891 |
("f3".into(), true), |
| 4892 |
("f4".into(), true), |
| 4893 |
]; |
| 4894 |
f._build(); |
| 4895 |
|
| 4896 |
assert_eq!(f.to_string(), "-f"); |
| 4897 |
} |
| 4898 |
|
| 4899 |
#[test] |
| 4900 |
fn flag_display_single_short_alias() { |
| 4901 |
let mut f = Arg::new("flg").short('a').action(ArgAction::SetTrue); |
| 4902 |
f.short_aliases = vec![('b', true)]; |
| 4903 |
f._build(); |
| 4904 |
|
| 4905 |
assert_eq!(f.to_string(), "-a"); |
| 4906 |
} |
| 4907 |
|
| 4908 |
#[test] |
| 4909 |
fn flag_display_multiple_short_aliases() { |
| 4910 |
let mut f = Arg::new("flg").short('a').action(ArgAction::SetTrue); |
| 4911 |
f.short_aliases = vec![('b', false), ('c', true), ('d', true), ('e', true)]; |
| 4912 |
f._build(); |
| 4913 |
|
| 4914 |
assert_eq!(f.to_string(), "-a"); |
| 4915 |
} |
| 4916 |
|
| 4917 |
// Options |
| 4918 |
|
| 4919 |
#[test] |
| 4920 |
fn option_display_multiple_occurrences() { |
| 4921 |
let mut o = Arg::new("opt").long("option").action(ArgAction::Append); |
| 4922 |
o._build(); |
| 4923 |
|
| 4924 |
assert_eq!(o.to_string(), "--option <opt>"); |
| 4925 |
} |
| 4926 |
|
| 4927 |
#[test] |
| 4928 |
fn option_display_multiple_values() { |
| 4929 |
let mut o = Arg::new("opt") |
| 4930 |
.long("option") |
| 4931 |
.action(ArgAction::Set) |
| 4932 |
.num_args(1..); |
| 4933 |
o._build(); |
| 4934 |
|
| 4935 |
assert_eq!(o.to_string(), "--option <opt>..."); |
| 4936 |
} |
| 4937 |
|
| 4938 |
#[test] |
| 4939 |
fn option_display_zero_or_more_values() { |
| 4940 |
let mut o = Arg::new("opt") |
| 4941 |
.long("option") |
| 4942 |
.action(ArgAction::Set) |
| 4943 |
.num_args(0..); |
| 4944 |
o._build(); |
| 4945 |
|
| 4946 |
assert_eq!(o.to_string(), "--option [<opt>...]"); |
| 4947 |
} |
| 4948 |
|
| 4949 |
#[test] |
| 4950 |
fn option_display_one_or_more_values() { |
| 4951 |
let mut o = Arg::new("opt") |
| 4952 |
.long("option") |
| 4953 |
.action(ArgAction::Set) |
| 4954 |
.num_args(1..); |
| 4955 |
o._build(); |
| 4956 |
|
| 4957 |
assert_eq!(o.to_string(), "--option <opt>..."); |
| 4958 |
} |
| 4959 |
|
| 4960 |
#[test] |
| 4961 |
fn option_display_zero_or_more_values_with_value_name() { |
| 4962 |
let mut o = Arg::new("opt") |
| 4963 |
.short('o') |
| 4964 |
.action(ArgAction::Set) |
| 4965 |
.num_args(0..) |
| 4966 |
.value_names(["file"]); |
| 4967 |
o._build(); |
| 4968 |
|
| 4969 |
assert_eq!(o.to_string(), "-o [<file>...]"); |
| 4970 |
} |
| 4971 |
|
| 4972 |
#[test] |
| 4973 |
fn option_display_one_or_more_values_with_value_name() { |
| 4974 |
let mut o = Arg::new("opt") |
| 4975 |
.short('o') |
| 4976 |
.action(ArgAction::Set) |
| 4977 |
.num_args(1..) |
| 4978 |
.value_names(["file"]); |
| 4979 |
o._build(); |
| 4980 |
|
| 4981 |
assert_eq!(o.to_string(), "-o <file>..."); |
| 4982 |
} |
| 4983 |
|
| 4984 |
#[test] |
| 4985 |
fn option_display_optional_value() { |
| 4986 |
let mut o = Arg::new("opt") |
| 4987 |
.long("option") |
| 4988 |
.action(ArgAction::Set) |
| 4989 |
.num_args(0..=1); |
| 4990 |
o._build(); |
| 4991 |
|
| 4992 |
assert_eq!(o.to_string(), "--option [<opt>]"); |
| 4993 |
} |
| 4994 |
|
| 4995 |
#[test] |
| 4996 |
fn option_display_value_names() { |
| 4997 |
let mut o = Arg::new("opt") |
| 4998 |
.short('o') |
| 4999 |
.action(ArgAction::Set) |
| 5000 |
.value_names(["file", "name"]); |
| 5001 |
o._build(); |
| 5002 |
|
| 5003 |
assert_eq!(o.to_string(), "-o <file> <name>"); |
| 5004 |
} |
| 5005 |
|
| 5006 |
#[test] |
| 5007 |
fn option_display3() { |
| 5008 |
let mut o = Arg::new("opt") |
| 5009 |
.short('o') |
| 5010 |
.num_args(1..) |
| 5011 |
.action(ArgAction::Set) |
| 5012 |
.value_names(["file", "name"]); |
| 5013 |
o._build(); |
| 5014 |
|
| 5015 |
assert_eq!(o.to_string(), "-o <file> [name]..."); |
| 5016 |
} |
| 5017 |
|
| 5018 |
#[test] |
| 5019 |
fn option_display_partially_optional_values() { |
| 5020 |
let mut o = Arg::new("opt") |
| 5021 |
.long("example") |
| 5022 |
.action(ArgAction::Set) |
| 5023 |
.num_args(1..=2) |
| 5024 |
.value_names(["FOO", "BAR"]); |
| 5025 |
o._build(); |
| 5026 |
|
| 5027 |
assert_eq!(o.to_string(), "--example <FOO> [BAR]"); |
| 5028 |
} |
| 5029 |
|
| 5030 |
#[test] |
| 5031 |
fn option_display_partially_optional_values_require_equals() { |
| 5032 |
let mut o = Arg::new("opt") |
| 5033 |
.long("example") |
| 5034 |
.action(ArgAction::Set) |
| 5035 |
.num_args(1..=2) |
| 5036 |
.require_equals(true) |
| 5037 |
.value_delimiter(',') |
| 5038 |
.value_names(["FOO", "BAR"]); |
| 5039 |
o._build(); |
| 5040 |
|
| 5041 |
assert_eq!(o.to_string(), "--example=<FOO> [BAR]"); |
| 5042 |
} |
| 5043 |
|
| 5044 |
#[test] |
| 5045 |
fn option_display_partially_optional_values_with_extra_values() { |
| 5046 |
let mut o = Arg::new("opt") |
| 5047 |
.long("example") |
| 5048 |
.action(ArgAction::Set) |
| 5049 |
.num_args(1..=3) |
| 5050 |
.value_names(["A", "B"]); |
| 5051 |
o._build(); |
| 5052 |
|
| 5053 |
assert_eq!(o.to_string(), "--example <A> [B]..."); |
| 5054 |
} |
| 5055 |
|
| 5056 |
#[test] |
| 5057 |
fn option_display_single_alias() { |
| 5058 |
let mut o = Arg::new("opt") |
| 5059 |
.long("option") |
| 5060 |
.action(ArgAction::Set) |
| 5061 |
.visible_alias("als"); |
| 5062 |
o._build(); |
| 5063 |
|
| 5064 |
assert_eq!(o.to_string(), "--option <opt>"); |
| 5065 |
} |
| 5066 |
|
| 5067 |
#[test] |
| 5068 |
fn option_display_multiple_aliases() { |
| 5069 |
let mut o = Arg::new("opt") |
| 5070 |
.long("option") |
| 5071 |
.action(ArgAction::Set) |
| 5072 |
.visible_aliases(["als2", "als3", "als4"]) |
| 5073 |
.alias("als_not_visible"); |
| 5074 |
o._build(); |
| 5075 |
|
| 5076 |
assert_eq!(o.to_string(), "--option <opt>"); |
| 5077 |
} |
| 5078 |
|
| 5079 |
#[test] |
| 5080 |
fn option_display_single_short_alias() { |
| 5081 |
let mut o = Arg::new("opt") |
| 5082 |
.short('a') |
| 5083 |
.action(ArgAction::Set) |
| 5084 |
.visible_short_alias('b'); |
| 5085 |
o._build(); |
| 5086 |
|
| 5087 |
assert_eq!(o.to_string(), "-a <opt>"); |
| 5088 |
} |
| 5089 |
|
| 5090 |
#[test] |
| 5091 |
fn option_display_multiple_short_aliases() { |
| 5092 |
let mut o = Arg::new("opt") |
| 5093 |
.short('a') |
| 5094 |
.action(ArgAction::Set) |
| 5095 |
.visible_short_aliases(['b', 'c', 'd']) |
| 5096 |
.short_alias('e'); |
| 5097 |
o._build(); |
| 5098 |
|
| 5099 |
assert_eq!(o.to_string(), "-a <opt>"); |
| 5100 |
} |
| 5101 |
|
| 5102 |
// Positionals |
| 5103 |
|
| 5104 |
#[test] |
| 5105 |
fn positional_display_multiple_values() { |
| 5106 |
let mut p = Arg::new("pos").index(1).num_args(1..); |
| 5107 |
p._build(); |
| 5108 |
|
| 5109 |
assert_eq!(p.to_string(), "[pos]..."); |
| 5110 |
} |
| 5111 |
|
| 5112 |
#[test] |
| 5113 |
fn positional_display_multiple_values_required() { |
| 5114 |
let mut p = Arg::new("pos").index(1).num_args(1..).required(true); |
| 5115 |
p._build(); |
| 5116 |
|
| 5117 |
assert_eq!(p.to_string(), "<pos>..."); |
| 5118 |
} |
| 5119 |
|
| 5120 |
#[test] |
| 5121 |
fn positional_display_zero_or_more_values() { |
| 5122 |
let mut p = Arg::new("pos").index(1).num_args(0..); |
| 5123 |
p._build(); |
| 5124 |
|
| 5125 |
assert_eq!(p.to_string(), "[pos]..."); |
| 5126 |
} |
| 5127 |
|
| 5128 |
#[test] |
| 5129 |
fn positional_display_zero_or_more_values_required() { |
| 5130 |
let mut p = Arg::new("pos").index(1).num_args(0..).required(true); |
| 5131 |
p._build(); |
| 5132 |
|
| 5133 |
assert_eq!(p.to_string(), "[pos]..."); |
| 5134 |
} |
| 5135 |
|
| 5136 |
#[test] |
| 5137 |
fn positional_display_one_or_more_values() { |
| 5138 |
let mut p = Arg::new("pos").index(1).num_args(1..); |
| 5139 |
p._build(); |
| 5140 |
|
| 5141 |
assert_eq!(p.to_string(), "[pos]..."); |
| 5142 |
} |
| 5143 |
|
| 5144 |
#[test] |
| 5145 |
fn positional_display_one_or_more_values_required() { |
| 5146 |
let mut p = Arg::new("pos").index(1).num_args(1..).required(true); |
| 5147 |
p._build(); |
| 5148 |
|
| 5149 |
assert_eq!(p.to_string(), "<pos>..."); |
| 5150 |
} |
| 5151 |
|
| 5152 |
#[test] |
| 5153 |
fn positional_display_optional_value() { |
| 5154 |
let mut p = Arg::new("pos") |
| 5155 |
.index(1) |
| 5156 |
.num_args(0..=1) |
| 5157 |
.action(ArgAction::Set); |
| 5158 |
p._build(); |
| 5159 |
|
| 5160 |
assert_eq!(p.to_string(), "[pos]"); |
| 5161 |
} |
| 5162 |
|
| 5163 |
#[test] |
| 5164 |
fn positional_display_multiple_occurrences() { |
| 5165 |
let mut p = Arg::new("pos").index(1).action(ArgAction::Append); |
| 5166 |
p._build(); |
| 5167 |
|
| 5168 |
assert_eq!(p.to_string(), "[pos]..."); |
| 5169 |
} |
| 5170 |
|
| 5171 |
#[test] |
| 5172 |
fn positional_display_multiple_occurrences_required() { |
| 5173 |
let mut p = Arg::new("pos") |
| 5174 |
.index(1) |
| 5175 |
.action(ArgAction::Append) |
| 5176 |
.required(true); |
| 5177 |
p._build(); |
| 5178 |
|
| 5179 |
assert_eq!(p.to_string(), "<pos>..."); |
| 5180 |
} |
| 5181 |
|
| 5182 |
#[test] |
| 5183 |
fn positional_display_required() { |
| 5184 |
let mut p = Arg::new("pos").index(1).required(true); |
| 5185 |
p._build(); |
| 5186 |
|
| 5187 |
assert_eq!(p.to_string(), "<pos>"); |
| 5188 |
} |
| 5189 |
|
| 5190 |
#[test] |
| 5191 |
fn positional_display_val_names() { |
| 5192 |
let mut p = Arg::new("pos").index(1).value_names(["file1", "file2"]); |
| 5193 |
p._build(); |
| 5194 |
|
| 5195 |
assert_eq!(p.to_string(), "[file1] [file2]"); |
| 5196 |
} |
| 5197 |
|
| 5198 |
#[test] |
| 5199 |
fn positional_display_val_names_required() { |
| 5200 |
let mut p = Arg::new("pos") |
| 5201 |
.index(1) |
| 5202 |
.value_names(["file1", "file2"]) |
| 5203 |
.required(true); |
| 5204 |
p._build(); |
| 5205 |
|
| 5206 |
assert_eq!(p.to_string(), "<file1> <file2>"); |
| 5207 |
} |
| 5208 |
} |