Skip to content

Commit

Permalink
Merge pull request #2538 from clap-rs/multiple
Browse files Browse the repository at this point in the history
Removed Arg::multiple
  • Loading branch information
pksunkara authored Jun 16, 2021
2 parents 43909dd + e5e20b3 commit 412b71e
Show file tree
Hide file tree
Showing 33 changed files with 393 additions and 407 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TODO: `cargo`, `std` features
* **Removed Methods**
* **Arg**
* `Arg::settings` in favor of `Arg::setting(Setting1 | Setting2)`
* `Arg::multiple` in favour of `Arg::multiple_values` and `Arg::multiple_occurrences`
* **Renamed Settings**
* `AppSettings::DisableHelpFlags` => `AppSettings::DisableHelpFlag`
* `AppSettings::DisableVersion` => `AppSettings::DisableVersionFlag`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn main() {
.index(1))
.arg(Arg::new("v")
.short('v')
.multiple(true)
.multiple_occurrences(true)
.takes_value(true)
.about("Sets the level of verbosity"))
.subcommand(App::new("test")
Expand Down
8 changes: 4 additions & 4 deletions clap_derive/src/derives/into_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ pub fn gen_app_augmentation(

Ty::OptionOption => quote_spanned! { ty.span()=>
.takes_value(true)
.multiple(false)
.multiple_values(false)
.min_values(0)
.max_values(1)
#validator
},

Ty::OptionVec => quote_spanned! { ty.span()=>
.takes_value(true)
.multiple(true)
.multiple_values(true)
.min_values(0)
#validator
},
Expand All @@ -315,7 +315,7 @@ pub fn gen_app_augmentation(

quote_spanned! { ty.span()=>
.takes_value(true)
.multiple(true)
.multiple_values(true)
#possible_values
#validator
}
Expand All @@ -327,7 +327,7 @@ pub fn gen_app_augmentation(

Ty::Other if flag => quote_spanned! { ty.span()=>
.takes_value(false)
.multiple(false)
.multiple_values(false)
},

Ty::Other => {
Expand Down
2 changes: 1 addition & 1 deletion clap_derive/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn basic() {
assert_eq!(Opt { arg: vec![] }, Opt::parse_from(&["test"]));
assert_eq!(
Opt { arg: vec![24, 42] },
Opt::parse_from(&["test", "-a24", "--arg", "42"])
Opt::parse_from(&["test", "--arg", "24", "42"])
);
}

Expand Down
4 changes: 2 additions & 2 deletions clap_derive/tests/custom-string-parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct PathOpt {
#[clap(short, default_value = "../", parse(from_os_str))]
default_path: PathBuf,

#[clap(short, parse(from_os_str))]
#[clap(short, parse(from_os_str), multiple_occurrences(true))]
vector_path: Vec<PathBuf>,

#[clap(short, parse(from_os_str))]
Expand Down Expand Up @@ -254,7 +254,7 @@ fn test_custom_bool() {
verbose: bool,
#[clap(short, parse(try_from_str = parse_bool))]
tribool: Option<bool>,
#[clap(short, parse(try_from_str = parse_bool))]
#[clap(short, parse(try_from_str = parse_bool), multiple_occurrences(true))]
bitset: Vec<bool>,
}

Expand Down
2 changes: 1 addition & 1 deletion clap_derive/tests/explicit_name_no_renaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use utils::*;
fn explicit_short_long_no_rename() {
#[derive(Clap, PartialEq, Debug)]
struct Opt {
#[clap(short = '.', long = ".foo")]
#[clap(short = '.', long = ".foo", multiple_occurrences(true))]
foo: Vec<String>,
}

Expand Down
10 changes: 5 additions & 5 deletions clap_derive/tests/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn option_with_raw_default() {
fn options() {
#[derive(Clap, PartialEq, Debug)]
struct Opt {
#[clap(short, long)]
#[clap(short, long, multiple_occurrences(true))]
arg: Vec<i32>,
}
assert_eq!(Opt { arg: vec![24] }, Opt::parse_from(&["test", "-a24"]));
Expand Down Expand Up @@ -120,7 +120,7 @@ fn option_from_str() {
fn optional_argument_for_optional_option() {
#[derive(Clap, PartialEq, Debug)]
struct Opt {
#[clap(short)]
#[clap(short, multiple_occurrences(true))]
#[allow(clippy::option_option)]
arg: Option<Option<i32>>,
}
Expand Down Expand Up @@ -193,7 +193,7 @@ fn two_option_options() {
fn optional_vec() {
#[derive(Clap, PartialEq, Debug)]
struct Opt {
#[clap(short)]
#[clap(short, multiple_occurrences(true))]
arg: Option<Vec<i32>>,
}
assert_eq!(
Expand Down Expand Up @@ -250,10 +250,10 @@ fn optional_vec() {
fn two_optional_vecs() {
#[derive(Clap, PartialEq, Debug)]
struct Opt {
#[clap(short)]
#[clap(short, multiple_occurrences(true))]
arg: Option<Vec<i32>>,

#[clap(short)]
#[clap(short, multiple_occurrences(true))]
b: Option<Vec<i32>>,
}

Expand Down
2 changes: 1 addition & 1 deletion clap_derive/tests/raw_idents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::Clap;
fn raw_idents() {
#[derive(Clap, Debug, PartialEq)]
struct Opt {
#[clap(short, long)]
#[clap(short, long, multiple_occurrences(true))]
r#type: Vec<String>,
}

Expand Down
7 changes: 3 additions & 4 deletions examples/05_flag_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ fn main() {
.about("turns up the awesome") // Displayed when showing help info
.short('a') // Trigger this arg with "-a"
.long("awesome") // Trigger this arg with "--awesome"
.takes_value(true)
.multiple(true) // This flag should allow multiple
.multiple_occurrences(true) // This flag should allow multiple
// occurrences such as "-aaa" or "-a -a"
.requires("config") // Says, "If the user uses -a, they MUST
// also use this other 'config' arg too"
Expand All @@ -39,9 +38,9 @@ fn main() {
println!("Awesomeness is turned on");
}

// If we set the multiple() option of a flag we can check how many times the user specified
// If we set the multiple option of a flag we can check how many times the user specified
//
// Note: if we did not specify the multiple() option, and the user used "awesome" we would get
// Note: if we did not specify the multiple option, and the user used "awesome" we would get
// a 1 (no matter how many times they actually used it), or a 0 if they didn't use it at all
match matches.occurrences_of("awesome") {
0 => println!("Nothing is awesome"),
Expand Down
8 changes: 4 additions & 4 deletions examples/07_option_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
.takes_value(true) // MUST be set to true in order to be an "option" argument
.short('i') // This argument is triggered with "-i"
.long("input") // This argument is triggered with "--input"
.multiple(true) // Set to true if you wish to allow multiple occurrences
.multiple_occurrences(true) // Set to true if you wish to allow multiple occurrences
// such as "-i file -i other_file -i third_file"
.required(true) // By default this argument MUST be present
// NOTE: mutual exclusions take precedence over
Expand All @@ -44,13 +44,13 @@ fn main() {

// We can also get the value for "input"
//
// NOTE: If we specified multiple(), this will only return the _FIRST_
// NOTE: If we specified multiple_occurrences(), this will only return the _FIRST_
// occurrence
if let Some(ref in_file) = matches.value_of("input") {
println!("An input file: {}", in_file);
}

// If we specified the multiple() setting we can get all the values
// If we specified the multiple_occurrences() setting we can get all the values
if let Some(in_v) = matches.values_of("input") {
for in_file in in_v {
println!("An input file: {}", in_file);
Expand All @@ -59,7 +59,7 @@ fn main() {

// We can see how many times the option was used with the occurrences_of() method
//
// NOTE: Just like with flags, if we did not specify the multiple() setting this will only
// NOTE: Just like with flags, if we did not specify the multiple_occurrences() setting this will only
// return 1 no matter how many times the argument was used (unless it wasn't used at all, in
// in which case 0 is returned)
println!(
Expand Down
2 changes: 1 addition & 1 deletion examples/20_subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn main() {
.long("stuff")
.about("Stuff to add")
.takes_value(true)
.multiple(true),
.multiple_values(true),
),
)
.get_matches();
Expand Down
7 changes: 6 additions & 1 deletion examples/22_stop_parsing_with_--.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ fn main() {
let matches = App::new("myprog")
.arg(Arg::new("eff").short('f'))
.arg(Arg::new("pea").short('p').takes_value(true))
.arg(Arg::new("slop").takes_value(true).multiple(true).last(true))
.arg(
Arg::new("slop")
.takes_value(true)
.multiple_values(true)
.last(true),
)
.get_matches();

println!("-f used: {:?}", matches.is_present("eff"));
Expand Down
4 changes: 2 additions & 2 deletions examples/23_flag_subcommands_pacman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ fn main() {
.arg(
Arg::new("package")
.about("packages")
.multiple(true)
.required_unless_present("search")
.takes_value(true),
.takes_value(true)
.multiple_values(true),
),
)
.get_matches();
Expand Down
10 changes: 5 additions & 5 deletions src/build/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub enum AppSettings {
/// .setting(AppSettings::AllowMissingPositional)
/// .arg(Arg::new("foo"))
/// .arg(Arg::new("bar"))
/// .arg(Arg::new("baz").takes_value(true).multiple(true))
/// .arg(Arg::new("baz").takes_value(true).multiple_values(true))
/// .get_matches_from(vec![
/// "prog", "foo", "bar", "baz1", "baz2", "baz3"
/// ]);
Expand All @@ -357,7 +357,7 @@ pub enum AppSettings {
/// .setting(AppSettings::AllowMissingPositional)
/// .arg(Arg::new("foo"))
/// .arg(Arg::new("bar"))
/// .arg(Arg::new("baz").takes_value(true).multiple(true))
/// .arg(Arg::new("baz").takes_value(true).multiple_values(true))
/// .get_matches_from(vec![
/// "prog", "--", "baz1", "baz2", "baz3"
/// ]);
Expand Down Expand Up @@ -476,7 +476,7 @@ pub enum AppSettings {
/// let app = App::new("app").subcommand(App::new("sub")).arg(
/// Arg::new("arg")
/// .long("arg")
/// .multiple(true)
/// .multiple_values(true)
/// .takes_value(true),
/// );
///
Expand Down Expand Up @@ -994,7 +994,7 @@ pub enum AppSettings {
///
/// The values of the trailing positional argument will contain all args from itself on.
///
/// **NOTE:** The final positional argument **must** have [`Arg::multiple(true)`] or the usage
/// **NOTE:** The final positional argument **must** have [`Arg::multiple_values(true)`] or the usage
/// string equivalent.
///
/// # Examples
Expand All @@ -1009,7 +1009,7 @@ pub enum AppSettings {
/// let trail: Vec<&str> = m.values_of("cmd").unwrap().collect();
/// assert_eq!(trail, ["arg1", "-r", "val1"]);
/// ```
/// [`Arg::multiple(true)`]: Arg::multiple()
/// [`Arg::multiple_values(true)`]: Arg::multiple_values()
TrailingVarArg,

/// Groups flags and options together, presenting a more unified help message
Expand Down
11 changes: 10 additions & 1 deletion src/build/arg/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ pub(crate) fn assert_arg(arg: &Arg) {

if arg.index.is_some() {
assert!(
arg.short.is_none() && arg.long.is_none(),
!arg.has_switch(),
"Argument '{}' is a positional argument and can't have short or long name versions",
arg.name
);
}

// Positionals should not have multiple_occurrences
if arg.is_positional() {
assert!(
!arg.is_set(ArgSettings::MultipleOccurrences),
"Argument '{}' is a positional argument and can't be set as multiple occurrences",
arg.name
);
}

if arg.is_set(ArgSettings::Required) {
assert!(
arg.default_vals.is_empty(),
Expand Down
Loading

0 comments on commit 412b71e

Please sign in to comment.