Replies: 1 comment
-
Sorry for the delay #[derive(ValueEnum)]
pub enum ThingNum {
#[value(name = "0")]
Zero,
#[value(name = "1")]
One,
#[value(name = "2")]
Two,
} Should work. See https://docs.rs/clap/latest/clap/_derive/index.html#possible-value-attributes You can also use |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Oftentimes when we want input from the user in the form of a number we may only want to accept a subset of all possible values.
For example only integers from 0-2.
Now we can already sort of do this in Clap via this:
This will reject the user if they attempt to put in an integer that is not from 0-2 but Rust's type system does not know this. So for this match statement I need to then add an unsatisfying unreachable!() macro.
and if I need to match against the value multiple times this can quickly get tedious...
One alternative is to instead create a specialized enum that ONLY allows for a value between 0-2 to occur such as:
but then if I try to get clap to parse this enum it will default to the values being [zero, one, two] rather than the integer values of [0,1,2]. So clap would force the user to type in "two" rather than "2".
Is there any (hopefully concise?) way to allow the user to enter a decimal but then immediately parse the value into an enum?
(so the cli struct can end up looking something like below)
Beta Was this translation helpful? Give feedback.
All reactions