use clap::{Parser, ValueEnum};
#[derive(ValueEnum)]
enum Output {
X,
Y,
Z,
...
}
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(long, num_args = 0.., value_enum, value_delimiter = ',', default_values_t = [Output::X])]
outputs: Vec<Output>,
}Via specified I'm also looking for a way to pass vector of full possible values of as vector, so instead of writing |
Replies: 3 comments 1 reply
|
Current workaround is to add new variant if value == Output::X or value == Output::All {
...
} else if value == Output::Y or value == Output::All {
...
} else if value == Output::Z or value == Output::All {
...
}recommended way is to add an option like |
|
I would hesitate to add support for |
|
Ended up with this: (hint: to avoid using use clap::Parser;
use enum_map::{Enum, EnumMap};
#[derive(Enum, Clone)]
enum Output {
X,
Y,
Z,
...
}
pub const OUTPUT_MAP: EnumMap<Output, &'static str> = EnumMap::from_array([
/* Output::X=> */ "x",
/* Output::Y=> */ "y",
/* Output::Z => */ "z",
...
]);
fn parse_outputs(value: &str) -> Result<Vec<Output>, String> {
if value == "all" {
Ok(OUTPUT_MAP.iter().map(|(o, _)| o).collect())
} else {
OUTPUT_MAP
.iter()
.find_map(|(o, s)| (&value == s).then_some(vec![o]))
.ok_or_else(|| "invalid variant".into())
}
}
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(long, num_args = 0.., value_parser = parse_outputs, value_delimiter = ',', default_values = ["x"])]
outputs: Vec<Vec<Output>>,
}
#[tokio::main]
async fn main() {
let args = Args::parse();
let outputs: Vec<Output> = args
.outputs
.iter()
.flat_map(|x| x.iter())
.map(|o| o.clone().into())
.collect();for clap side, value_parser of args with type of |
Ended up with this: (hint: to avoid using
eunm_mapyou can implToStringand use VARIANTS const)