Repository: clap-rs/clap
Pull request #3815 (2022-06-11 00:22:38)

fix:default_value_ifs_os modified arg name, it replaces arg with arg.key(),but it call self.default_value_if_os
<!--
Thanks for helping out!

Please link the appropriate issue from your PR.

If you don't have an issue, we'd recommend starting with one first so the PR can focus on the
implementation (unless its an obvious bug or documentation fix that will have
little conversation).
-->



Patch:
diff --git a/src/builder/arg.rs b/src/builder/arg.rs
index 0cbd5af2..2d7f1c01 100644
--- a/src/builder/arg.rs
+++ b/src/builder/arg.rs
@@ -3307,7 +3307,7 @@ impl<'help> Arg<'help> {
         ifs: &[(T, Option<&'help OsStr>, Option<&'help OsStr>)],
     ) -> Self {
         for (arg, val, default) in ifs {
-            self = self.default_value_if_os(arg.key(), *val, *default);
+            self = self.default_value_if_os(arg, *val, *default);
         }
         self
     }
diff --git a/src/parser/matches/arg_matches.rs b/src/parser/matches/arg_matches.rs
index f2cd14d4..8dfdd9dd 100644
--- a/src/parser/matches/arg_matches.rs
+++ b/src/parser/matches/arg_matches.rs
@@ -521,11 +521,11 @@ impl ArgMatches {
         self.values_of_t(name).unwrap_or_else(|e| e.exit())
     }
 
-    /// Deprecated, replaced with [`ArgMatches::contains_id`] or
-    /// [`ArgAction::SetTrue`][crate::ArgAction].
+    /// Deprecated, replaced with [`ArgAction::SetTrue`][crate::ArgAction] or
+    /// [`ArgMatches::contains_id`].
     #[deprecated(
         since = "3.2.0",
