-
-
Notifications
You must be signed in to change notification settings - Fork 682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using typer.prompt with choices from Enum doesn't display choices #472
Comments
Managed to figure out that I needed to add |
Managed to find a workaround for this by not using an enum and instead use the
Still think that the |
I solved the issue by creating a new class IntEnumChoice(click.Choice):
def __init__(self, enum_type: EnumType, case_sensitive: bool = True) -> None:
choices = [f"{value.value}: {value.name}" for value in enum_type]
super().__init__(choices, case_sensitive)
self._enum_type = enum_type
def convert(self, value: t.Any, param: t.Optional["Parameter"], ctx: t.Optional["Context"]) -> t.Any:
try:
return self._enum_type(int(value))
except ValueError:
choices_str = ", ".join(map(repr, self.choices))
self.fail(
ngettext(
"{value!r} is not {choice}.",
"{value!r} is not one of {choices}.",
len(self.choices),
).format(value=value, choice=choices_str, choices=choices_str),
param,
ctx,
) You can easily adapt it for string |
I'm looking for a parser.add_argument('move', choices=['rock', 'paper', 'scissors']) It would be great to have a similar feature in the core of |
First Check
Commit to Help
Example Code
Description
Using the
typer.prompt
function isn't displaying the choices of an enum the same way that an Option does.Am I doing something wrong here? Is there some other way I should be doing the prompt in order for it to display the choices as is done with the argument choices?
Also, why are the return values different (the prompt gives me the string "opt3", while the argument returns the enum value itself).
Operating System
macOS
Operating System Details
No response
Typer Version
0.6.1
Python Version
Python 3.10.8
Additional Context
No response
The text was updated successfully, but these errors were encountered: