mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 07:17:26 +00:00
fix(parser): Deprecate args_override_self
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 https://github.com/clap-rs/clap/discussions/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 #3795
This commit is contained in:
parent
a979cf9bb8
commit
1428785677
5 changed files with 52 additions and 18 deletions
|
@ -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!(
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -1090,6 +1092,7 @@ fn aaos_flags_mult() {
|
|||
|
||||
#[test]
|
||||
fn aaos_opts() {
|
||||
#![allow(deprecated)]
|
||||
// opts
|
||||
let res = Command::new("posix")
|
||||
.args_override_self(true)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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))
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#![cfg(feature = "unstable-grouped")]
|
||||
|
||||
use clap::{Arg, Command};
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
|
||||
#[test]
|
||||
fn grouped_value_works() {
|
||||
|
@ -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"))
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue