select all possible values for Vec<PossibleValues>
#6433
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: 1 comment
|
clap doesn't have a built-in "all" keyword for value enums, and you can't fake it with a The clean way is to add use clap::{Parser, ValueEnum};
#[derive(ValueEnum, Clone, Copy, PartialEq)]
enum Output { All, X, Y, Z }
#[derive(Parser)]
struct Args {
#[arg(long, value_delimiter = ',', default_value = "x")]
outputs: Vec<Output>,
}
impl Args {
fn outputs(&self) -> Vec<Output> {
if self.outputs.contains(&Output::All) {
Output::value_variants()
.iter()
.copied()
.filter(|o| *o != Output::All)
.collect()
} else {
self.outputs.clone()
}
}
}Now If you'd rather not have |
clap doesn't have a built-in "all" keyword for value enums, and you can't fake it with a
value_parsereither. A parser only ever turns one token into one value, so there's no way forallto fan out into three. You have to expand it yourself after parsing.The clean way is to add
Allas a variant and letValueEnum::value_variants()give you the rest: