Skip to content

Commit

Permalink
depr(Arg.rs): adds deprecations in prep for v3
Browse files Browse the repository at this point in the history
A full list of deprecations is:

* Arg::last -> ArgSettings::Last
* Arg::required -> ArgSettings::Required
* Arg::require_equals -> ArgSettings::RequireEquals
* Arg::allow_hyphen_values -> ArgSettings::AllowHyphenValues
* Arg::takes_value -> ArgSettings::TakesValue
* Arg::hide_possible_values -> ArgSettings::HidePossibleValues
* Arg::hide_default_value -> ArgSettings::HideDefaultValue
* Arg::multiple -> ArgSettings::Multiple (see Arg::multiple split)
* Arg::multiple -> ArgSettings::MultipleValues (see Arg::multiple split)
* Arg::multiple -> ArgSettings::MultipleOccurrences (see Arg::multiple split)
* Arg::global -> ArgSettings::Global
* Arg::empty_values -> ArgSettings::AllowEmptyValues
* Arg::hidden -> ArgSettings::Hidden
* Arg::case_insensitive -> ArgSettings::IgnoreCase
* Arg::use_delimiter -> ArgSettings::UseDelimiter
* Arg::require_delimiter -> ArgSettings::RequireDelimiter
* Arg::hide_env_values -> ArgSettings::HideEnvValues
* Arg::next_line_help -> ArgSettings::NextLineHelp
* Arg::set -> Arg::unset_setting (consistent naming with App)
* Arg::unset -> Arg::setting (consistent naming with App)

Relates to #1037
  • Loading branch information
