Skip to content

Commit

Permalink
fix(builder)!: Accept a more liberal Iterator
Browse files Browse the repository at this point in the history
This replaces the most common uses of `&[]` with `IntoIterator`.

This is based on clap-rs#2414.  Unlike that, this stays consistent with using
`Into<&'help str>`.

There are some remaining functions not converted over, like defaults and
requires.

BREAKING CHANGE: Instances like `aliases(&[])` need to switch to
`aliases([])`.
  • Loading branch information
epage committed Oct 12, 2021
1 parent ab05044 commit e86194d
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 72 deletions.
4 changes: 2 additions & 2 deletions benches/03_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn build_from_builder(c: &mut Criterion) {
Arg::new("multvals")
.long("multvals")
.about("Tests multiple values, not mult occs")
.value_names(&["one", "two"]),
.value_names(["one", "two"]),
)
.arg(
Arg::new("multvalsmo")
Expand All @@ -120,7 +120,7 @@ pub fn build_from_builder(c: &mut Criterion) {
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences)
.about("Tests multiple values, not mult occs")
.value_names(&["one", "two"]),
.value_names(["one", "two"]),
)
.arg(
Arg::new("minvals")
Expand Down
2 changes: 1 addition & 1 deletion clap_derive/examples/subcommand_aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum Opt {
#[clap(alias = "foobar")]
Foo,
// https://docs.rs/clap/2/clap/struct.App.html#method.aliases
#[clap(aliases = &["baz", "fizz"])]
#[clap(aliases = ["baz", "fizz"])]
Bar,
}

Expand Down
2 changes: 1 addition & 1 deletion clap_derive/tests/non_literal_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Opt {
)]
x: i32,

#[clap(short = 'l', long = "level", aliases = &["set-level", "lvl"])]
#[clap(short = 'l', long = "level", aliases = ["set-level", "lvl"])]
level: String,

