From d45e4be14bc77562deac740e7a287f26fd2204ac Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 6 Sep 2022 17:12:17 -0500 Subject: [PATCH] fix(derive): Deprecate Command::allow_hyphen_values Fixes #3450 --- CHANGELOG.md | 1 + src/builder/command.rs | 40 +++++++++----------------- tests/builder/app_settings.rs | 39 ++++++------------------- tests/builder/positionals.rs | 2 +- tests/derive/non_literal_attributes.rs | 2 +- 5 files changed, 25 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a66fb3409a0..c0eacb97551 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ Deprecated - `default_value_os`, `default_values_os`, `default_value_if_os`, and `default_value_ifs_os` as the non `_os` variants now accept either a `str` or an `OsStr` - `Command::dont_collapse_args_in_usage` is now the default and is deprecated - `Command::trailing_var_arg` in favor of `Arg::trailing_var_arg` +- `Command::allow_hyphen_values` in favor of `Arg::allow_hyphen_values` - *(derive)* `structopt` and `clap` attributes in favor of the more specific `command`, `arg`, and `value` ### Features diff --git a/src/builder/command.rs b/src/builder/command.rs index d1f0e1f59f8..1469b529bf8 100644 --- a/src/builder/command.rs +++ b/src/builder/command.rs @@ -1938,32 +1938,11 @@ impl Command { } } - /// Specifies that leading hyphens are allowed in all argument *values* (e.g. `-10`). - /// - /// Otherwise they will be parsed as another flag or option. See also - /// [`Command::allow_negative_numbers`]. - /// - /// **NOTE:** Use this setting with caution as it silences certain circumstances which would - /// otherwise be an error (such as accidentally forgetting to specify a value for leading - /// option). It is preferred to set this on a per argument basis, via [`Arg::allow_hyphen_values`]. - /// - /// # Examples - /// - /// ```rust - /// # use clap::{Arg, Command}; - /// // Imagine you needed to represent negative numbers as well, such as -10 - /// let m = Command::new("nums") - /// .allow_hyphen_values(true) - /// .arg(Arg::new("neg")) - /// .get_matches_from(vec![ - /// "nums", "-20" - /// ]); - /// - /// assert_eq!(m.get_one::("neg").unwrap(), "-20"); - /// # ; - /// ``` - /// [`Arg::allow_hyphen_values`]: crate::Arg::allow_hyphen_values() - #[inline] + #[doc(hidden)] + #[cfg_attr( + feature = "deprecated", + deprecated(since = "4.0.0", note = "Replaced with `Arg::allow_hyphen_values`") + )] pub fn allow_hyphen_values(self, yes: bool) -> Self { if yes { self.setting(AppSettings::AllowHyphenValues) @@ -3573,7 +3552,14 @@ impl Command { self.is_set(AppSettings::ArgRequiredElseHelp) } - /// Report whether [`Command::allow_hyphen_values`] is set + #[doc(hidden)] + #[cfg_attr( + feature = "deprecated", + deprecated( + since = "4.0.0", + note = "Replaced with `Arg::is_allow_hyphen_values_set`" + ) + )] pub(crate) fn is_allow_hyphen_values_set(&self) -> bool { self.is_set(AppSettings::AllowHyphenValues) } diff --git a/tests/builder/app_settings.rs b/tests/builder/app_settings.rs index aaae4306254..3695ce6c3ac 100644 --- a/tests/builder/app_settings.rs +++ b/tests/builder/app_settings.rs @@ -428,8 +428,7 @@ fn delim_values_trailingvararg_with_delim() { #[test] fn leading_hyphen_short() { let res = Command::new("leadhy") - .allow_hyphen_values(true) - .arg(Arg::new("some")) + .arg(Arg::new("some").allow_hyphen_values(true)) .arg(Arg::new("other").short('o').action(ArgAction::SetTrue)) .try_get_matches_from(vec!["", "-bar", "-o"]); assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind()); @@ -449,8 +448,7 @@ fn leading_hyphen_short() { #[test] fn leading_hyphen_long() { let res = Command::new("leadhy") - .allow_hyphen_values(true) - .arg(Arg::new("some")) + .arg(Arg::new("some").allow_hyphen_values(true)) .arg(Arg::new("other").short('o').action(ArgAction::SetTrue)) .try_get_matches_from(vec!["", "--bar", "-o"]); assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind()); @@ -470,8 +468,12 @@ fn leading_hyphen_long() { #[test] fn leading_hyphen_opt() { let res = Command::new("leadhy") - .allow_hyphen_values(true) - .arg(Arg::new("some").action(ArgAction::Set).long("opt")) + .arg( + Arg::new("some") + .action(ArgAction::Set) + .long("opt") + .allow_hyphen_values(true), + ) .arg(Arg::new("other").short('o').action(ArgAction::SetTrue)) .try_get_matches_from(vec!["", "--opt", "--bar", "-o"]); assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind()); @@ -835,33 +837,10 @@ fn missing_positional_hyphen_req_error() { assert_eq!(r.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); } -#[test] -fn issue_1066_allow_leading_hyphen_and_unknown_args() { - let res = Command::new("prog") - .allow_hyphen_values(true) - .arg(arg!(--"some-argument")) - .try_get_matches_from(vec!["prog", "hello"]); - - assert!(res.is_err()); - assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument); -} - -#[test] -fn issue_1066_allow_leading_hyphen_and_unknown_args_no_vals() { - let res = Command::new("prog") - .allow_hyphen_values(true) - .arg(arg!(--"some-argument")) - .try_get_matches_from(vec!["prog", "--hello"]); - - assert!(res.is_err()); - assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument); -} - #[test] fn issue_1066_allow_leading_hyphen_and_unknown_args_option() { let res = Command::new("prog") - .allow_hyphen_values(true) - .arg(arg!(--"some-argument" )) + .arg(arg!(--"some-argument" ).allow_hyphen_values(true)) .try_get_matches_from(vec!["prog", "-fish"]); assert!(res.is_err()); diff --git a/tests/builder/positionals.rs b/tests/builder/positionals.rs index 7be092f759e..072770fa18e 100644 --- a/tests/builder/positionals.rs +++ b/tests/builder/positionals.rs @@ -18,12 +18,12 @@ fn only_pos_follow() { #[test] fn issue_946() { let r = Command::new("compiletest") - .allow_hyphen_values(true) .arg(arg!(--exact "filters match exactly").action(ArgAction::SetTrue)) .arg( clap::Arg::new("filter") .index(1) .action(ArgAction::Set) + .allow_hyphen_values(true) .help("filters to apply to output"), ) .try_get_matches_from(vec!["compiletest", "--exact"]); diff --git a/tests/derive/non_literal_attributes.rs b/tests/derive/non_literal_attributes.rs index 69441de2401..767c3774c26 100644 --- a/tests/derive/non_literal_attributes.rs +++ b/tests/derive/non_literal_attributes.rs @@ -20,7 +20,7 @@ pub const DISPLAY_ORDER: usize = 2; // Check if the global settings compile #[derive(Parser, Debug, PartialEq, Eq)] -#[command(allow_hyphen_values = true)] +#[command(group = clap::ArgGroup::new("foo"))] struct Opt { #[arg( long = "x",