mirror of
https://github.com/clap-rs/clap
synced 2024-11-12 23:57:10 +00:00
parent
7a2bbca62b
commit
d45e4be14b
5 changed files with 25 additions and 59 deletions
|
@ -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`
|
- `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::dont_collapse_args_in_usage` is now the default and is deprecated
|
||||||
- `Command::trailing_var_arg` in favor of `Arg::trailing_var_arg`
|
- `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`
|
- *(derive)* `structopt` and `clap` attributes in favor of the more specific `command`, `arg`, and `value`
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
|
@ -1938,32 +1938,11 @@ impl Command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specifies that leading hyphens are allowed in all argument *values* (e.g. `-10`).
|
#[doc(hidden)]
|
||||||
///
|
#[cfg_attr(
|
||||||
/// Otherwise they will be parsed as another flag or option. See also
|
feature = "deprecated",
|
||||||
/// [`Command::allow_negative_numbers`].
|
deprecated(since = "4.0.0", note = "Replaced with `Arg::allow_hyphen_values`")
|
||||||
///
|
)]
|
||||||
/// **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::<String>("neg").unwrap(), "-20");
|
|
||||||
/// # ;
|
|
||||||
/// ```
|
|
||||||
/// [`Arg::allow_hyphen_values`]: crate::Arg::allow_hyphen_values()
|
|
||||||
#[inline]
|
|
||||||
pub fn allow_hyphen_values(self, yes: bool) -> Self {
|
pub fn allow_hyphen_values(self, yes: bool) -> Self {
|
||||||
if yes {
|
if yes {
|
||||||
self.setting(AppSettings::AllowHyphenValues)
|
self.setting(AppSettings::AllowHyphenValues)
|
||||||
|
@ -3573,7 +3552,14 @@ impl Command {
|
||||||
self.is_set(AppSettings::ArgRequiredElseHelp)
|
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 {
|
pub(crate) fn is_allow_hyphen_values_set(&self) -> bool {
|
||||||
self.is_set(AppSettings::AllowHyphenValues)
|
self.is_set(AppSettings::AllowHyphenValues)
|
||||||
}
|
}
|
||||||
|
|
|
@ -428,8 +428,7 @@ fn delim_values_trailingvararg_with_delim() {
|
||||||
#[test]
|
#[test]
|
||||||
fn leading_hyphen_short() {
|
fn leading_hyphen_short() {
|
||||||
let res = Command::new("leadhy")
|
let res = Command::new("leadhy")
|
||||||
.allow_hyphen_values(true)
|
.arg(Arg::new("some").allow_hyphen_values(true))
|
||||||
.arg(Arg::new("some"))
|
|
||||||
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
|
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
|
||||||
.try_get_matches_from(vec!["", "-bar", "-o"]);
|
.try_get_matches_from(vec!["", "-bar", "-o"]);
|
||||||
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
|
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
|
||||||
|
@ -449,8 +448,7 @@ fn leading_hyphen_short() {
|
||||||
#[test]
|
#[test]
|
||||||
fn leading_hyphen_long() {
|
fn leading_hyphen_long() {
|
||||||
let res = Command::new("leadhy")
|
let res = Command::new("leadhy")
|
||||||
.allow_hyphen_values(true)
|
.arg(Arg::new("some").allow_hyphen_values(true))
|
||||||
.arg(Arg::new("some"))
|
|
||||||
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
|
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
|
||||||
.try_get_matches_from(vec!["", "--bar", "-o"]);
|
.try_get_matches_from(vec!["", "--bar", "-o"]);
|
||||||
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
|
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
|
||||||
|
@ -470,8 +468,12 @@ fn leading_hyphen_long() {
|
||||||
#[test]
|
#[test]
|
||||||
fn leading_hyphen_opt() {
|
fn leading_hyphen_opt() {
|
||||||
let res = Command::new("leadhy")
|
let res = Command::new("leadhy")
|
||||||
.allow_hyphen_values(true)
|
.arg(
|
||||||
.arg(Arg::new("some").action(ArgAction::Set).long("opt"))
|
Arg::new("some")
|
||||||
|
.action(ArgAction::Set)
|
||||||
|
.long("opt")
|
||||||
|
.allow_hyphen_values(true),
|
||||||
|
)
|
||||||
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
|
.arg(Arg::new("other").short('o').action(ArgAction::SetTrue))
|
||||||
.try_get_matches_from(vec!["", "--opt", "--bar", "-o"]);
|
.try_get_matches_from(vec!["", "--opt", "--bar", "-o"]);
|
||||||
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind());
|
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);
|
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]
|
#[test]
|
||||||
fn issue_1066_allow_leading_hyphen_and_unknown_args_option() {
|
fn issue_1066_allow_leading_hyphen_and_unknown_args_option() {
|
||||||
let res = Command::new("prog")
|
let res = Command::new("prog")
|
||||||
.allow_hyphen_values(true)
|
.arg(arg!(--"some-argument" <val>).allow_hyphen_values(true))
|
||||||
.arg(arg!(--"some-argument" <val>))
|
|
||||||
.try_get_matches_from(vec!["prog", "-fish"]);
|
.try_get_matches_from(vec!["prog", "-fish"]);
|
||||||
|
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
|
|
@ -18,12 +18,12 @@ fn only_pos_follow() {
|
||||||
#[test]
|
#[test]
|
||||||
fn issue_946() {
|
fn issue_946() {
|
||||||
let r = Command::new("compiletest")
|
let r = Command::new("compiletest")
|
||||||
.allow_hyphen_values(true)
|
|
||||||
.arg(arg!(--exact "filters match exactly").action(ArgAction::SetTrue))
|
.arg(arg!(--exact "filters match exactly").action(ArgAction::SetTrue))
|
||||||
.arg(
|
.arg(
|
||||||
clap::Arg::new("filter")
|
clap::Arg::new("filter")
|
||||||
.index(1)
|
.index(1)
|
||||||
.action(ArgAction::Set)
|
.action(ArgAction::Set)
|
||||||
|
.allow_hyphen_values(true)
|
||||||
.help("filters to apply to output"),
|
.help("filters to apply to output"),
|
||||||
)
|
)
|
||||||
.try_get_matches_from(vec!["compiletest", "--exact"]);
|
.try_get_matches_from(vec!["compiletest", "--exact"]);
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub const DISPLAY_ORDER: usize = 2;
|
||||||
|
|
||||||
// Check if the global settings compile
|
// Check if the global settings compile
|
||||||
#[derive(Parser, Debug, PartialEq, Eq)]
|
#[derive(Parser, Debug, PartialEq, Eq)]
|
||||||
#[command(allow_hyphen_values = true)]
|
#[command(group = clap::ArgGroup::new("foo"))]
|
||||||
struct Opt {
|
struct Opt {
|
||||||
#[arg(
|
#[arg(
|
||||||
long = "x",
|
long = "x",
|
||||||
|
|
Loading…
Reference in a new issue