kbknapp committed Feb 3, 2018
1 parent e1f1b47 commit 6fc70d8
Show file tree
Hide file tree
Showing 14 changed files with 1,916 additions and 1,609 deletions.
8 changes: 5 additions & 3 deletions src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ impl<'w> Help<'w> {
/// Writes argument's possible values to the wrapped stream.
fn val<'b, 'c>(&mut self, arg: &Arg<'b, 'c>) -> Result<String, io::Error> {
debugln!("Help::val: arg={}", arg.name);
let mult =
arg.is_set(ArgSettings::MultipleValues) || arg.is_set(ArgSettings::MultipleOccurrences);
if arg.is_set(ArgSettings::TakesValue) {
let delim = if arg.is_set(ArgSettings::RequireDelimiter) {
arg.val_delim.expect(INTERNAL_ERROR_MSG)
Expand All @@ -311,7 +313,7 @@ impl<'w> Help<'w> {
}
}
let num = vec.len();
if arg.is_set(ArgSettings::Multiple) && num == 1 {
if mult && num == 1 {
color!(self, "...", good)?;
}
} else if let Some(num) = arg.num_vals {
Expand All @@ -322,12 +324,12 @@ impl<'w> Help<'w> {
write!(self.writer, "{}", delim)?;
}
}
if arg.is_set(ArgSettings::Multiple) && num == 1 {
if mult && num == 1 {
color!(self, "...", good)?;
}
} else if arg.has_switch() {
color!(self, "<{}>", arg.name, good)?;
if arg.is_set(ArgSettings::Multiple) {
if mult {
color!(self, "...", good)?;
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,8 +1564,8 @@ impl<'a, 'b> App<'a, 'b> {
assert!(
positionals!(self).fold(0, |acc, p| if p.index == Some(idx as u64){acc+1}else{acc}) < 2,
"Argument '{}' has the same index as another positional \
argument\n\n\tUse Arg::multiple(true) to allow one positional argument \
to take multiple values",
argument\n\n\tUse Arg::setting(ArgSettings::MultipleValues) to allow one \
positional argument to take multiple values",
a.name
);
}
Expand Down
26 changes: 14 additions & 12 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ where

// Next we verify that only the highest index has a .multiple(true) (if any)
if positionals!(self.app).any(|a| {
a.is_set(ArgSettings::Multiple) && (a.index.unwrap_or(0) != highest_idx as u64)
a.is_set(ArgSettings::MultipleValues) && (a.index.unwrap_or(0) != highest_idx as u64)
}) {
// First we make sure if there is a positional that allows multiple values
// the one before it (second to last) has one of these:
Expand Down Expand Up @@ -157,7 +157,8 @@ where
);

// We make sure if the second to last is Multiple the last is ArgSettings::Last
let ok = second_to_last.is_set(ArgSettings::Multiple) || last.is_set(ArgSettings::Last);
let ok = second_to_last.is_set(ArgSettings::MultipleValues)
|| last.is_set(ArgSettings::Last);
assert!(
ok,
"Only the last positional argument, or second to last positional \
Expand All @@ -166,15 +167,15 @@ where

// Next we check how many have both Multiple and not a specific number of values set
let count = positionals!(self.app).fold(0, |acc, p| {
if p.settings.is_set(ArgSettings::Multiple) && p.num_vals.is_none() {
if p.settings.is_set(ArgSettings::MultipleValues) && p.num_vals.is_none() {
acc + 1
} else {
acc
}
});
let ok = count <= 1
|| (last.is_set(ArgSettings::Last) && last.is_set(ArgSettings::Multiple)
&& second_to_last.is_set(ArgSettings::Multiple)
|| (last.is_set(ArgSettings::Last) && last.is_set(ArgSettings::MultipleValues)
&& second_to_last.is_set(ArgSettings::MultipleValues)
&& count == 2);
assert!(
ok,
Expand Down Expand Up @@ -307,7 +308,7 @@ where
debug_assert!(self._verify_positionals());
// Set the LowIndexMultiple flag if required
if positionals!(self.app).any(|a| {
a.is_set(ArgSettings::Multiple)
a.is_set(ArgSettings::MultipleValues)
&& (a.index.unwrap_or(0) as usize != self.positionals.len())
}) && self.positionals.values().last().map_or(false, |p_name| {
!find!(self.app, p_name)
Expand Down Expand Up @@ -535,7 +536,7 @@ where

self.app.settings.set(AS::ValidArgFound);
// Only increment the positional counter if it doesn't allow multiples
if !p.settings.is_set(ArgSettings::Multiple) {
if !p.settings.is_set(ArgSettings::MultipleValues) {
pos_counter += 1;
}
self.app.settings.set(AS::ValidArgFound);
Expand Down Expand Up @@ -721,7 +722,7 @@ where
if help_help {
let mut pb = Arg::with_name("subcommand")
.index(1)
.set(ArgSettings::Multiple)
.setting(ArgSettings::MultipleValues)
.help("The subcommand whose help message to display");
pb._build();
parser.positionals.insert(1, pb.name);
Expand Down Expand Up @@ -754,11 +755,11 @@ where
let arg_allows_tac = match needs_val_of {
ParseResult::Opt(name) => {
let o = find!(self.app, &name).expect(INTERNAL_ERROR_MSG);
(o.is_set(ArgSettings::AllowLeadingHyphen) || app_wide_settings)
(o.is_set(ArgSettings::AllowHyphenValues) || app_wide_settings)
}
ParseResult::Pos(name) => {
let p = find!(self.app, &name).expect(INTERNAL_ERROR_MSG);
(p.is_set(ArgSettings::AllowLeadingHyphen) || app_wide_settings)
(p.is_set(ArgSettings::AllowHyphenValues) || app_wide_settings)
}
ParseResult::ValuesDone => return true,
_ => false,
Expand Down Expand Up @@ -1061,7 +1062,7 @@ where
debugln!("Parser::parse_opt; opt.settings={:?}", opt.settings);
let mut has_eq = false;
let no_val = val.is_none();
let empty_vals = opt.is_set(ArgSettings::EmptyValues);
let empty_vals = opt.is_set(ArgSettings::AllowEmptyValues);
let min_vals_zero = opt.min_vals.unwrap_or(1) == 0;
let needs_eq = opt.is_set(ArgSettings::RequireEquals);

Expand Down Expand Up @@ -1101,7 +1102,8 @@ where
.and_then(|vec| Some(matcher.inc_occurrences_of(&*vec)));

let needs_delim = opt.is_set(ArgSettings::RequireDelimiter);
let mult = opt.is_set(ArgSettings::Multiple);
let mult = opt.is_set(ArgSettings::MultipleValues);
// @TODO @soundness: if doesn't have an equal, but requires equal is ValuesDone?!
if no_val && min_vals_zero && !has_eq && needs_eq {
debugln!("Parser::parse_opt: More arg vals not required...");
return Ok(ParseResult::ValuesDone);
Expand Down
2 changes: 1 addition & 1 deletion src/app/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'a, 'b, 'c, 'z> Usage<'a, 'b, 'c, 'z> {
let has_last = positionals!(self.0.app).any(|p| p.is_set(ArgSettings::Last));
// places a '--' in the usage string if there are args and options
// supporting multiple values
if opts!(self.0.app).any(|o| o.is_set(ArgSettings::Multiple))
if opts!(self.0.app).any(|o| o.is_set(ArgSettings::MultipleValues))
&& positionals!(self.0.app).any(|p| !p.is_set(ArgSettings::Required))
&& !(self.0.app.has_visible_subcommands()
|| self.0.is_set(AS::AllowExternalSubcommands)) && !has_last
Expand Down
13 changes: 7 additions & 6 deletions src/app/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> {
if let Some(ref p_vals) = arg.possible_vals {
debugln!("Validator::validate_arg_values: possible_vals={:?}", p_vals);
let val_str = val.to_string_lossy();
let ok = if arg.is_set(ArgSettings::CaseInsensitive) {
let ok = if arg.is_set(ArgSettings::IgnoreCase) {
p_vals.iter().any(|pv| pv.eq_ignore_ascii_case(&*val_str))
} else {
p_vals.contains(&&*val_str)
Expand All @@ -110,7 +110,7 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> {
));
}
}
if !arg.is_set(ArgSettings::EmptyValues) && val.is_empty_()
if !arg.is_set(ArgSettings::AllowEmptyValues) && val.is_empty_()
&& matcher.contains(&*arg.name)
{
debugln!("Validator::validate_arg_values: illegal empty val found");
Expand Down Expand Up @@ -292,7 +292,7 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> {
matcher: &ArgMatcher<'a>,
) -> ClapResult<()> {
debugln!("Validator::validate_arg_num_occurs: a={};", a.name);
if ma.occurs > 1 && !a.is_set(ArgSettings::Multiple) {
if ma.occurs > 1 && !a.is_set(ArgSettings::MultipleOccurrences) {
// Not the first time, and we don't allow multiples
return Err(Error::unexpected_multiple_usage(
a,
Expand All @@ -312,7 +312,7 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> {
debugln!("Validator::validate_arg_num_vals;");
if let Some(num) = a.num_vals {
debugln!("Validator::validate_arg_num_vals: num_vals set...{}", num);
let should_err = if a.is_set(ArgSettings::Multiple) {
let should_err = if a.is_set(ArgSettings::MultipleValues) {
((ma.vals.len() as u64) % num) != 0
} else {
num != (ma.vals.len() as u64)
Expand All @@ -322,13 +322,14 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> {
return Err(Error::wrong_number_of_values(
a,
num,
if a.is_set(ArgSettings::Multiple) {
if a.is_set(ArgSettings::MultipleValues) {
(ma.vals.len() % num as usize)
} else {
ma.vals.len()
},
if ma.vals.len() == 1
|| (a.is_set(ArgSettings::Multiple) && (ma.vals.len() % num as usize) == 1)
|| (a.is_set(ArgSettings::MultipleValues)
&& (ma.vals.len() % num as usize) == 1)
{
"as"
} else {
Expand Down
Loading

0 comments on commit 6fc70d8

Please sign in to comment.