-
clap 3.2 deprecated use clap::Parser;
/// This enum is not defined in the crate doing the parsing and should not depend on clap
#[derive(Clone, Debug)]
pub enum Template {
Foo,
}
impl Template {
const NAMES: &'static [&'static str] = &["foo"];
}
impl std::str::FromStr for Template {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"foo" => Ok(Self::Foo),
_ => Err("Unrecognized"),
}
}
}
#[derive(Clone, Debug, Parser)]
pub(crate) struct Args {
#[clap(
long = "template",
default_value = "foo",
// possible_values = Template::NAMES, // this works
value_parser = clap::builder::PossibleValuesParser::new(Template::NAMES) // this fails
)]
pub(crate) template: Template,
}
#[test]
fn test() {
Args::parse_from(["app", "--template", "foo"]);
} This program fails with
What is the correct equivalent of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Yes, In another thread, I gave this reply
I would say in your case, you could derive |
Beta Was this translation helpful? Give feedback.
-
I ran into the same issue, and the solution I came up with was (assuming
Like kpreid above, my type was also in a library crate that didn't depend on clap. However, I ended up solving this by making a duplicate type in the application crate and writing a |
Beta Was this translation helpful? Give feedback.
Yes,
PossibleValuesParser
only provides aString
output type at this time.In another thread, I gave this reply
I would say in your case, you coul…