Repository: clap-rs/clap (language: rust)

Issue:
Is `Arg::validator_regex` even needed anymore?
It was proposed for addition in #1968.  That included a workaround but the motivating reason for baking it in was yaml support but that was deprecated in #3087.

With #3732, we are getting support for users to define reusable parsers / validators, rather than having every kind baked in.  Users can provide their own validators or a `clap_regex` crate could be created to provide a general one.

The runtime costs of regex support are only paid for by those enabling the feature.  However, there are usability challenges with clap due to how large the API is.  If we could cut the `Arg::validator_regex`, `regex` feature, and `RegexSet`, that will help focus the clap API.  On top of that, we are deprecating the other `validator*` functions and this would stand out.


Discussion / hints:
I had considered adding a `RegexValueParser` but I wasn't quite happy with the generic error message and the API and realized it could just as well be supported outside of clap as inside and it would reduce the API burden of clap.  This led me down this path.

Fix patch:
diff --git a/src/builder/arg.rs b/src/builder/arg.rs
index 4ee03e118a5..49cb8a96819 100644
--- a/src/builder/arg.rs
+++ b/src/builder/arg.rs
@@ -1586,57 +1586,11 @@ impl<'help> Arg<'help> {
         self
     }
 
-    /// Validates the argument via the given regular expression.
-    ///
-    /// As regular expressions are not very user friendly, the additional `err_message` should
-    /// describe the expected format in clear words. All notes for [`Arg::validator()`] regarding the
-    /// error message and performance also hold for `validator_regex`.
-    ///
-    /// The regular expression can either be borrowed or moved into `validator_regex`. This happens
-    /// automatically via [`RegexRef`]'s `Into` implementation.
-    ///
-    /// # Performance
-    /// Regular expressions are expensive to compile. You should prefer sharing your regular expression.
-    /// We use a [`Cow`]-like internal structure to enable both sharing as well as taking ownership of a
-    /// provided regular expression.
-    ///
-    /// # Examples
-    ///
-    /// You can use the classical `"\d+"` regular expression to match digits only:
-    ///
-    /// ```rust
-    /// # use clap::{Command, Arg};
-    /// use regex::Regex;
-    ///
-    /// let digits = Regex::new(r"\d+").unwrap();
-    ///
-    /// let res = Command::new("prog")
-    ///     .arg(Arg::new("digits")
-    ///         .validator_regex(&digits, "only digits are allowed"))
-    ///     .try_get_matches_from(vec![
-    ///         "prog", "12345"
-    ///     ]);
-    /// assert!(res.is_ok());
-    /// assert_eq!(res.unwrap().value_of("digits"), Some("12345"));
-    /// ```
-    ///
-    /// However, any valid `Regex` can be used:
-    ///
-    /// ```rust
-    /// # use clap::{Command, Arg, ErrorKind};
-    /// use regex::Regex;
-    ///
-    /// let priority = Regex::new(r"[A-C]").unwrap();
-    ///
-    /// let res = Command::new("prog")
-    ///     .arg(Arg::new("priority")
-    ///         .validator_regex(priority, "only priorities A, B or C are allowed"))
-    ///     .try_get_matches_from(vec![
-    ///         "prog", "12345"
-    ///     ]);
-    /// assert!(res.is_err());
-    /// assert_eq!(res.err().unwrap().kind(), ErrorKind::ValueValidation)
-    /// ```
+    /// Deprecated in [Issue #3743](https://github.com/clap-rs/clap/issues/3743), replaced with [`Arg::value_parser(...)`]
+    #[deprecated(
+        since = "3.2.0",
+        note = "Deprecated in Issue #3743; eplaced with `Arg::value_parser(...)`"
+    )]
     #[cfg(feature = "regex")]
     #[must_use]
     pub fn validator_regex(


Test patch:
diff --git a/tests/builder/regex.rs b/tests/builder/regex.rs
index 004d049f8dd..ec925d8194e 100644
--- a/tests/builder/regex.rs
+++ b/tests/builder/regex.rs
@@ -1,4 +1,5 @@
 #![cfg(feature = "regex")]
+#![allow(deprecated)]
 
 use clap::{error::ErrorKind, Arg, Command};
 use regex::{Regex, RegexSet};