Skip to content

Commit

Permalink
fix(parser): Deprecate args_override_self
Browse files Browse the repository at this point in the history
This shouldn't be needed anymore now that this is effectively the new
behavior for the non-deprecated actions.

This was briefly talked about in
clap-rs#2627 but I wasn't familiar
enough with the implementation to know how safe it is.  Now, maintainrs
and users can be more confident because they are explicitly opting into
it.

See also clap-rs#3795
  • Loading branch information
epage committed Jun 6, 2022
1 parent a979cf9 commit 1428785
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
7 changes: 3 additions & 4 deletions examples/tutorial_builder/02_app_settings.rs
@@ -1,14 +1,13 @@
// Note: this requires the `cargo` feature

use clap::{arg, command, AppSettings};
use clap::{arg, command, AppSettings, ArgAction};

fn main() {
let matches = command!()
.args_override_self(true)
.global_setting(AppSettings::DeriveDisplayOrder)
.allow_negative_numbers(true)
.arg(arg!(--two <VALUE>))
.arg(arg!(--one <VALUE>))
.arg(arg!(--two <VALUE>).action(ArgAction::Set))
.arg(arg!(--one <VALUE>).action(ArgAction::Set))
.get_matches();

println!(
Expand Down
1 change: 0 additions & 1 deletion examples/tutorial_derive/02_app_settings.rs
Expand Up @@ -2,7 +2,6 @@ use clap::{AppSettings, Parser};

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(args_override_self = true)]
#[clap(allow_negative_numbers = true)]
#[clap(global_setting(AppSettings::DeriveDisplayOrder))]
struct Cli {
Expand Down
13 changes: 2 additions & 11 deletions src/builder/command.rs
Expand Up @@ -910,17 +910,8 @@ impl<'help> App<'help> {
}
}

/// Specifies that all arguments override themselves.
///
/// This is the equivalent to saying the `foo` arg using [`Arg::overrides_with("foo")`] for all
/// defined arguments.
///
/// **NOTE:** This will not be applied when [`Arg::multiple_occurrences(true)`].
///
/// **NOTE:** This choice is propagated to all child subcommands.
///
/// [`Arg::overrides_with("foo")`]: crate::Arg::overrides_with()
#[inline]
/// Deprecated, replaced with [`ArgAction::Set`][super::ArgAction::Set]
#[deprecated(since = "3.2.0", note = "Replaced with `Arg::action(ArgAction::Set)`")]
pub fn args_override_self(self, yes: bool) -> Self {
if yes {
self.global_setting(AppSettings::AllArgsOverrideSelf)
Expand Down
13 changes: 13 additions & 0 deletions tests/builder/app_settings.rs
Expand Up @@ -1064,6 +1064,7 @@ fn built_in_subcommand_escaped() {

#[test]
fn aaos_flags() {
#![allow(deprecated)]
// flags
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1077,6 +1078,7 @@ fn aaos_flags() {

#[test]
fn aaos_flags_mult() {
#![allow(deprecated)]
// flags with multiple
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1090,6 +1092,7 @@ fn aaos_flags_mult() {

#[test]
fn aaos_opts() {
#![allow(deprecated)]
// opts
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1107,6 +1110,7 @@ fn aaos_opts() {

#[test]
fn aaos_opts_w_other_overrides() {
#![allow(deprecated)]
// opts with other overrides
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1130,6 +1134,7 @@ fn aaos_opts_w_other_overrides() {

#[test]
fn aaos_opts_w_other_overrides_rev() {
#![allow(deprecated)]
// opts with other overrides, rev
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1152,6 +1157,7 @@ fn aaos_opts_w_other_overrides_rev() {

#[test]
fn aaos_opts_w_other_overrides_2() {
#![allow(deprecated)]
// opts with other overrides
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1175,6 +1181,7 @@ fn aaos_opts_w_other_overrides_2() {

#[test]
fn aaos_opts_w_other_overrides_rev_2() {
#![allow(deprecated)]
// opts with other overrides, rev
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1197,6 +1204,7 @@ fn aaos_opts_w_other_overrides_rev_2() {

#[test]
fn aaos_opts_w_override_as_conflict_1() {
#![allow(deprecated)]
// opts with other overrides, rev
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1216,6 +1224,7 @@ fn aaos_opts_w_override_as_conflict_1() {

#[test]
fn aaos_opts_w_override_as_conflict_2() {
#![allow(deprecated)]
// opts with other overrides, rev
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1238,6 +1247,7 @@ fn aaos_opts_w_override_as_conflict_2() {

#[test]
fn aaos_opts_mult() {
#![allow(deprecated)]
// opts with multiple
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1264,6 +1274,7 @@ fn aaos_opts_mult() {

#[test]
fn aaos_opts_mult_req_delims() {
#![allow(deprecated)]
// opts with multiple and require delims
let res = Command::new("posix")
.args_override_self(true)
Expand Down Expand Up @@ -1293,6 +1304,7 @@ fn aaos_opts_mult_req_delims() {

#[test]
fn aaos_pos_mult() {
#![allow(deprecated)]
// opts with multiple
let res = Command::new("posix")
.args_override_self(true)
Expand All @@ -1313,6 +1325,7 @@ fn aaos_pos_mult() {

#[test]
fn aaos_option_use_delim_false() {
#![allow(deprecated)]
let m = Command::new("posix")
.args_override_self(true)
.arg(arg!(--opt <val> "some option").use_value_delimiter(false))
Expand Down
36 changes: 34 additions & 2 deletions tests/builder/grouped_values.rs
@@ -1,6 +1,6 @@
#![cfg(feature = "unstable-grouped")]

use clap::{Arg, Command};
use clap::{Arg, ArgAction, Command};

#[test]
fn grouped_value_works() {
Expand Down Expand Up @@ -251,7 +251,8 @@ fn issue_1374() {
}

#[test]
fn issue_2171() {
fn issue_2171_deprecated() {
#![allow(deprecated)]
let schema = Command::new("ripgrep#1701 reproducer")
.args_override_self(true)
.arg(Arg::new("pretty").short('p').long("pretty"))
Expand All @@ -271,3 +272,34 @@ fn issue_2171() {
let _ = schema.clone().try_get_matches_from(argv).unwrap();
}
}

#[test]
fn issue_2171() {
let schema = Command::new("ripgrep#1701 reproducer")
.arg(
Arg::new("pretty")
.short('p')
.long("pretty")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("search_zip")
.short('z')
.long("search-zip")
.action(ArgAction::SetTrue),
);

let test_args = &[
vec!["reproducer", "-pz", "-p"],
vec!["reproducer", "-pzp"],
vec!["reproducer", "-zpp"],
vec!["reproducer", "-pp", "-z"],
vec!["reproducer", "-p", "-p", "-z"],
vec!["reproducer", "-p", "-pz"],
vec!["reproducer", "-ppz"],
];

for argv in test_args {
let _ = schema.clone().try_get_matches_from(argv).unwrap();
}
}

0 comments on commit 1428785

Please sign in to comment.