You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
pubenumThingNum{Zero,One,Two,}
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)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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