Skip to content

Commit

Permalink
feat: Create ValueParser from Vec<PossibleValue>
Browse files Browse the repository at this point in the history
This dynamically generated list of possible values.  Inspired by #4504
  • Loading branch information
epage committed Nov 24, 2022
1 parent 3bccfce commit 94aca92
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/builder/value_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,41 @@ where
}
}

/// Create a [`ValueParser`] with [`PossibleValuesParser`]
///
/// See [`PossibleValuesParser`] for more flexibility in creating the
/// [`PossibleValue`][crate::builder::PossibleValue]s.
///
/// # Examples
///
/// ```rust
/// let possible = vec!["always", "auto", "never"];
/// let mut cmd = clap::Command::new("raw")
/// .arg(
/// clap::Arg::new("color")
/// .long("color")
/// .value_parser(possible)
/// .default_value("auto")
/// );
///
/// let m = cmd.try_get_matches_from_mut(
/// ["cmd", "--color", "never"]
/// ).unwrap();
///
/// let color: &String = m.get_one("color")
/// .expect("default");
/// assert_eq!(color, "never");
/// ```
impl<P> From<Vec<P>> for ValueParser
where
P: Into<super::PossibleValue>,
{
fn from(values: Vec<P>) -> Self {
let inner = PossibleValuesParser::from(values);
Self::from(inner)
}
}

impl std::fmt::Debug for ValueParser {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match &self.0 {
Expand Down

0 comments on commit 94aca92

Please sign in to comment.