fix: Rename App's AllowLeadingHyphen to AllowHyphenValues

This aligns us with `Arg::allow_hyphen_values` in clap2.
This commit is contained in:
Ed Page 2021-11-24 10:28:06 -06:00
parent 6a580d4de2
commit c15a0bf5b3
6 changed files with 30 additions and 21 deletions

View file

@ -19,7 +19,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)]
#[clap(global_setting = AppSettings::AllowLeadingHyphen)] #[clap(global_setting = AppSettings::AllowHyphenValues)]
struct Opt { struct Opt {
#[clap( #[clap(
long = "x", long = "x",

View file

@ -77,6 +77,8 @@ impl_settings! { AppSettings, AppFlags,
=> Flags::NO_OP, => Flags::NO_OP,
AllowInvalidUtf8ForExternalSubcommands("allowinvalidutf8forexternalsubcommands") AllowInvalidUtf8ForExternalSubcommands("allowinvalidutf8forexternalsubcommands")
=> Flags::SC_UTF8_NONE, => Flags::SC_UTF8_NONE,
AllowHyphenValues("allowhyphenvalues")
=> Flags::LEADING_HYPHEN,
AllowLeadingHyphen("allowleadinghyphen") AllowLeadingHyphen("allowleadinghyphen")
=> Flags::LEADING_HYPHEN, => Flags::LEADING_HYPHEN,
AllowNegativeNumbers("allownegativenumbers") AllowNegativeNumbers("allownegativenumbers")
@ -225,7 +227,7 @@ pub enum AppSettings {
/// # use clap::{Arg, App, AppSettings}; /// # use clap::{Arg, App, AppSettings};
/// // Imagine you needed to represent negative numbers as well, such as -10 /// // Imagine you needed to represent negative numbers as well, such as -10
/// let m = App::new("nums") /// let m = App::new("nums")
/// .setting(AppSettings::AllowLeadingHyphen) /// .setting(AppSettings::AllowHyphenValues)
/// .arg(Arg::new("neg")) /// .arg(Arg::new("neg"))
/// .get_matches_from(vec![ /// .get_matches_from(vec![
/// "nums", "-20" /// "nums", "-20"
@ -235,6 +237,13 @@ pub enum AppSettings {
/// # ; /// # ;
/// ``` /// ```
/// [`Arg::allow_hyphen_values`]: crate::Arg::allow_hyphen_values() /// [`Arg::allow_hyphen_values`]: crate::Arg::allow_hyphen_values()
AllowHyphenValues,
/// Deprecated, replaced with [`AppSettings::AllowHyphenValues`]
#[deprecated(
since = "3.0.0",
note = "Replaced with `AppSettings::AllowHyphenValues`"
)]
AllowLeadingHyphen, AllowLeadingHyphen,
/// Specifies that all arguments override themselves. This is the equivalent to saying the `foo` /// Specifies that all arguments override themselves. This is the equivalent to saying the `foo`
@ -244,7 +253,7 @@ pub enum AppSettings {
AllArgsOverrideSelf, AllArgsOverrideSelf,
/// Allows negative numbers to pass as values. This is similar to /// Allows negative numbers to pass as values. This is similar to
/// [`AppSettings::AllowLeadingHyphen`] except that it only allows numbers, all /// [`AppSettings::AllowHyphenValues`] except that it only allows numbers, all
/// other undefined leading hyphens will fail to parse. /// other undefined leading hyphens will fail to parse.
/// ///
/// # Examples /// # Examples
@ -1146,8 +1155,8 @@ mod test {
AppSettings::AllowInvalidUtf8ForExternalSubcommands AppSettings::AllowInvalidUtf8ForExternalSubcommands
); );
assert_eq!( assert_eq!(
"allowleadinghyphen".parse::<AppSettings>().unwrap(), "allowhyphenvalues".parse::<AppSettings>().unwrap(),
AppSettings::AllowLeadingHyphen AppSettings::AllowHyphenValues
); );
assert_eq!( assert_eq!(
"allownegativenumbers".parse::<AppSettings>().unwrap(), "allownegativenumbers".parse::<AppSettings>().unwrap(),

View file

@ -13,7 +13,7 @@ fn propagate_version() {
#[test] #[test]
fn global_setting() { fn global_setting() {
let mut app = App::new("test") let mut app = App::new("test")
.global_setting(AppSettings::AllowLeadingHyphen) .global_setting(AppSettings::AllowHyphenValues)
.subcommand(App::new("subcmd")); .subcommand(App::new("subcmd"));
app._propagate(); app._propagate();
assert!(app assert!(app
@ -21,13 +21,13 @@ fn global_setting() {
.iter() .iter()
.find(|s| s.name == "subcmd") .find(|s| s.name == "subcmd")
.unwrap() .unwrap()
.is_set(AppSettings::AllowLeadingHyphen)); .is_set(AppSettings::AllowHyphenValues));
} }
#[test] #[test]
fn global_settings() { fn global_settings() {
let mut app = App::new("test") let mut app = App::new("test")
.global_setting(AppSettings::AllowLeadingHyphen) .global_setting(AppSettings::AllowHyphenValues)
.global_setting(AppSettings::TrailingVarArg) .global_setting(AppSettings::TrailingVarArg)
.subcommand(App::new("subcmd")); .subcommand(App::new("subcmd"));
app._propagate(); app._propagate();
@ -36,7 +36,7 @@ fn global_settings() {
.iter() .iter()
.find(|s| s.name == "subcmd") .find(|s| s.name == "subcmd")
.unwrap() .unwrap()
.is_set(AppSettings::AllowLeadingHyphen)); .is_set(AppSettings::AllowHyphenValues));
assert!(app assert!(app
.subcommands .subcommands
.iter() .iter()

View file

@ -547,7 +547,7 @@ impl<'help, 'app> Parser<'help, 'app> {
} else if let Some(short_arg) = arg_os.strip_prefix("-") { } else if let Some(short_arg) = arg_os.strip_prefix("-") {
// Arg looks like a short flag, and not a possible number // Arg looks like a short flag, and not a possible number
// Try to parse short args like normal, if AllowLeadingHyphen or // Try to parse short args like normal, if AllowHyphenValues or
// AllowNegativeNumbers is set, parse_short_arg will *not* throw // AllowNegativeNumbers is set, parse_short_arg will *not* throw
// an error, and instead return Ok(None) // an error, and instead return Ok(None)
let parse_result = self.parse_short_arg( let parse_result = self.parse_short_arg(
@ -949,7 +949,7 @@ impl<'help, 'app> Parser<'help, 'app> {
next, current_positional.name next, current_positional.name
); );
if self.is_set(AS::AllowLeadingHyphen) if self.is_set(AS::AllowHyphenValues)
|| self.app[&current_positional.id].is_set(ArgSettings::AllowHyphenValues) || self.app[&current_positional.id].is_set(ArgSettings::AllowHyphenValues)
|| (self.is_set(AS::AllowNegativeNumbers) && next.to_str_lossy().parse::<f64>().is_ok()) || (self.is_set(AS::AllowNegativeNumbers) && next.to_str_lossy().parse::<f64>().is_ok())
{ {
@ -1235,7 +1235,7 @@ impl<'help, 'app> Parser<'help, 'app> {
} }
} else if let Some(sc_name) = self.possible_long_flag_subcommand(arg) { } else if let Some(sc_name) = self.possible_long_flag_subcommand(arg) {
ParseResult::FlagSubCommand(sc_name.to_string()) ParseResult::FlagSubCommand(sc_name.to_string())
} else if self.is_set(AS::AllowLeadingHyphen) { } else if self.is_set(AS::AllowHyphenValues) {
ParseResult::MaybeHyphenValue ParseResult::MaybeHyphenValue
} else { } else {
ParseResult::NoMatchingArg { ParseResult::NoMatchingArg {
@ -1258,7 +1258,7 @@ impl<'help, 'app> Parser<'help, 'app> {
let arg = short_arg.to_str_lossy(); let arg = short_arg.to_str_lossy();
if (self.is_set(AS::AllowNegativeNumbers) && arg.parse::<f64>().is_ok()) if (self.is_set(AS::AllowNegativeNumbers) && arg.parse::<f64>().is_ok())
|| (self.is_set(AS::AllowLeadingHyphen) || (self.is_set(AS::AllowHyphenValues)
&& arg.chars().any(|c| !self.app.contains_short(c))) && arg.chars().any(|c| !self.app.contains_short(c)))
|| matches!(parse_state, ParseState::Opt(opt) | ParseState::Pos(opt) || matches!(parse_state, ParseState::Opt(opt) | ParseState::Pos(opt)
if self.app[opt].is_set(ArgSettings::AllowHyphenValues)) if self.app[opt].is_set(ArgSettings::AllowHyphenValues))

View file

@ -539,7 +539,7 @@ fn delim_values_trailingvararg_with_delim() {
#[test] #[test]
fn leading_hyphen_short() { fn leading_hyphen_short() {
let res = App::new("leadhy") let res = App::new("leadhy")
.setting(AppSettings::AllowLeadingHyphen) .setting(AppSettings::AllowHyphenValues)
.arg(Arg::new("some")) .arg(Arg::new("some"))
.arg(Arg::new("other").short('o')) .arg(Arg::new("other").short('o'))
.try_get_matches_from(vec!["", "-bar", "-o"]); .try_get_matches_from(vec!["", "-bar", "-o"]);
@ -553,7 +553,7 @@ fn leading_hyphen_short() {
#[test] #[test]
fn leading_hyphen_long() { fn leading_hyphen_long() {
let res = App::new("leadhy") let res = App::new("leadhy")
.setting(AppSettings::AllowLeadingHyphen) .setting(AppSettings::AllowHyphenValues)
.arg(Arg::new("some")) .arg(Arg::new("some"))
.arg(Arg::new("other").short('o')) .arg(Arg::new("other").short('o'))
.try_get_matches_from(vec!["", "--bar", "-o"]); .try_get_matches_from(vec!["", "--bar", "-o"]);
@ -567,7 +567,7 @@ fn leading_hyphen_long() {
#[test] #[test]
fn leading_hyphen_opt() { fn leading_hyphen_opt() {
let res = App::new("leadhy") let res = App::new("leadhy")
.setting(AppSettings::AllowLeadingHyphen) .setting(AppSettings::AllowHyphenValues)
.arg(Arg::new("some").takes_value(true).long("opt")) .arg(Arg::new("some").takes_value(true).long("opt"))
.arg(Arg::new("other").short('o')) .arg(Arg::new("other").short('o'))
.try_get_matches_from(vec!["", "--opt", "--bar", "-o"]); .try_get_matches_from(vec!["", "--opt", "--bar", "-o"]);
@ -606,7 +606,7 @@ fn allow_negative_numbers_fail() {
fn leading_double_hyphen_trailingvararg() { fn leading_double_hyphen_trailingvararg() {
let m = App::new("positional") let m = App::new("positional")
.setting(AppSettings::TrailingVarArg) .setting(AppSettings::TrailingVarArg)
.setting(AppSettings::AllowLeadingHyphen) .setting(AppSettings::AllowHyphenValues)
.arg(arg!([opt] ... "some pos")) .arg(arg!([opt] ... "some pos"))
.get_matches_from(vec!["", "--foo", "-Wl", "bar"]); .get_matches_from(vec!["", "--foo", "-Wl", "bar"]);
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -823,7 +823,7 @@ fn missing_positional_hyphen_req_error() {
#[test] #[test]
fn issue_1066_allow_leading_hyphen_and_unknown_args() { fn issue_1066_allow_leading_hyphen_and_unknown_args() {
let res = App::new("prog") let res = App::new("prog")
.global_setting(AppSettings::AllowLeadingHyphen) .global_setting(AppSettings::AllowHyphenValues)
.arg(arg!(--"some-argument")) .arg(arg!(--"some-argument"))
.try_get_matches_from(vec!["prog", "hello"]); .try_get_matches_from(vec!["prog", "hello"]);
@ -834,7 +834,7 @@ fn issue_1066_allow_leading_hyphen_and_unknown_args() {
#[test] #[test]
fn issue_1066_allow_leading_hyphen_and_unknown_args_no_vals() { fn issue_1066_allow_leading_hyphen_and_unknown_args_no_vals() {
let res = App::new("prog") let res = App::new("prog")
.global_setting(AppSettings::AllowLeadingHyphen) .global_setting(AppSettings::AllowHyphenValues)
.arg(arg!(--"some-argument")) .arg(arg!(--"some-argument"))
.try_get_matches_from(vec!["prog", "--hello"]); .try_get_matches_from(vec!["prog", "--hello"]);
@ -845,7 +845,7 @@ fn issue_1066_allow_leading_hyphen_and_unknown_args_no_vals() {
#[test] #[test]
fn issue_1066_allow_leading_hyphen_and_unknown_args_option() { fn issue_1066_allow_leading_hyphen_and_unknown_args_option() {
let res = App::new("prog") let res = App::new("prog")
.global_setting(AppSettings::AllowLeadingHyphen) .global_setting(AppSettings::AllowHyphenValues)
.arg(arg!(--"some-argument" <val>)) .arg(arg!(--"some-argument" <val>))
.try_get_matches_from(vec!["prog", "-hello"]); .try_get_matches_from(vec!["prog", "-hello"]);

View file

@ -15,7 +15,7 @@ fn only_pos_follow() {
#[test] #[test]
fn issue_946() { fn issue_946() {
let r = App::new("compiletest") let r = App::new("compiletest")
.setting(clap::AppSettings::AllowLeadingHyphen) .setting(clap::AppSettings::AllowHyphenValues)
.arg(arg!(--exact "filters match exactly")) .arg(arg!(--exact "filters match exactly"))
.arg( .arg(
clap::Arg::new("filter") clap::Arg::new("filter")