Possible values from Vec of strings #5516
Replies: 2 comments 1 reply
-
For the first, because of https://docs.rs/clap/latest/clap/builder/struct.ValueParser.html#impl-From%3CVec%3CP%3E%3E-for-ValueParser you should be able to do #[arg(value_parser = possible_values()]
arg: String, If not, you can construct https://docs.rs/clap/latest/clap/builder/struct.PossibleValuesParser.html#impl-From%3CI%3E-for-PossibleValuesParser directly The same is true for string literals. If you want to adapt those string literals into another type, you can use https://docs.rs/clap/latest/clap/builder/trait.TypedValueParser.html#method.map or https://docs.rs/clap/latest/clap/builder/struct.EnumValueParser.html |
Beta Was this translation helpful? Give feedback.
-
I had to add struct Args {
/// Language
#[arg(short, long, value_parser = get_possible_languages())]
language: Option<String>
}
fn get_possible_languages() -> Vec<String> { ... } The only issue with that is that Regarding string literals, not sure how to use it. I imagine something like that struct Args {
/// Format
#[arg(short, long, default_value = "srt", value_parser = &["srt", "vtt", "txt"])]
// TODO: use possible values. confusing crate!
format: String
} |
Beta Was this translation helpful? Give feedback.
-
I'm using clap with derive macros,
and I would like to have an string argument with possible string values returned from a function.
The use cases are:
Beta Was this translation helpful? Give feedback.
All reactions