我想StructOpt
使用枚举,这样每次用户通过-d sunday
时,它都会被解析为Day::Sunday
:
#[macro_use]
extern crate structopt;
use std::path::PathBuf;
use structopt::StructOpt;
// My enum
enum Day {
Sunday, Monday
}
#[derive(Debug, StructOpt)]
#[structopt(name = "example", about = "An example of StructOpt usage.")]
struct Opt {
/// Set speed
#[structopt(short = "s", long = "speed", default_value = "42")]
speed: f64,
/// Input file
#[structopt(parse(from_os_str))]
input: PathBuf,
/// Day of the week
#[structopt(short = "d", long = "day", default_value = Day::Monday)]
day: Day,
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
我目前最好的解决方案是Option<String>
用作类型并传递自定义parse_day()
:
fn parse_day(day: &str) -> Result<Day, ParseError> {
match day {
"sunday" => Ok(Day::Sunday),
_ => Ok(Day::Monday)
}
Err("Could not parse a day")
}