From 1258f3e5f6d65683947c0dbca5e6b50dd0e73982 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 7 Sep 2022 07:24:42 -0500 Subject: [PATCH] fix: Deprecate `Command::allow_negative_numbers` Better to set on individual args --- CHANGELOG.md | 1 + examples/tutorial_builder/02_app_settings.md | 17 ++++++---- examples/tutorial_builder/02_app_settings.rs | 2 +- examples/tutorial_derive/02_app_settings.md | 17 ++++++---- examples/tutorial_derive/02_app_settings.rs | 2 +- src/builder/command.rs | 34 ++++++++------------ tests/builder/app_settings.rs | 20 ++++++++---- 7 files changed, 50 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0eacb97..985291df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ Deprecated - `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` +- `Command::allow_negative_numbers` in favor of `Arg::allow_negative_numbers` - *(derive)* `structopt` and `clap` attributes in favor of the more specific `command`, `arg`, and `value` ### Features diff --git a/examples/tutorial_builder/02_app_settings.md b/examples/tutorial_builder/02_app_settings.md index 8417b616..d3989830 100644 --- a/examples/tutorial_builder/02_app_settings.md +++ b/examples/tutorial_builder/02_app_settings.md @@ -6,13 +6,16 @@ Usage: 02_app_settings[EXE] --two --one Options: - --two - --one - -h, --help Print help information - -V, --version Print version information + --two + -$ 02_app_settings --one -1 --one -3 --two 10 -two: "10" -one: "-3" + --one + + + -h, --help + Print help information + + -V, --version + Print version information ``` diff --git a/examples/tutorial_builder/02_app_settings.rs b/examples/tutorial_builder/02_app_settings.rs index ee221976..8d64b4dc 100644 --- a/examples/tutorial_builder/02_app_settings.rs +++ b/examples/tutorial_builder/02_app_settings.rs @@ -2,7 +2,7 @@ use clap::{arg, command, ArgAction}; fn main() { let matches = command!() // requires `cargo` feature - .allow_negative_numbers(true) + .next_line_help(true) .arg(arg!(--two ).action(ArgAction::Set)) .arg(arg!(--one ).action(ArgAction::Set)) .get_matches(); diff --git a/examples/tutorial_derive/02_app_settings.md b/examples/tutorial_derive/02_app_settings.md index 4cf93682..4efd7fd0 100644 --- a/examples/tutorial_derive/02_app_settings.md +++ b/examples/tutorial_derive/02_app_settings.md @@ -6,13 +6,16 @@ Usage: 02_app_settings_derive[EXE] --two --one Options: - --two - --one - -h, --help Print help information - -V, --version Print version information + --two + -$ 02_app_settings_derive --one -1 --one -3 --two 10 -two: "10" -one: "-3" + --one + + + -h, --help + Print help information + + -V, --version + Print version information ``` diff --git a/examples/tutorial_derive/02_app_settings.rs b/examples/tutorial_derive/02_app_settings.rs index 8dff1a89..abea13d1 100644 --- a/examples/tutorial_derive/02_app_settings.rs +++ b/examples/tutorial_derive/02_app_settings.rs @@ -2,7 +2,7 @@ use clap::Parser; #[derive(Parser)] #[command(author, version, about, long_about = None)] -#[command(allow_negative_numbers = true)] +#[command(next_line_help = true)] struct Cli { #[arg(long)] two: String, diff --git a/src/builder/command.rs b/src/builder/command.rs index 4f306195..a9e2b430 100644 --- a/src/builder/command.rs +++ b/src/builder/command.rs @@ -1951,26 +1951,11 @@ impl Command { } } - /// Allows negative numbers to pass as values. - /// - /// This is similar to [`Command::allow_hyphen_values`] except that it only allows numbers, - /// all other undefined leading hyphens will fail to parse. - /// - /// # Examples - /// - /// ```rust - /// # use clap::{Command, Arg}; - /// let res = Command::new("myprog") - /// .allow_negative_numbers(true) - /// .arg(Arg::new("num")) - /// .try_get_matches_from(vec![ - /// "myprog", "-20" - /// ]); - /// assert!(res.is_ok()); - /// let m = res.unwrap(); - /// assert_eq!(m.get_one::("num").unwrap(), "-20"); - /// ``` - #[inline] + #[doc(hidden)] + #[cfg_attr( + feature = "deprecated", + deprecated(since = "4.0.0", note = "Replaced with `Arg::allow_negative_numbers`") + )] pub fn allow_negative_numbers(self, yes: bool) -> Self { if yes { self.setting(AppSettings::AllowNegativeNumbers) @@ -3564,7 +3549,14 @@ impl Command { self.is_set(AppSettings::AllowHyphenValues) } - /// Report whether [`Command::allow_negative_numbers`] is set + #[doc(hidden)] + #[cfg_attr( + feature = "deprecated", + deprecated( + since = "4.0.0", + note = "Replaced with `Arg::is_allow_negative_numbers_set`" + ) + )] pub fn is_allow_negative_numbers_set(&self) -> bool { self.is_set(AppSettings::AllowNegativeNumbers) } diff --git a/tests/builder/app_settings.rs b/tests/builder/app_settings.rs index e1be7c57..edef5581 100644 --- a/tests/builder/app_settings.rs +++ b/tests/builder/app_settings.rs @@ -493,9 +493,13 @@ fn leading_hyphen_opt() { #[test] fn allow_negative_numbers_success() { let res = Command::new("negnum") - .allow_negative_numbers(true) - .arg(Arg::new("panum")) - .arg(Arg::new("onum").short('o').action(ArgAction::Set)) + .arg(Arg::new("panum").allow_negative_numbers(true)) + .arg( + Arg::new("onum") + .short('o') + .action(ArgAction::Set) + .allow_negative_numbers(true), + ) .try_get_matches_from(vec!["negnum", "-20", "-o", "-1.2"]); assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind()); let m = res.unwrap(); @@ -512,9 +516,13 @@ fn allow_negative_numbers_success() { #[test] fn allow_negative_numbers_fail() { let res = Command::new("negnum") - .allow_negative_numbers(true) - .arg(Arg::new("panum")) - .arg(Arg::new("onum").short('o').action(ArgAction::Set)) + .arg(Arg::new("panum").allow_negative_numbers(true)) + .arg( + Arg::new("onum") + .short('o') + .action(ArgAction::Set) + .allow_negative_numbers(true), + ) .try_get_matches_from(vec!["negnum", "--foo", "-o", "-1.2"]); assert!(res.is_err()); assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument)