Using enum argument without implementing FromStr
#2046
-
Hi! I'm building a CLI app using clap-rs and I'd like to have an enum argument for it but this requires implementing use clap::Clap;
use std::io::Error;
use std::io::ErrorKind;
use std::str::FromStr;
#[derive(Clap, Debug)]
enum LogLevel {
Error,
Info,
Verbose,
}
impl FromStr for LogLevel {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"error" => Ok(LogLevel::Error),
"info" => Ok(LogLevel::Info),
"verbose" => Ok(LogLevel::Verbose),
_ => Err(Error::new(
ErrorKind::InvalidInput,
"Invalid LogLevel string, options are 'error', 'info', 'verbose'",
)),
}
}
}
#[derive(Clap)]
#[clap(
version = "0.0.1",
author = "Kirill Bobyrev <[email protected]>"
)]
struct Opts {
/// Logging level.
#[clap(long, possible_values(&["error", "info", "verbose"]))]
log: LogLevel,
} Is there any way to avoid implementing |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This is implemented in master. Not sure if we released it though. Probably not. You can use the git dependency, I guess. |
Beta Was this translation helpful? Give feedback.
This is implemented in master. Not sure if we released it though. Probably not. You can use the git dependency, I guess.