-        note = "Replaced with either `ArgMatches::contains_id(...)` or `ArgAction::SetTrue`"
+        note = "Replaced with either `ArgAction::SetTrue` or `ArgMatches::contains_id(...)`"
     )]
     pub fn is_present<T: Key>(&self, id: T) -> bool {
         let id = Id::from(id);
@@ -565,11 +565,11 @@ impl ArgMatches {
         value.and_then(MatchedArg::source)
     }
 
-    /// Deprecated, replaced with [`ArgMatches::get_many`]`.len()` or
-    /// [`ArgAction::Count`][crate::ArgAction].
+    /// Deprecated, replaced with  [`ArgAction::Count`][crate::ArgAction] or
+    /// [`ArgMatches::get_many`]`.len()`.
     #[deprecated(
         since = "3.2.0",
-        note = "Replaced with either `ArgMatches::get_many(...).len()` or `ArgAction::Count`"
+        note = "Replaced with either `ArgAction::Count` or `ArgMatches::get_many(...).len()`"
     )]
     pub fn occurrences_of<T: Key>(&self, id: T) -> u64 {
         #![allow(deprecated)]


Test patch:
diff --git a/tests/builder/arg_settings.rs b/tests/builder/arg_settings.rs
index d4e900ab..71972160 100644
--- a/tests/builder/arg_settings.rs
+++ b/tests/builder/arg_settings.rs
@@ -1,5 +1,7 @@
 #![allow(deprecated)]
-use clap::{Arg, ArgSettings};
+
+use clap::{Arg, ArgSettings, Command};
+use std::ffi::OsStr;
 
 #[test]
 fn setting() {
@@ -42,3 +44,42 @@ fn unset_setting_bitor() {
     assert!(!m.is_hide_set(), "{:#?}", m);
     assert!(!m.is_last_set(), "{:#?}", m);
 }
+
+#[test]
+fn default_value_ifs_os() {
+    let cmd = Command::new("my_cargo")
+        .arg(
+            Arg::new("flag")
+                .long("flag")
+                .allow_invalid_utf8(true)
+                .takes_value(true),
+        )
+        .arg(
+            Arg::new("other")
+                .long("other")
+                .allow_invalid_utf8(true)
+                .default_value_ifs_os(&[(
+                    "flag",
+                    Some("标记2").map(OsStr::new),
+                    Some("flag=标记2").map(OsStr::new),
+                )]),
+        );
+    let result = cmd.try_get_matches_from([
+        OsStr::new("my_cargo"),
+        OsStr::new("--flag"),
+        OsStr::new("标记2"),
+    ]);
+    assert!(result.is_ok());
+    match result {
+        Ok(arg_matches) => {
+            assert_eq!(arg_matches.value_of_os("flag"), Some(OsStr::new("标记2")));
+            assert_eq!(
+                arg_matches.value_of_os("other"),
+                Some(OsStr::new("flag=标记2")),
+            );
+        }
+        Err(e) => {
+            println!("{}", e.to_string());
+        }
+    }
+}
diff --git a/tests/derive_ui/next/bool_value_enum.stderr b/tests/derive_ui/next/bool_value_enum.stderr
index 3cf48c81..f85ea116 100644
--- a/tests/derive_ui/next/bool_value_enum.stderr
+++ b/tests/derive_ui/next/bool_value_enum.stderr
@@ -35,7 +35,7 @@ help: `bool` is a unit variant, you need to write it without the parenthesis
 7 |     opts: bool,
   |           ~~~~
 
-warning: use of deprecated associated function `clap::ArgMatches::is_present`: Replaced with either `ArgMatches::contains_id(...)` or `ArgAction::SetTrue`
+warning: use of deprecated associated function `clap::ArgMatches::is_present`: Replaced with either `ArgAction::SetTrue` or `ArgMatches::contains_id(...)`
  --> tests/derive_ui/next/bool_value_enum.rs:7:11
   |
 7 |     opts: bool,
diff --git a/tests/derive_ui/non_existent_attr.stderr b/tests/derive_ui/non_existent_attr.stderr
index 1bd9b5f6..c970b909 100644
--- a/tests/derive_ui/non_existent_attr.stderr
+++ b/tests/derive_ui/non_existent_attr.stderr
@@ -1,4 +1,4 @@
-warning: use of deprecated associated function `clap::ArgMatches::is_present`: Replaced with either `ArgMatches::contains_id(...)` or `ArgAction::SetTrue`
+warning: use of deprecated associated function `clap::ArgMatches::is_present`: Replaced with either `ArgAction::SetTrue` or `ArgMatches::contains_id(...)`
   --> tests/derive_ui/non_existent_attr.rs:15:12
    |
 15 |     debug: bool,
@@ -6,7 +6,7 @@ warning: use of deprecated associated function `clap::ArgMatches::is_present`: R
    |
    = note: `#[warn(deprecated)]` on by default
 
-warning: use of deprecated associated function `clap::ArgMatches::is_present`: Replaced with either `ArgMatches::contains_id(...)` or `ArgAction::SetTrue`
+warning: use of deprecated associated function `clap::ArgMatches::is_present`: Replaced with either `ArgAction::SetTrue` or `ArgMatches::contains_id(...)`
   --> tests/derive_ui/non_existent_attr.rs:15:12
    |
 15 |     debug: bool,
diff --git a/tests/derive_ui/stable/bool_value_enum.stderr b/tests/derive_ui/stable/bool_value_enum.stderr
index e6136dbb..de052c39 100644
--- a/tests/derive_ui/stable/bool_value_enum.stderr
+++ b/tests/derive_ui/stable/bool_value_enum.stderr
@@ -35,7 +35,7 @@ help: `bool` is a unit variant, you need to write it without the parenthesis
 7 |     opts: bool,
   |           ~~~~
 
-warning: use of deprecated associated function `clap::ArgMatches::is_present`: Replaced with either `ArgMatches::contains_id(...)` or `ArgAction::SetTrue`
+warning: use of deprecated associated function `clap::ArgMatches::is_present`: Replaced with either `ArgAction::SetTrue` or `ArgMatches::contains_id(...)`
  --> tests/derive_ui/stable/bool_value_enum.rs:7:11
   |
 7 |     opts: bool,