mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 23:04:23 +00:00
fix: Deprecate Command::allow_negative_numbers
Better to set on individual args
This commit is contained in:
parent
8ad29ef337
commit
1258f3e5f6
7 changed files with 50 additions and 43 deletions
|
@ -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
|
||||
|
|
|
@ -6,13 +6,16 @@ Usage:
|
|||
02_app_settings[EXE] --two <VALUE> --one <VALUE>
|
||||
|
||||
Options:
|
||||
--two <VALUE>
|
||||
--one <VALUE>
|
||||
-h, --help Print help information
|
||||
-V, --version Print version information
|
||||
--two <VALUE>
|
||||
|
||||
|
||||
$ 02_app_settings --one -1 --one -3 --two 10
|
||||
two: "10"
|
||||
one: "-3"
|
||||
--one <VALUE>
|
||||
|
||||
|
||||
-h, --help
|
||||
Print help information
|
||||
|
||||
-V, --version
|
||||
Print version information
|
||||
|
||||
```
|
||||
|
|
|
@ -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 <VALUE>).action(ArgAction::Set))
|
||||
.arg(arg!(--one <VALUE>).action(ArgAction::Set))
|
||||
.get_matches();
|
||||
|
|
|
@ -6,13 +6,16 @@ Usage:
|
|||
02_app_settings_derive[EXE] --two <TWO> --one <ONE>
|
||||
|
||||
Options:
|
||||
--two <TWO>
|
||||
--one <ONE>
|
||||
-h, --help Print help information
|
||||
-V, --version Print version information
|
||||
--two <TWO>
|
||||
|
||||
|
||||
$ 02_app_settings_derive --one -1 --one -3 --two 10
|
||||
two: "10"
|
||||
one: "-3"
|
||||
--one <ONE>
|
||||
|
||||
|
||||
-h, --help
|
||||
Print help information
|
||||
|
||||
-V, --version
|
||||
Print version information
|
||||
|
||||
```
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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::<String>("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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue