Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Builder pattern methods receiving str array parameters to instead take ref iterators #2414

44 changes: 33 additions & 11 deletions src/build/arg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,13 @@ impl<'help> Arg<'help> {
/// assert!(m.is_present("test"));
/// ```
/// [`Arg`]: ./struct.Arg.html
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 = &'help T>,
T: AsRef<str> + 'help,
{
self.aliases
.extend(names.into_iter().map(|x| (x.as_ref(), false)));
self
}

Expand Down Expand Up @@ -583,8 +588,13 @@ impl<'help> Arg<'help> {
/// ```
/// [`Arg`]: ./struct.Arg.html
/// [`App::aliases`]: ./struct.Arg.html#method.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 = &'help T>,
T: AsRef<str> + 'help,
{
self.aliases
.extend(names.into_iter().map(|n| (n.as_ref(), true)));
self
}

Expand Down Expand Up @@ -1047,8 +1057,12 @@ impl<'help> Arg<'help> {
/// ```
/// [`Arg::conflicts_with`]: ./struct.Arg.html#method.conflicts_with
/// [`Arg::exclusive(true)`]: ./struct.Arg.html#method.exclusive
pub fn conflicts_with_all(mut self, names: &[&str]) -> Self {
self.blacklist.extend(names.iter().map(Id::from));
pub fn conflicts_with_all<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = &'help T>,
T: AsRef<str> + 'help + Display + std::hash::Hash,
{
self.blacklist.extend(names.into_iter().map(Id::from));
self
}

Expand Down Expand Up @@ -1870,8 +1884,13 @@ impl<'help> Arg<'help> {
/// ```
/// [options]: ./struct.Arg.html#method.takes_value
/// [positional arguments]: ./struct.Arg.html#method.index
pub fn possible_values(mut self, names: &[&'help str]) -> Self {
self.possible_vals.extend(names);
pub fn possible_values<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = &'help T>,
T: AsRef<str> + 'help,
{
self.possible_vals
.extend(names.into_iter().map(|name| name.as_ref()));
self.takes_value(true)
}

Expand Down Expand Up @@ -2423,13 +2442,16 @@ impl<'help> Arg<'help> {
/// [`Arg::number_of_values`]: ./struct.Arg.html#method.number_of_values
/// [`Arg::takes_value(true)`]: ./struct.Arg.html#method.takes_value
/// [`Arg::multiple(true)`]: ./struct.Arg.html#method.multiple
pub fn value_names(mut self, names: &[&'help str]) -> Self {
pub fn value_names<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = &'help T>,
T: AsRef<str> + 'help,
{
let mut i = self.val_names.len();
for s in names {
self.val_names.insert(i, s);
self.val_names.insert(i, s.as_ref());
i += 1;
}

self.takes_value(true)
}

Expand Down