Skip to content

Commit

Permalink
fix(derive): Deprecate Command::allow_hyphen_values
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 7, 2022
1 parent 7a2bbca commit d45e4be
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
40 changes: 13 additions & 27 deletions src/builder/command.rs
Expand Up @@ -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::<String>("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)
Expand Down Expand Up @@ -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)
}
Expand Down
39 changes: 9 additions & 30 deletions tests/builder/app_settings.rs
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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" <val>))
.arg(arg!(--"some-argument" <val>).allow_hyphen_values(true))
.try_get_matches_from(vec!["prog", "-fish"]);

assert!(res.is_err());
Expand Down
2 changes: 1 addition & 1 deletion tests/builder/positionals.rs
Expand Up @@ -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"]);
Expand Down
2 changes: 1 addition & 1 deletion tests/derive/non_literal_attributes.rs
Expand Up @@ -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",
Expand Down

0 comments on commit d45e4be

Please sign in to comment.