@@ -508,6 +508,41 @@ where
508
508
}
509
509
}
510
510
511
+ /// Create a [`ValueParser`] with [`PossibleValuesParser`]
512
+ ///
513
+ /// See [`PossibleValuesParser`] for more flexibility in creating the
514
+ /// [`PossibleValue`][crate::builder::PossibleValue]s.
515
+ ///
516
+ /// # Examples
517
+ ///
518
+ /// ```rust
519
+ /// let possible = vec!["always", "auto", "never"];
520
+ /// let mut cmd = clap::Command::new("raw")
521
+ /// .arg(
522
+ /// clap::Arg::new("color")
523
+ /// .long("color")
524
+ /// .value_parser(possible)
525
+ /// .default_value("auto")
526
+ /// );
527
+ ///
528
+ /// let m = cmd.try_get_matches_from_mut(
529
+ /// ["cmd", "--color", "never"]
530
+ /// ).unwrap();
531
+ ///
532
+ /// let color: &String = m.get_one("color")
533
+ /// .expect("default");
534
+ /// assert_eq!(color, "never");
535
+ /// ```
536
+ impl < P > From < Vec < P > > for ValueParser
537
+ where
538
+ P : Into < super :: PossibleValue > ,
539
+ {
540
+ fn from ( values : Vec < P > ) -> Self {
541
+ let inner = PossibleValuesParser :: from ( values) ;
542
+ Self :: from ( inner)
543
+ }
544
+ }
545
+
511
546
impl std:: fmt:: Debug for ValueParser {
512
547
fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> Result < ( ) , std:: fmt:: Error > {
513
548
match & self . 0 {
@@ -957,31 +992,11 @@ impl Default for PathBufValueParser {
957
992
///
958
993
/// ```rust
959
994
/// # use std::ffi::OsStr;
995
+ /// # use clap::ColorChoice;
960
996
/// # use clap::builder::TypedValueParser;
961
997
/// # let cmd = clap::Command::new("test");
962
998
/// # let arg = None;
963
999
///
964
- /// #[derive(Copy, Clone, Debug, PartialEq, Eq)]
965
- /// enum ColorChoice {
966
- /// Always,
967
- /// Auto,
968
- /// Never,
969
- /// }
970
- ///
971
- /// impl clap::ValueEnum for ColorChoice {
972
- /// fn value_variants<'a>() -> &'a [Self] {
973
- /// &[Self::Always, Self::Auto, Self::Never]
974
- /// }
975
- ///
976
- /// fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue> {
977
- /// match self {
978
- /// Self::Always => Some(clap::builder::PossibleValue::new("always")),
979
- /// Self::Auto => Some(clap::builder::PossibleValue::new("auto")),
980
- /// Self::Never => Some(clap::builder::PossibleValue::new("never")),
981
- /// }
982
- /// }
983
- /// }
984
- ///
985
1000
/// // Usage
986
1001
/// let mut cmd = clap::Command::new("raw")
987
1002
/// .arg(
@@ -1086,8 +1101,9 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> Default for EnumValueP
1086
1101
/// Verify the value is from an enumerated set of [`PossibleValue`][crate::builder::PossibleValue].
1087
1102
///
1088
1103
/// See also:
1089
- /// - [`EnumValueParser`] for directly supporting `enum`s
1090
- /// - [`TypedValueParser::map`] for adapting values to a more specialized type
1104
+ /// - [`EnumValueParser`] for directly supporting [`ValueEnum`][crate::ValueEnum] types
1105
+ /// - [`TypedValueParser::map`] for adapting values to a more specialized type, like an external
1106
+ /// enums that can't implement [`ValueEnum`][crate::ValueEnum]
1091
1107
///
1092
1108
/// # Example
1093
1109
///
@@ -2327,6 +2343,7 @@ pub mod via_prelude {
2327
2343
///
2328
2344
/// Example mappings:
2329
2345
/// ```rust
2346
+ /// # use clap::ColorChoice;
2330
2347
/// // Built-in types
2331
2348
/// let parser = clap::value_parser!(String);
2332
2349
/// assert_eq!(format!("{:?}", parser), "ValueParser::string");
@@ -2344,25 +2361,6 @@ pub mod via_prelude {
2344
2361
/// assert_eq!(format!("{:?}", parser), "_AnonymousValueParser(ValueParser::other(usize))");
2345
2362
///
2346
2363
/// // ValueEnum types
2347
- /// #[derive(Copy, Clone, Debug, PartialEq, Eq)]
2348
- /// enum ColorChoice {
2349
- /// Always,
2350
- /// Auto,
2351
- /// Never,
2352
- /// }
2353
- /// impl clap::ValueEnum for ColorChoice {
2354
- /// // ...
2355
- /// # fn value_variants<'a>() -> &'a [Self] {
2356
- /// # &[Self::Always, Self::Auto, Self::Never]
2357
- /// # }
2358
- /// # fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue> {
2359
- /// # match self {
2360
- /// # Self::Always => Some(clap::builder::PossibleValue::new("always")),
2361
- /// # Self::Auto => Some(clap::builder::PossibleValue::new("auto")),
2362
- /// # Self::Never => Some(clap::builder::PossibleValue::new("never")),
2363
- /// # }
2364
- /// # }
2365
- /// }
2366
2364
/// let parser = clap::value_parser!(ColorChoice);
2367
2365
/// assert_eq!(format!("{:?}", parser), "EnumValueParser(PhantomData)");
2368
2366
/// ```
0 commit comments