fix(parser): Deprecate occurrences_of

This mostly exist for
- Knowing of the value came from the command-line but we now have
  `ArgMatches::source`
- Counting the number of flags but we now have `ArgAction::Count`
This commit is contained in:
Ed Page 2022-06-06 12:35:00 -05:00
parent f0cc8b8d25
commit 86a162d1bb
22 changed files with 654 additions and 238 deletions

View file

@ -22,7 +22,7 @@ pub mod bash {
#[derive(Clone, Debug)]
pub struct CompleteArgs {
/// Path to write completion-registration to
#[clap(long, required = true, parse(from_os_str))]
#[clap(long, required = true, value_parser)]
register: Option<std::path::PathBuf>,
#[clap(
@ -30,34 +30,36 @@ pub mod bash {
required = true,
value_name = "COMP_CWORD",
hide_short_help = true,
group = "complete"
group = "complete",
value_parser
)]
index: Option<usize>,
#[clap(long, hide_short_help = true, group = "complete")]
#[clap(long, hide_short_help = true, group = "complete", value_parser)]
ifs: Option<String>,
#[clap(
long = "type",
required = true,
arg_enum,
hide_short_help = true,
group = "complete"
group = "complete",
value_parser
)]
comp_type: Option<CompType>,
#[clap(long, hide_short_help = true, group = "complete")]
#[clap(long, hide_short_help = true, group = "complete", action)]
space: bool,
#[clap(
long,
conflicts_with = "space",
hide_short_help = true,
group = "complete"
group = "complete",
action
)]
no_space: bool,
#[clap(raw = true, hide_short_help = true, group = "complete")]
#[clap(raw = true, hide_short_help = true, group = "complete", value_parser)]
comp_words: Vec<OsString>,
}

View file