#[clap(long("values"))]
Expand Down
2 changes: 1 addition & 1 deletion examples/21_aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() {
let matches = App::new("MyApp")
.subcommand(
App::new("ls")
.aliases(&["list", "dir"])
.aliases(["list", "dir"])
.about("Adds files to myapp")
.license("MIT OR Apache-2.0")
.version("0.1")
Expand Down
64 changes: 44 additions & 20 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ impl<'help> App<'help> {
/// # use clap::{App, Arg};
/// let m = App::new("myprog")
/// .subcommand(App::new("test")
/// .aliases(&["do-stuff", "do-tests", "tests"]))
/// .aliases(["do-stuff", "do-tests", "tests"]))
/// .arg(Arg::new("input")
/// .about("the file to add")
/// .index(1)
Expand All @@ -1241,8 +1241,13 @@ impl<'help> App<'help> {
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
/// [`App::visible_aliases`]: App::visible_aliases()
pub fn aliases(mut self, names: &[&'help str]) -> Self {
self.aliases.extend(names.iter().map(|n| (*n, false)));
pub fn aliases<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<&'help str>,
{
self.aliases
.extend(names.into_iter().map(|n| (n.into(), false)));
self
}

Expand All @@ -1257,18 +1262,21 @@ impl<'help> App<'help> {
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(App::new("test").short_flag('t')
/// .short_flag_aliases(&['a', 'b', 'c']))
/// .short_flag_aliases(['a', 'b', 'c']))
/// .arg(Arg::new("input")
/// .about("the file to add")
/// .index(1)
/// .required(false))
/// .get_matches_from(vec!["myprog", "-a"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
pub fn short_flag_aliases(mut self, names: &[char]) -> Self {
pub fn short_flag_aliases<I>(mut self, names: I) -> Self
where
I: IntoIterator<Item = char>,
{
for s in names {
assert!(s != &'-', "short alias name cannot be `-`");
self.short_flag_aliases.push((*s, false));
assert!(s != '-', "short alias name cannot be `-`");
self.short_flag_aliases.push((s, false));
}
self
}
Expand All @@ -1284,17 +1292,21 @@ impl<'help> App<'help> {
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(App::new("test").long_flag("test")
/// .long_flag_aliases(&["testing", "testall", "test_all"]))
/// .long_flag_aliases(["testing", "testall", "test_all"]))
/// .arg(Arg::new("input")
/// .about("the file to add")
/// .index(1)
/// .required(false))
/// .get_matches_from(vec!["myprog", "--testing"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
pub fn long_flag_aliases(mut self, names: &[&'help str]) -> Self {
pub fn long_flag_aliases<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<&'help str>,
{
for s in names {
self.long_flag_aliases.push((s, false));
self.long_flag_aliases.push((s.into(), false));
}
self
}
Expand Down Expand Up @@ -1390,13 +1402,18 @@ impl<'help> App<'help> {
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(App::new("test")
/// .visible_aliases(&["do-stuff", "tests"]))
/// .visible_aliases(["do-stuff", "tests"]))
/// .get_matches_from(vec!["myprog", "do-stuff"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
/// [`App::alias`]: App::alias()
pub fn visible_aliases(mut self, names: &[&'help str]) -> Self {
self.aliases.extend(names.iter().map(|n| (*n, true)));
pub fn visible_aliases<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<&'help str>,
{
self.aliases
.extend(names.into_iter().map(|n| (n.into(), true)));
self
}

Expand All @@ -1409,15 +1426,18 @@ impl<'help> App<'help> {
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(App::new("test").short_flag('b')
/// .visible_short_flag_aliases(&['t']))
/// .visible_short_flag_aliases(['t']))
/// .get_matches_from(vec!["myprog", "-t"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
/// [`App::short_flag_aliases`]: App::short_flag_aliases()
pub fn visible_short_flag_aliases(mut self, names: &[char]) -> Self {
pub fn visible_short_flag_aliases<I>(mut self, names: I) -> Self
where
I: IntoIterator<Item = char>,
{
for s in names {
assert!(!(s == &'-'), "short alias name cannot be `-`");
self.short_flag_aliases.push((*s, true));
assert!(!(s == '-'), "short alias name cannot be `-`");
self.short_flag_aliases.push((s, true));
}
self
}
Expand All @@ -1431,14 +1451,18 @@ impl<'help> App<'help> {
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(App::new("test").long_flag("test")
/// .visible_long_flag_aliases(&["testing", "testall", "test_all"]))
/// .visible_long_flag_aliases(["testing", "testall", "test_all"]))
/// .get_matches_from(vec!["myprog", "--testing"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
/// [`App::long_flag_aliases`]: App::long_flag_aliases()
pub fn visible_long_flag_aliases(mut self, names: &[&'help str]) -> Self {
pub fn visible_long_flag_aliases<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<&'help str>,
{
for s in names {
self.long_flag_aliases.push((s, true));
self.long_flag_aliases.push((s.into(), true));
}
self
}
Expand Down
72 changes: 46 additions & 26 deletions src/build/arg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,21 @@ impl<'help> Arg<'help> {
/// let m = App::new("prog")
/// .arg(Arg::new("test")
/// .long("test")
/// .aliases(&["do-stuff", "do-tests", "tests"])
/// .aliases(["do-stuff", "do-tests", "tests"])
/// .about("the file to add")
/// .required(false))
/// .get_matches_from(vec![
/// "prog", "--do-tests"
/// ]);
/// assert!(m.is_present("test"));
/// ```
pub fn aliases(mut self, names: &[&'help str]) -> Self {
self.aliases.extend(names.iter().map(|&x| (x, false)));
pub fn aliases<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<&'help str>,
{
self.aliases
.extend(names.into_iter().map(|x| (x.into(), false)));
self
}

Expand All @@ -504,18 +509,21 @@ impl<'help> Arg<'help> {
/// let m = App::new("prog")
/// .arg(Arg::new("test")
/// .short('t')
/// .short_aliases(&['e', 's'])
/// .short_aliases(['e', 's'])
/// .about("the file to add")
/// .required(false))
/// .get_matches_from(vec![
/// "prog", "-s"
/// ]);
/// assert!(m.is_present("test"));
/// ```
pub fn short_aliases(mut self, names: &[char]) -> Self {
pub fn short_aliases<I>(mut self, names: I) -> Self
where
I: IntoIterator<Item = char>,
{
for s in names {
assert!(s != &'-', "short alias name cannot be `-`");
self.short_aliases.push((*s, false));
assert!(s != '-', "short alias name cannot be `-`");
self.short_aliases.push((s, false));
}
self
}
Expand Down Expand Up @@ -580,15 +588,20 @@ impl<'help> Arg<'help> {
/// let m = App::new("prog")
/// .arg(Arg::new("test")
/// .long("test")
/// .visible_aliases(&["something", "awesome", "cool"]))
/// .visible_aliases(["something", "awesome", "cool"]))
/// .get_matches_from(vec![
/// "prog", "--awesome"
/// ]);
/// assert!(m.is_present("test"));
/// ```
/// [`App::aliases`]: Arg::aliases()
pub fn visible_aliases(mut self, names: &[&'help str]) -> Self {
self.aliases.extend(names.iter().map(|n| (*n, true)));
pub fn visible_aliases<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<&'help str>,
{
self.aliases
.extend(names.into_iter().map(|n| (n.into(), true)));
self
}

Expand All @@ -602,17 +615,20 @@ impl<'help> Arg<'help> {
/// let m = App::new("prog")
/// .arg(Arg::new("test")
/// .long("test")
/// .visible_short_aliases(&['t', 'e']))
/// .visible_short_aliases(['t', 'e']))
/// .get_matches_from(vec![
/// "prog", "-t"
/// ]);
/// assert!(m.is_present("test"));
/// ```
/// [`App::aliases`]: Arg::short_aliases()
pub fn visible_short_aliases(mut self, names: &[char]) -> Self {
pub fn visible_short_aliases<I>(mut self, names: I) -> Self
where
I: IntoIterator<Item = char>,
{
for n in names {
assert!(n != &'-', "short alias name cannot be `-`");
self.short_aliases.push((*n, true));
assert!(n != '-', "short alias name cannot be `-`");
self.short_aliases.push((n, true));
}
self
}
Expand Down Expand Up @@ -2555,15 +2571,15 @@ impl<'help> Arg<'help> {
/// # use clap::{App, Arg};
/// Arg::new("speed")
/// .short('s')
/// .value_names(&["fast", "slow"]);
/// .value_names(["fast", "slow"]);
/// ```
///
/// ```rust
/// # use clap::{App, Arg};
/// let m = App::new("prog")
/// .arg(Arg::new("io")
/// .long("io-files")
/// .value_names(&["INFILE", "OUTFILE"]))
/// .value_names(["INFILE", "OUTFILE"]))
/// .get_matches_from(vec![
/// "prog", "--help"
/// ]);
Expand All @@ -2588,8 +2604,12 @@ impl<'help> Arg<'help> {
/// [`Arg::number_of_values`]: Arg::number_of_values()
/// [`Arg::takes_value(true)`]: Arg::takes_value()
/// [`Arg::multiple_values(true)`]: Arg::multiple_values()
pub fn value_names(mut self, names: &[&'help str]) -> Self {
self.val_names = names.to_vec();
pub fn value_names<I>(mut self, names: I) -> Self
where
I: IntoIterator<Item = &'help str>,
{
self.val_names.clear();
self.val_names.extend(names);
self.takes_value(true)
}

Expand Down Expand Up @@ -2642,7 +2662,7 @@ impl<'help> Arg<'help> {
/// [`Arg::takes_value(true)`]: Arg::takes_value()
#[inline]
pub fn value_name(self, name: &'help str) -> Self {
self.value_names(&[name])
self.value_names([name])
}

/// Specifies the value of the argument when *not* specified at runtime.
Expand Down Expand Up @@ -4099,7 +4119,7 @@ impl<'help> Arg<'help> {
/// .short('o')
/// .setting(ArgSettings::TakesValue)
/// .setting(ArgSettings::NextLineHelp)
/// .value_names(&["value1", "value2"])
/// .value_names(["value1", "value2"])
/// .about("Some really long help and complex\n\
/// help that makes more sense to be\n\
/// on a line after the option"))
Expand Down Expand Up @@ -5204,7 +5224,7 @@ mod test {

#[test]
fn option_display2() {
let o2 = Arg::new("opt").short('o').value_names(&["file", "name"]);
let o2 = Arg::new("opt").short('o').value_names(["file", "name"]);

assert_eq!(&*format!("{}", o2), "-o <file> <name>");
}
Expand All @@ -5215,7 +5235,7 @@ mod test {
.short('o')
.takes_value(true)
.multiple_values(true)
.value_names(&["file", "name"]);
.value_names(["file", "name"]);

assert_eq!(&*format!("{}", o2), "-o <file> <name>");
}
Expand All @@ -5235,7 +5255,7 @@ mod test {
let o = Arg::new("opt")
.long("option")
.takes_value(true)
.visible_aliases(&["als2", "als3", "als4"])
.visible_aliases(["als2", "als3", "als4"])
.alias("als_not_visible");

assert_eq!(&*format!("{}", o), "--option <opt>");
Expand All @@ -5256,7 +5276,7 @@ mod test {
let o = Arg::new("opt")
.short('a')
.takes_value(true)
.visible_short_aliases(&['b', 'c', 'd'])
.visible_short_aliases(['b', 'c', 'd'])
.short_alias('e');

assert_eq!(&*format!("{}", o), "-a <opt>");
Expand Down Expand Up @@ -5293,7 +5313,7 @@ mod test {

#[test]
fn positional_display_val_names() {
let p2 = Arg::new("pos").index(1).value_names(&["file1", "file2"]);
let p2 = Arg::new("pos").index(1).value_names(["file1", "file2"]);

assert_eq!(&*format!("{}", p2), "<file1> <file2>");
}
Expand All @@ -5303,7 +5323,7 @@ mod test {
let p2 = Arg::new("pos")
.index(1)
.setting(ArgSettings::Required)
.value_names(&["file1", "file2"]);
.value_names(["file1", "file2"]);

assert_eq!(&*format!("{}", p2), "<file1> <file2>");
}
Expand Down
Loading

0 comments on commit e86194d

Please sign in to comment.