enumchoice package¶
Module contents¶
Enum Choice for Click.
- class enumchoice.EnumChoice(enum: Enum, case_sensitive=False)[source]¶
Bases:
ChoiceExpose Enum options within a click Choice option, using the names of the enum options.
based on https://github.com/pallets/click/issues/605#issuecomment-917022108 - but lookup by name instead of by value
Suppose you have an enum called ColorEnum.
- WITHOUT THIS, you would do something like this to expose ColorEnum choices in a Click CLI:
- @click.option(
“–color”, multiple=True, default=list(ColorEnum), show_default=True, type=click.Choice([c.name for c in ColorEnum], case_sensitive=False)
) def func(color):
… color = [ColorEnum[c] if isinstance(c, str) else c for c in color]
- Instead, simply use this (after from enumchoice import EnumChoice):
- @click.option(
“–color”, multiple=True, default=list(ColorEnum), show_default=True, type=EnumChoice(ColorEnum, case_sensitive=False),
) # if multiple isn’t True, default could be ‘red’ or ColorEnum.red
- convert(value, param, ctx)[source]¶
Convert the value to the correct type. This is not called if the value is
None(the missing value).This must accept string values from the command line, as well as values that are already the correct type. It may also convert other compatible types.
The
paramandctxarguments may beNonein certain situations, such as when converting prompt input.If the value cannot be converted, call
fail()with a descriptive message.- Parameters:
value – The value to convert.
param – The parameter that is using this type to convert its value. May be
None.ctx – The current context that arrived at this value. May be
None.