@ -35,7 +35,7 @@ fn main() {
let matches = cmd.get_matches();
let mut subcommand = matches.subcommand();
if let Some(("busybox", cmd)) = subcommand {
if cmd.occurrences_of("install") > 0 {
if cmd.is_present("install") {
unimplemented!("Make hardlinks to the executable here");
}
subcommand = cmd.subcommand();

View file

@ -2,7 +2,7 @@
use std::path::PathBuf;
use clap::{arg, command, value_parser, Command};
use clap::{arg, command, value_parser, ArgAction, Command};
fn main() {
let matches = command!()
@ -15,9 +15,12 @@ fn main() {
.required(false)
.value_parser(value_parser!(PathBuf)),
)
.arg(arg!(
-d --debug ... "Turn debugging information on"
))
.arg(
arg!(
-d --debug "Turn debugging information on"
)
.action(ArgAction::Count),
)
.subcommand(
Command::new("test")
.about("does testing things")
@ -36,7 +39,10 @@ fn main() {
// You can see how many times a particular flag or argument occurred
// Note, only flags can have multiple occurrences
match matches.occurrences_of("debug") {
match matches
.get_one::<u64>("debug")
.expect("Count's are defaulted")
{
0 => println!("Debug mode is off"),
1 => println!("Debug mode is kind of on"),
2 => println!("Debug mode is on"),

View file

@ -1,9 +1,16 @@
// Note: this requires the `cargo` feature
use clap::{arg, command};
use clap::{arg, command, ArgAction};
fn main() {
let matches = command!().arg(arg!(-v --verbose ...)).get_matches();
let matches = command!()
.arg(arg!(-v - -verbose).action(ArgAction::Count))
.get_matches();
println!("verbose: {:?}", matches.occurrences_of("verbose"));
println!(
"verbose: {:?}",
matches
.get_one::<u64>("verbose")
.expect("Count always defaulted")
);
}

View file

@ -171,6 +171,7 @@ impl ArgMatcher {
let ma = self.entry(id).or_insert(MatchedArg::new_arg(arg));
debug_assert_eq!(ma.type_id(), Some(arg.get_value_parser().type_id()));
ma.set_source(ValueSource::CommandLine);
#[allow(deprecated)]
ma.inc_occurrences();
ma.new_val_group();
}
@ -180,6 +181,7 @@ impl ArgMatcher {
let ma = self.entry(id).or_insert(MatchedArg::new_group());
debug_assert_eq!(ma.type_id(), None);
ma.set_source(ValueSource::CommandLine);
#[allow(deprecated)]
ma.inc_occurrences();
ma.new_val_group();
}
@ -197,6 +199,7 @@ impl ArgMatcher {
)
);
ma.set_source(ValueSource::CommandLine);
#[allow(deprecated)]
ma.inc_occurrences();
ma.new_val_group();
}

View file

@ -561,51 +561,14 @@ impl ArgMatches {
value.and_then(MatchedArg::source)
}
/// The number of times an argument was used at runtime.
///
/// If an argument isn't present it will return `0`.
///
/// **NOTE:** This returns the number of times the argument was used, *not* the number of
/// values. For example, `-o val1 val2 val3 -o val4` would return `2` (2 occurrences, but 4
/// values). See [Arg::multiple_occurrences][crate::Arg::multiple_occurrences].
///
/// # Panics
///
/// If `id` is is not a valid argument or group name.
///
/// # Examples
///
/// ```rust
/// # use clap::{Command, Arg};
/// let m = Command::new("myprog")
/// .arg(Arg::new("debug")
/// .short('d')
/// .multiple_occurrences(true))
/// .get_matches_from(vec![
/// "myprog", "-d", "-d", "-d"
/// ]);
///
/// assert_eq!(m.occurrences_of("debug"), 3);
/// ```
///
/// This next example shows that counts actual uses of the argument, not just `-`'s
///
/// ```rust
/// # use clap::{Command, Arg};
/// let m = Command::new("myprog")
/// .arg(Arg::new("debug")
/// .short('d')
/// .multiple_occurrences(true))
/// .arg(Arg::new("flag")
/// .short('f'))
/// .get_matches_from(vec![
/// "myprog", "-ddfd"
/// ]);
///
/// assert_eq!(m.occurrences_of("debug"), 3);
/// assert_eq!(m.occurrences_of("flag"), 1);
/// ```
/// Deprecated, replaced with [`ArgMatches::get_many`]`.len()` or
/// [`ArgAction::Count`][crate::ArgAction].
#[deprecated(
since = "3.2.0",
note = "Replaced with either `ArgMatches::get_many(...).len()` or `ArgAction::Count`"
)]
pub fn occurrences_of<T: Key>(&self, id: T) -> u64 {
#![allow(deprecated)]
self.get_arg(&Id::from(id))
.map_or(0, |a| a.get_occurrences())
}

View file

@ -67,14 +67,17 @@ impl MatchedArg {
}
}
#[deprecated(since = "3.2.0")]
pub(crate) fn inc_occurrences(&mut self) {
self.occurs += 1;
}
#[deprecated(since = "3.2.0")]
pub(crate) fn set_occurrences(&mut self, occurs: u64) {
self.occurs = occurs
}
#[deprecated(since = "3.2.0")]
pub(crate) fn get_occurrences(&self) -> u64 {
self.occurs
}

View file

@ -1197,6 +1197,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
if ident == Some(Identifier::Index) && arg.is_multiple_values_set() {
// HACK: Maintain existing occurrence behavior
let matched = matcher.get_mut(&arg.id).unwrap();
#[allow(deprecated)]
matched.set_occurrences(matched.num_vals() as u64);
}
if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {

View file

@ -330,6 +330,7 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {
}
fn validate_arg_num_occurs(&self, a: &Arg, ma: &MatchedArg) -> ClapResult<()> {
#![allow(deprecated)]
debug!(
"Validator::validate_arg_num_occurs: {:?}={}",
a.name,

View file

@ -11,7 +11,10 @@ fn set() {
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(matches.get_one::<String>("mammal"), None);
assert_eq!(matches.is_present("mammal"), false);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), None);
let matches = cmd
@ -20,7 +23,10 @@ fn set() {
.unwrap();
assert_eq!(matches.get_one::<String>("mammal").unwrap(), "dog");
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(2));
let matches = cmd
@ -29,7 +35,10 @@ fn set() {
.unwrap();
assert_eq!(matches.get_one::<String>("mammal").unwrap(), "cat");
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(4));
}
@ -40,7 +49,10 @@ fn append() {
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(matches.get_one::<String>("mammal"), None);
assert_eq!(matches.is_present("mammal"), false);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), None);
let matches = cmd
@ -49,7 +61,10 @@ fn append() {
.unwrap();
assert_eq!(matches.get_one::<String>("mammal").unwrap(), "dog");
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(
matches.indices_of("mammal").unwrap().collect::<Vec<_>>(),
vec![2]
@ -68,7 +83,10 @@ fn append() {
vec!["dog", "cat"]
);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(
matches.indices_of("mammal").unwrap().collect::<Vec<_>>(),
vec![2, 4]
@ -83,7 +101,10 @@ fn set_true() {
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd
@ -92,7 +113,10 @@ fn set_true() {
.unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd
@ -101,7 +125,10 @@ fn set_true() {
.unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(2));
}
@ -120,13 +147,19 @@ fn set_true_with_explicit_default_value() {
.unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
}
@ -225,7 +258,10 @@ fn set_false() {
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd
@ -234,7 +270,10 @@ fn set_false() {
.unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd
@ -243,7 +282,10 @@ fn set_false() {
.unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(2));
}
@ -262,13 +304,19 @@ fn set_false_with_explicit_default_value() {
.unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
}
@ -333,7 +381,10 @@ fn count() {
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(*matches.get_one::<u64>("mammal").unwrap(), 0);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd
@ -342,7 +393,10 @@ fn count() {
.unwrap();
assert_eq!(*matches.get_one::<u64>("mammal").unwrap(), 1);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd
@ -351,7 +405,10 @@ fn count() {
.unwrap();
assert_eq!(*matches.get_one::<u64>("mammal").unwrap(), 2);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(2));
}
@ -370,13 +427,19 @@ fn count_with_explicit_default_value() {
.unwrap();
assert_eq!(*matches.get_one::<u64>("mammal").unwrap(), 1);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(*matches.get_one::<u64>("mammal").unwrap(), 10);
assert_eq!(matches.is_present("mammal"), true);
#[allow(deprecated)]
{
assert_eq!(matches.occurrences_of("mammal"), 0);
}
assert_eq!(matches.index_of("mammal"), Some(1));
}

View file

@ -19,7 +19,10 @@ fn opt_missing() {
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
"auto"
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("color"), 0);
}
assert_eq!(
m.value_source("color").unwrap(),
clap::ValueSource::DefaultValue
@ -46,7 +49,10 @@ fn opt_present_with_missing_value() {
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
"always"
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("color"), 1);
}
assert_eq!(
m.value_source("color").unwrap(),
clap::ValueSource::CommandLine
@ -73,7 +79,10 @@ fn opt_present_with_value() {
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
"never"
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("color"), 1);
}
assert_eq!(
m.value_source("color").unwrap(),
clap::ValueSource::CommandLine
@ -99,7 +108,10 @@ fn opt_present_with_empty_value() {
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
""
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("color"), 1);
}
assert_eq!(
m.value_source("color").unwrap(),
clap::ValueSource::CommandLine
@ -164,7 +176,10 @@ fn default_missing_value_flag_value() {
m.get_one::<String>("flag").map(|v| v.as_str()),
Some("false")
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 0);
}
assert_eq!(
m.value_source("flag").unwrap(),
clap::ValueSource::DefaultValue
@ -179,7 +194,10 @@ fn default_missing_value_flag_value() {
m.get_one::<String>("flag").map(|v| v.as_str()),
Some("true")
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 1);
}
assert_eq!(
m.value_source("flag").unwrap(),
clap::ValueSource::CommandLine
@ -194,7 +212,10 @@ fn default_missing_value_flag_value() {
m.get_one::<String>("flag").map(|v| v.as_str()),
Some("true")
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 1);
}
assert_eq!(
m.value_source("flag").unwrap(),
clap::ValueSource::CommandLine
@ -206,7 +227,10 @@ fn default_missing_value_flag_value() {
m.get_one::<String>("flag").map(|v| v.as_str()),
Some("false")
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 1);
}
assert_eq!(
m.value_source("flag").unwrap(),
clap::ValueSource::CommandLine
@ -233,7 +257,10 @@ fn delimited_missing_value() {
.collect::<Vec<_>>(),
vec!["one", "two"]
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 0);
}
let m = cmd.try_get_matches_from(["test", "--flag"]).unwrap();
assert_eq!(
@ -243,7 +270,10 @@ fn delimited_missing_value() {
.collect::<Vec<_>>(),
vec!["three", "four"]
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 1);
}
}
#[cfg(debug_assertions)]
@ -295,7 +325,10 @@ fn valid_index() {
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
"always"
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("color"), 1);
}
assert_eq!(
m.value_source("color").unwrap(),
clap::ValueSource::CommandLine

View file

@ -10,7 +10,10 @@ fn opt_default_no_delim() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
"val1,val2,val3"
@ -27,7 +30,10 @@ fn opt_eq_no_delim() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
"val1,val2,val3"
@ -44,7 +50,10 @@ fn opt_s_eq_no_delim() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
"val1,val2,val3"
@ -61,7 +70,10 @@ fn opt_s_default_no_delim() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
"val1,val2,val3"
@ -78,7 +90,10 @@ fn opt_s_no_space_no_delim() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
"val1,val2,val3"
@ -100,7 +115,10 @@ fn opt_s_no_space_mult_no_delim() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
"val1,val2,val3"
@ -123,7 +141,10 @@ fn opt_eq_mult_def_delim() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()

View file

@ -16,7 +16,10 @@ fn env() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
"env"
@ -37,7 +40,10 @@ fn env_bool_literal() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("present"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("present"), 0);
}
assert_eq!(m.get_one::<String>("present").map(|v| v.as_str()), None);
assert!(!m.is_present("negated"));
assert!(!m.is_present("absent"));
@ -58,7 +64,10 @@ fn env_os() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
"env"
@ -82,7 +91,10 @@ fn no_env() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(!m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(m.get_one::<String>("arg").map(|v| v.as_str()), None);
}
@ -99,7 +111,10 @@ fn no_env_no_takes_value() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(!m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(m.get_one::<String>("arg").map(|v| v.as_str()), None);
}
@ -119,7 +134,10 @@ fn with_default() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
"env"
@ -141,7 +159,10 @@ fn opt_user_override() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 1);
}
assert_eq!(
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
"opt"
@ -171,7 +192,10 @@ fn positionals() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
"env"
@ -193,7 +217,10 @@ fn positionals_user_override() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 1);
}
assert_eq!(
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
"opt"
@ -225,7 +252,10 @@ fn multiple_one() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_many::<String>("arg")
.unwrap()
@ -252,7 +282,10 @@ fn multiple_three() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_many::<String>("arg")
.unwrap()
@ -278,7 +311,10 @@ fn multiple_no_delimiter() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_many::<String>("arg")
.unwrap()
@ -304,7 +340,10 @@ fn possible_value() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
"env"
@ -349,7 +388,10 @@ fn value_parser() {
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("arg"), 0);
}
assert_eq!(
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
"env"

View file

@ -56,7 +56,10 @@ fn lots_o_flags_sep() {
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind());
let m = r.unwrap();
assert!(m.is_present("o"));
assert_eq!(m.occurrences_of("o"), 297); // i.e. more than u8
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("o"), 297);
} // i.e. more than u8
}
#[test]
@ -74,7 +77,10 @@ fn lots_o_flags_combined() {
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind());
let m = r.unwrap();
assert!(m.is_present("o"));
assert_eq!(m.occurrences_of("o"), 297); // i.e. more than u8
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("o"), 297);
} // i.e. more than u8
}
#[test]

View file

@ -191,10 +191,16 @@ fn grouped_interleaved_positional_values() {
.unwrap();
let pos: Vec<_> = m.grouped_values_of("pos").unwrap().collect();
assert_eq!(pos, vec![vec!["1", "2", "3", "4"]]);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 4);
}
let flag: Vec<_> = m.grouped_values_of("flag").unwrap().collect();
assert_eq!(flag, vec![vec!["a"], vec!["b"]]);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 2);
}
}
#[test]
@ -214,10 +220,16 @@ fn grouped_interleaved_positional_occurrences() {
.unwrap();
let pos: Vec<_> = m.grouped_values_of("pos").unwrap().collect();
assert_eq!(pos, vec![vec!["1"], vec!["2"], vec!["3"], vec!["4"]]);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 4);
}
let flag: Vec<_> = m.grouped_values_of("flag").unwrap().collect();
assert_eq!(flag, vec![vec!["a"], vec!["b"]]);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 2);
}
}
#[test]

View file

@ -8,9 +8,15 @@ fn multiple_occurrences_of_flags_long() {
.try_get_matches_from(vec!["", "--multflag", "--flag", "--multflag"])
.unwrap();
assert!(m.is_present("multflag"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("multflag"), 2);
}
assert!(m.is_present("flag"));
assert_eq!(m.occurrences_of("flag"), 1)
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 1);
}
}
#[test]
@ -21,9 +27,15 @@ fn multiple_occurrences_of_flags_short() {
.try_get_matches_from(vec!["", "-m", "-f", "-m"])
.unwrap();
assert!(m.is_present("multflag"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("multflag"), 2);
}
assert!(m.is_present("flag"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 1);
}
}
#[test]
@ -43,11 +55,20 @@ fn multiple_occurrences_of_flags_mixed() {
])
.unwrap();
assert!(m.is_present("multflag1"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("multflag1"), 3);
}
assert!(m.is_present("multflag2"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("multflag2"), 2);
}
assert!(m.is_present("flag"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 1);
}
}
#[test]
@ -59,7 +80,10 @@ fn multiple_occurrences_of_positional() {
.try_get_matches_from(&["test"])
.expect("zero occurrences work");
assert!(!m.is_present("multi"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("multi"), 0);
}
assert!(m.get_many::<String>("multi").is_none());
let m = cmd
@ -67,7 +91,10 @@ fn multiple_occurrences_of_positional() {
.try_get_matches_from(&["test", "one"])
.expect("single occurrence work");
assert!(m.is_present("multi"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("multi"), 1);
}
assert_eq!(
m.get_many::<String>("multi")
.unwrap()
@ -81,7 +108,10 @@ fn multiple_occurrences_of_positional() {
.try_get_matches_from(&["test", "one", "two", "three", "four"])
.expect("many occurrences work");
assert!(m.is_present("multi"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("multi"), 4);
}
assert_eq!(
m.get_many::<String>("multi")
.unwrap()
@ -102,7 +132,10 @@ fn multiple_occurrences_of_flags_large_quantity() {
.try_get_matches_from(args)
.unwrap();
assert!(m.is_present("multflag"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("multflag"), 1024);
}
}
#[cfg(feature = "env")]
@ -119,18 +152,30 @@ fn multiple_occurrences_of_before_env() {
let m = cmd.clone().try_get_matches_from(vec![""]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 0);
}
let m = cmd.clone().try_get_matches_from(vec!["", "-v"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 1);
}
let m = cmd.clone().try_get_matches_from(vec!["", "-vv"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
}
let m = cmd.clone().try_get_matches_from(vec!["", "-vvv"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
}
}
#[cfg(feature = "env")]
@ -147,18 +192,30 @@ fn multiple_occurrences_of_after_env() {
let m = cmd.clone().try_get_matches_from(vec![""]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 0);
}
let m = cmd.clone().try_get_matches_from(vec!["", "-v"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 1);
}
let m = cmd.clone().try_get_matches_from(vec!["", "-vv"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
}
let m = cmd.clone().try_get_matches_from(vec!["", "-vvv"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
}
}
#[test]
@ -173,7 +230,10 @@ fn max_occurrences_implies_multiple_occurrences() {
let m = cmd.try_get_matches_from(vec!["prog", "-vvv"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
}
// One max should not imply multiple occurrences
let cmd = Command::new("prog").arg(
@ -200,15 +260,24 @@ fn max_occurrences_try_inputs() {
);
let m = cmd.clone().try_get_matches_from(vec!["prog", "-v"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 1);
}
let m = cmd.clone().try_get_matches_from(vec!["prog", "-vv"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
}
let m = cmd.clone().try_get_matches_from(vec!["prog", "-vvv"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
}
let m = cmd.clone().try_get_matches_from(vec!["prog", "-vvvv"]);
assert!(m.is_err());
@ -218,7 +287,10 @@ fn max_occurrences_try_inputs() {
.clone()
.try_get_matches_from(vec!["prog", "-v", "-v", "-v"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
}
let m = cmd
.clone()
@ -233,17 +305,26 @@ fn max_occurrences_positional() {
let cmd = Command::new("prog").arg(Arg::new("verbose").max_occurrences(3));
let m = cmd.clone().try_get_matches_from(vec!["prog", "v"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 1);
}
let m = cmd.clone().try_get_matches_from(vec!["prog", "v", "v"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
}
let m = cmd
.clone()
.try_get_matches_from(vec!["prog", "v", "v", "v"]);
assert!(m.is_ok(), "{}", m.unwrap_err());
#[allow(deprecated)]
{
assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
}
let m = cmd
.clone()

View file

@ -19,7 +19,10 @@ fn option_long() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 3);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -46,7 +49,10 @@ fn option_short() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 3);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -76,7 +82,10 @@ fn option_mixed() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 4);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -102,7 +111,10 @@ fn option_exact_exact() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 3);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -127,7 +139,10 @@ fn option_exact_exact_not_mult() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -155,7 +170,10 @@ fn option_exact_exact_mult() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 2);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -215,7 +233,10 @@ fn option_min_exact() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 3);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -261,7 +282,10 @@ fn option_short_min_more_mult_occurs() {
assert!(m.is_present("option"));
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 4);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -289,7 +313,10 @@ fn option_short_min_more_single_occur() {
assert!(m.is_present("option"));
assert!(m.is_present("arg"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -316,7 +343,10 @@ fn option_max_exact() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 3);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -342,7 +372,10 @@ fn option_max_less() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 2);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -385,7 +418,10 @@ fn positional() {
let m = m.unwrap();
assert!(m.is_present("pos"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 3);
}
assert_eq!(
m.get_many::<String>("pos")
.unwrap()
@ -409,7 +445,10 @@ fn positional_exact_exact() {
let m = m.unwrap();
assert!(m.is_present("pos"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 3);
}
assert_eq!(
m.get_many::<String>("pos")
.unwrap()
@ -457,7 +496,10 @@ fn positional_min_exact() {
let m = m.unwrap();
assert!(m.is_present("pos"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 3);
}
assert_eq!(
m.get_many::<String>("pos")
.unwrap()
@ -487,7 +529,10 @@ fn positional_min_more() {
let m = m.unwrap();
assert!(m.is_present("pos"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 4);
}
assert_eq!(
m.get_many::<String>("pos")
.unwrap()
@ -507,7 +552,10 @@ fn positional_max_exact() {
let m = m.unwrap();
assert!(m.is_present("pos"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 3);
}
assert_eq!(
m.get_many::<String>("pos")
.unwrap()
@ -527,7 +575,10 @@ fn positional_max_less() {
let m = m.unwrap();
assert!(m.is_present("pos"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 2);
}
assert_eq!(
m.get_many::<String>("pos")
.unwrap()
@ -562,7 +613,10 @@ fn sep_long_equals() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -587,7 +641,10 @@ fn sep_long_space() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -612,7 +669,10 @@ fn sep_short_equals() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -637,7 +697,10 @@ fn sep_short_space() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -662,7 +725,10 @@ fn sep_short_no_space() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -686,7 +752,10 @@ fn sep_positional() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -711,7 +780,10 @@ fn different_sep() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -735,7 +807,10 @@ fn different_sep_positional() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -761,7 +836,10 @@ fn no_sep() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
"val1,val2,val3"
@ -783,7 +861,10 @@ fn no_sep_positional() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_one::<String>("option").map(|v| v.as_str()).unwrap(),
"val1,val2,val3"
@ -812,7 +893,10 @@ fn req_delimiter_long() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -851,7 +935,10 @@ fn req_delimiter_long_with_equal() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -890,7 +977,10 @@ fn req_delimiter_short_with_space() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -929,7 +1019,10 @@ fn req_delimiter_short_with_no_space() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -968,7 +1061,10 @@ fn req_delimiter_short_with_equal() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 1);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -1036,7 +1132,10 @@ fn req_delimiter_complex() {
let m = m.unwrap();
assert!(m.is_present("option"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("option"), 10);
}
assert_eq!(
m.get_many::<String>("option")
.unwrap()
@ -1139,9 +1238,15 @@ fn low_index_positional() {
let m = m.unwrap();
assert!(m.is_present("files"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("files"), 3);
}
assert!(m.is_present("target"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("target"), 1);
}
assert_eq!(
m.get_many::<String>("files")
.unwrap()
@ -1176,9 +1281,15 @@ fn low_index_positional_in_subcmd() {
let sm = m.subcommand_matches("test").unwrap();
assert!(sm.is_present("files"));
#[allow(deprecated)]
{
assert_eq!(sm.occurrences_of("files"), 3);
}
assert!(sm.is_present("target"));
#[allow(deprecated)]
{
assert_eq!(sm.occurrences_of("target"), 1);
}
assert_eq!(
sm.get_many::<String>("files")
.unwrap()
@ -1212,9 +1323,15 @@ fn low_index_positional_with_option() {
let m = m.unwrap();
assert!(m.is_present("files"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("files"), 3);
}
assert!(m.is_present("target"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("target"), 1);
}
assert_eq!(
m.get_many::<String>("files")
.unwrap()
@ -1250,9 +1367,15 @@ fn low_index_positional_with_flag() {
let m = m.unwrap();
assert!(m.is_present("files"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("files"), 3);
}
assert!(m.is_present("target"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("target"), 1);
}
assert_eq!(
m.get_many::<String>("files")
.unwrap()
@ -1284,7 +1407,10 @@ fn multiple_value_terminator_option() {
let m = m.unwrap();
assert!(m.is_present("other"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("other"), 1);
}
assert!(m.is_present("files"));
assert_eq!(
m.get_many::<String>("files")
@ -1498,7 +1624,10 @@ fn values_per_occurrence_named() {
.collect::<Vec<_>>(),
["val1", "val2"]
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 1);
}
let m = a.try_get_matches_from_mut(vec![
"myprog", "--pos", "val1", "val2", "--pos", "val3", "val4",
@ -1514,7 +1643,10 @@ fn values_per_occurrence_named() {
.collect::<Vec<_>>(),
["val1", "val2", "val3", "val4"]
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 2);
}
}
#[test]
@ -1537,7 +1669,10 @@ fn values_per_occurrence_positional() {
.collect::<Vec<_>>(),
["val1", "val2"]
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos"), 2);
}
let m = a.try_get_matches_from_mut(vec!["myprog", "val1", "val2", "val3", "val4"]);
let m = match m {
@ -1551,7 +1686,7 @@ fn values_per_occurrence_positional() {
.collect::<Vec<_>>(),
["val1", "val2", "val3", "val4"]
);
//assert_eq!(m.occurrences_of("pos"), 2); // Fails, we don't recognize this as two occurrences
//#[allow(deprecated)]{assert_eq!(m.occurrences_of("pos"), 2);} // Fails, we don't recognize this as two occurrences
}
// Theoretically we could support this but we aren't tracking occurrence boundaries for positionals
@ -1566,7 +1701,10 @@ fn positional_parser_advances() {
assert!(m.is_ok(), "{}", m.unwrap_err());
let m = m.unwrap();
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos1"), 1);
}
assert_eq!(
m.get_many::<String>("pos1")
.unwrap()
@ -1575,7 +1713,10 @@ fn positional_parser_advances() {
["val1", "val2"]
);
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("pos2"), 1);
}
assert_eq!(
m.get_many::<String>("pos2")
.unwrap()

View file

@ -520,7 +520,10 @@ fn issue_1047_min_zero_vals_default_val() {
)
.try_get_matches_from(vec!["foo", "-d"])
.unwrap();
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("del"), 1);
}
assert_eq!(
m.get_one::<String>("del").map(|v| v.as_str()),
Some("default")

View file

@ -12,7 +12,10 @@ fn flag_overrides_itself() {
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.is_present("flag"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 1);
}
}
#[test]
@ -23,7 +26,10 @@ fn mult_flag_overrides_itself() {
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.is_present("flag"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("flag"), 4);
}
}
#[test]
@ -38,7 +44,10 @@ fn option_overrides_itself() {
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.is_present("opt"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("opt"), 1);
}
assert_eq!(
m.get_one::<String>("opt").map(|v| v.as_str()),
Some("other")
@ -61,7 +70,10 @@ fn mult_option_require_delim_overrides_itself() {
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.is_present("opt"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("opt"), 3);
}
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
@ -93,7 +105,10 @@ fn mult_option_overrides_itself() {
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.is_present("opt"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("opt"), 2);
}
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
@ -114,7 +129,10 @@ fn option_use_delim_false_override_itself() {
.try_get_matches_from(vec!["", "--opt=some,other", "--opt=one,two"])
.unwrap();
assert!(m.is_present("opt"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("opt"), 1);
}
assert_eq!(
m.get_many::<String>("opt")
.unwrap()
@ -133,7 +151,10 @@ fn pos_mult_overrides_itself() {
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.is_present("val"));
#[allow(deprecated)]
{
assert_eq!(m.occurrences_of("val"), 3);
}
assert_eq!(
m.get_many::<String>("val")
.unwrap()

View file

@ -55,17 +55,26 @@ fn outer_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -
}
fn top_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
#[allow(deprecated)]
{
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
}
}
fn inner_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
let m = get_inner_matches(m);
#[allow(deprecated)]
{
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
}
}
fn outer_can_access_flag(m: &ArgMatches, present: bool, occurrences: u64) -> bool {
let m = get_outer_matches(m);
#[allow(deprecated)]
{
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
}
}
#[test]

View file

@ -40,7 +40,7 @@ scpositional present with value: value
";
static O2P: &str = "flag NOT present
option present 2 times with value: some
option present with value: some
An option: some
An option: other
positional present with value: value
@ -49,7 +49,7 @@ option2 maybe present with value of: Nothing
positional2 maybe present with value of: Nothing
option3 NOT present
positional3 NOT present
option present 2 times with value: some
option present with value: some
An option: some
An option: other
positional present with value: value
@ -57,7 +57,7 @@ subcmd NOT present
";
static F2OP: &str = "flag present 2 times
option present 1 times with value: some
option present with value: some
An option: some
positional present with value: value
flag2 NOT present
@ -65,14 +65,14 @@ option2 maybe present with value of: Nothing
positional2 maybe present with value of: Nothing
option3 NOT present
positional3 NOT present
option present 1 times with value: some
option present with value: some
An option: some
positional present with value: value
subcmd NOT present
";
static FOP: &str = "flag present 1 times
option present 1 times with value: some
option present with value: some
An option: some
positional present with value: value
flag2 NOT present
@ -80,7 +80,7 @@ option2 maybe present with value of: Nothing
positional2 maybe present with value of: Nothing
option3 NOT present
positional3 NOT present
option present 1 times with value: some
option present with value: some
An option: some
positional present with value: value
subcmd NOT present
@ -91,21 +91,18 @@ pub fn check_complex_output(args: &str, out: &str) {
let matches = utils::complex_app()
.try_get_matches_from(args.split(' ').collect::<Vec<_>>())
.unwrap();
if matches.is_present("flag") {
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
} else {
match matches.get_one::<u64>("flag").unwrap() {
0 => {
writeln!(w, "flag NOT present").unwrap();
}
n => {
writeln!(w, "flag present {} times", n).unwrap();
}
}
if matches.is_present("option") {
if let Some(v) = matches.get_one::<String>("option").map(|v| v.as_str()) {
writeln!(
w,
"option present {} times with value: {}",
matches.occurrences_of("option"),
v
)
.unwrap();
writeln!(w, "option present with value: {}", v).unwrap();
}
if let Some(ov) = matches.get_many::<String>("option") {
for o in ov {
@ -186,13 +183,7 @@ pub fn check_complex_output(args: &str, out: &str) {
if matches.is_present("option") {
if let Some(v) = matches.get_one::<String>("option").map(|v| v.as_str()) {
writeln!(
w,
"option present {} times with value: {}",
matches.occurrences_of("option"),
v
)
.unwrap();
writeln!(w, "option present with value: {}", v).unwrap();
}
if let Some(ov) = matches.get_many::<String>("option") {
for o in ov {
@ -211,11 +202,14 @@ pub fn check_complex_output(args: &str, out: &str) {
if let Some("subcmd") = matches.subcommand_name() {
writeln!(w, "subcmd present").unwrap();
if let Some(matches) = matches.subcommand_matches("subcmd") {
if matches.is_present("flag") {
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
} else {
match matches.get_one::<u64>("flag").unwrap() {
0 => {
writeln!(w, "flag NOT present").unwrap();
}
n => {
writeln!(w, "flag present {} times", n).unwrap();
}
}
if matches.is_present("option") {
if let Some(v) = matches.get_one::<String>("option").map(|v| v.as_str()) {
@ -267,7 +261,7 @@ fn flag_x2_opt() {
check_complex_output(
"clap-test value -f -f -o some",
"flag present 2 times
option present 1 times with value: some
option present with value: some
An option: some
positional present with value: value
flag2 NOT present
@ -275,7 +269,7 @@ option2 maybe present with value of: Nothing
positional2 maybe present with value of: Nothing
option3 NOT present
positional3 NOT present
option present 1 times with value: some
option present with value: some
An option: some
positional present with value: value
subcmd NOT present

View file

@ -5,7 +5,7 @@ use std::str;
use regex::Regex;
use clap::{arg, Arg, ArgGroup, Command};
use clap::{arg, Arg, ArgAction, ArgGroup, Command};
#[track_caller]
pub fn assert_eq<S, S2>(expected: S, actual: S2)
@ -55,7 +55,11 @@ pub fn complex_app() -> Command<'static> {
.multiple_occurrences(true),
)
.arg(arg!([positional] "tests positionals"))
.arg(arg!(-f --flag ... "tests flags").global(true))
.arg(
arg!(-f --flag "tests flags")
.action(ArgAction::Count)
.global(true),
)
.args(&[
arg!(flag2: -F "tests flags with exclusions")
.conflicts_with("flag")