Skip to content

Python Enum support for click.Choice #605

Closed

Description

@devraj

I can work around it as described here it would be great if Choice supported Python Enum.

Unless of course I have missed something completely fundamental :-)

Activity

  1. mitsuhiko commented on Jul 9, 2016

    @mitsuhiko
    Contributor

    What exactly is implied with supporting an enum?

  2. devraj commented on Jul 9, 2016

    @devraj
    Author

    To be able to use Enum instead of tuples for Choice values, i.e define it as such

    from enum import Enum, unique
    
    @unique
    class ConfigFormat(Enum):
        yaml = 0
        json = 1
        plist = 2

    and then use it in the decorator as follows

    from . import const
    
    @dispatch.command()
    @click.option('--output', '-o', type=click.Choice(const.ConfigFormat),
                    help='Sets default output format for configuration files')
    @pass_project
    def init(project, output, force):
        """Initialises a managed schema"""
        click.echo(project.schema_home)
  3. aldanor commented on Jul 25, 2016

    @aldanor

    enum is only available in Python 3 though (in Python 2 you have to use an external backport).

  4. TAGC commented on Sep 8, 2016

    @TAGC
  5. untitaker commented on Sep 8, 2016

    @untitaker
  6. TAGC commented on Sep 8, 2016

    @TAGC

    I was being facetious, sorry. It would be a neat feature but it's nothing essential by any means. Loving this tool by the way.

  7. allanlewis commented on Dec 6, 2016

    @allanlewis

    I'm using something like this in my code, where I'm using Python 2.7 with enum34==1.1.6:

    @click.option(
        '--enum-val', type=click.Choice(MyEnum.__members__),
        callback=lambda c, p, v: getattr(MyEnum, v) if v else None)

    The callback provides enum_val as the actual enumeration instance rather than a string, or None if the option wasn't given.

    Perhaps this could be wrapped into another decorator, like click.enum_option:

    @click.enum_option('--enum-val', enum=MyEnum)

    UPDATE: In review, others thought that callback was rather ugly, which it is, so I've removed it. If it was inside click, it might be OK.

  8. skycaptain commented on Feb 5, 2017

    @skycaptain

    The easiest way I can think of, is to use a custom type (Although I think this would be a nice feature for click):

    class EnumType(click.Choice):
        def __init__(self, enum):
            self.__enum = enum
            super().__init__(enum.__members__)
    
        def convert(self, value, param, ctx):
            return self.__enum[super().convert(value, param, ctx)]

    You might overwrite get_metavar to compute the metavar from class name (since the complete choice list might be to long for a cli printout):

    ...
        def get_metavar(self, param):
            # Gets metavar automatically from enum name
            word = self.__enum.__name__
    
            # Stolen from jpvanhal/inflection
            word = re.sub(r"([A-Z]+)([A-Z][a-z])", r'\1_\2', word)
            word = re.sub(r"([a-z\d])([A-Z])", r'\1_\2', word)
            word = word.replace("-", "_").lower().split("_")
    
            if word[-1] == "enum":
                word.pop()
    
            return ("_".join(word)).upper()
    ...

    Since Enums are sometimes written uppercase, feel free to write a case insensitive version of the above code. E.g. one could start off with

    class EnumType(click.Choice):
        def __init__(self, enum, casesensitive=True):
            if isinstance(enum, tuple):
                choices = (_.name for _ in enum)
            elif isinstance(enum, EnumMeta):
                choices = enum.__members__
            else:
                raise TypeError("`enum` must be `tuple` or `Enum`")
    
            if not casesensitive:
                choices = (_.lower() for _ in choices)
    
            self.__enum = enum
            self.__casesensitive = casesensitive
    
            # TODO choices do not have the save order as enum
            super().__init__(list(sorted(set(choices))))
    
        def convert(self, value, param, ctx):
            if not self.__casesensitive:
                value = value.lower()
    
            value = super().convert(value, param, ctx)
    
            if not self.__casesensitive:
                return next(_ for _ in self._EnumType__enum if _.name.lower() ==
                                value.lower())
            else:
                return next(_ for _ in self._EnumType__enum if _.name == value)
    
        def get_metavar(self, param):
            word = self.__enum.__name__
    
            # Stolen from jpvanhal/inflection
            word = re.sub(r"([A-Z]+)([A-Z][a-z])", r'\1_\2', word)
            word = re.sub(r"([a-z\d])([A-Z])", r'\1_\2', word)
            word = word.replace("-", "_").lower().split("_")
    
            if word[-1] == "enum":
                word.pop()
    
            return ("_".join(word)).upper()

    This way you can either add a complete Enum or you can just use some values as a choice (listed as a tuple).

  9. added a commit that references this issue on Mar 10, 2018
  10. added a commit that references this issue on Mar 31, 2018
  11. aomader commented on Jun 21, 2019

    @aomader

    A bit old but I thought I add the nice idea of using str Enums for options (which is also great for old-school str-based settings w.r.t. comparisons).

    class ChoiceType(click.Choice):
        def __init__(self, enum):
            super().__init__(map(str, enum))
            self.enum = enum
    
        def convert(self, value, param, ctx):
            value = super().convert(value, param, ctx)
            return next(v for v in self.enum if str(v) == value)
    
    class Choice(str, Enum):
        def __str__(self):
            return str(self.value)
    
    class MyChoice(Choice):
        OPT_A = 'opt-a'
        OPT_B = 'opt-b'
    
    @click.option('--choice', type=MyChoiceType(MyChoice),
                  default=MyChoice.OPT_B)
    def func(choice):
        assert choice in ('opt-a', 'opt-b')
        assert choice in (MyChoice.OPT_A, MyChoice.OPT_B)
        
  12. gazpachoking commented on Feb 5, 2020

    @gazpachoking

    Here's my take on supporting this:

    class EnumChoice(click.Choice):
        def __init__(self, enum, case_sensitive=False, use_value=False):
            self.enum = enum
            self.use_value = use_value
            choices = [str(e.value) if use_value else e.name for e in self.enum]
            super().__init__(choices, case_sensitive)
    
        def convert(self, value, param, ctx):
            if value in self.enum:
                return value
            result = super().convert(value, param, ctx)
            # Find the original case in the enum
            if not self.case_sensitive and result not in self.choices:
                result = next(c for c in self.choices if result.lower() == c.lower())
            if self.use_value:
                return next(e for e in self.enum if str(e.value) == result)
            return self.enum[result]

    Allows using either the names or values of the enum items based on the use_value parameter. Should work whether values are strings or not.

  13. bartekpacia commented on Aug 2, 2020

    @bartekpacia

    I don't know if somebody posted it here, but that's how I dealt with it:

    Language = Enum("Language", "pl en")
    
    @click.option("--language", type=click.Choice(list(map(lambda x: x.name, Language)), case_sensitive=False))
    def main():
    ...
    
  14. 5 remaining items

  15. ShivKJ commented on Sep 10, 2021

    @ShivKJ

    @yashrathi-git a minor change in the code you provided,

    from enum import Enum
    
    from click import Choice
    
    class Test(Enum):
        test = "test"
        another_option = "another_option"
    
        def __str__(self):
            return self.value
    
    
    class EnumType(Choice):
        def __init__(self, enum: Enum, case_sensitive=False):
            self.__enum = enum
            super().__init__(choices=[item.value for item in enum], case_sensitive=case_sensitive)
    
        def convert(self, value, param, ctx):
            if value is None or isinstance(value, Enum)::
                return value
    
            converted_str = super().convert(value, param, ctx)
            return self.__enum(converted_str)

    But I feel this should be done under the hood instead of importing EnumType.

    In other words, it would be much more desirable if we could write,

    @click.option("-m", "--method", type=Test, default = 'test') # or default = Test.test
  16. jerluc commented on Dec 29, 2021

    @jerluc

    I know this thread is pretty old, but I've been using this pattern for str + Enums, which IMO is pretty clean and intuitive:

    import click
    import enum
    
    
    class MyEnum(str, enum.Enum):
        A = "a"
        B = "b"
        C = "c"
    
    
    @click.command("my-cmd")
    @click.argument("which_option", type=click.Choice(MyEnum))
    def my_cmd(which_option: MyEnum):
        print(which_option)
        print(type(which_option))
    
    
    if __name__ == "__main__":
        my_cmd()
    $ python click_enum.py
    
    Usage: click_enum.py [OPTIONS] [a|b|c]
    Try 'click_enum.py --help' for help.
    
    Error: Missing argument '[a|b|c]'.  Choose from:
    	a,
    	b,
    	c.
    
    $ python click_enum.py a
    
    MyEnum.A
    <enum 'MyEnum'>
    
    $ python click_enum.py d
    
    Usage: click_enum.py [OPTIONS] [a|b|c]
    Try 'click_enum.py --help' for help.
    
    Error: Invalid value for '[a|b|c]': invalid choice: d. (choose from a, b, c)
    
  17. rdbisme commented on Jan 19, 2022

    @rdbisme

    Hello @jerluc, your pattern works functionally, but it fails type checking

  18. jerluc commented on Jan 19, 2022

    @jerluc

    Hello @jerluc, your pattern works functionally, but it fails type checking

    Does it @rdbisme? I don't see any issues using latest mypy (0.931) on Python 3.7:

    jerluc@ws ~ $ cat click_enum.py
    import click
    import enum
    
    
    class MyEnum(str, enum.Enum):
        A = "a"
        B = "b"
        C = "c"
    
    
    @click.command("my-cmd")
    @click.argument("which_option", type=click.Choice(MyEnum))
    def my_cmd(which_option: MyEnum):
        print(which_option)
        print(type(which_option))
    
    
    if __name__ == "__main__":
        my_cmd()
    jerluc@ws ~ $ mypy click_enum.py
    Success: no issues found in 1 source file
    jerluc@ws ~ $ mypy --version
    mypy 0.931
    
  19. rdbisme commented on Jan 21, 2022

    @rdbisme

    Well,

    $ cat click_test.py
    import click
    import enum
    
    
    class MyEnum(str, enum.Enum):
        A = "a"
        B = "b"
        C = "c"
    
    
    @click.command("my-cmd")
    @click.argument("which_option", type=click.Choice(MyEnum))
    def my_cmd(which_option: MyEnum):
        print(which_option)
        print(type(which_option))
    
    
    if __name__ == "__main__":
        my_cmd()
    

    Still Gives:

    click_test.py:12: error: Argument 1 to "Choice" has incompatible type "Type[MyEnum]"; expected "Sequence[str]"
    Found 1 error in 1 file (checked 1 source file)
    
    $ python --version
    Python 3.9.9
    
    $ python -c "import click; print(click.__version__)"
    8.0.3
    
  20. dzcode commented on May 2, 2022

    @dzcode
    Contributor

    I'm at the PyCon sprint now and could work on this

  21. davidism commented on May 2, 2022

    @davidism
    Member

    @dzcode thanks, but there's already an open PR for this.

  22. MicaelJarniac commented on May 3, 2022

    @MicaelJarniac

    @dzcode thanks, but there's already an open PR for this.

    #2210?

  23. AndreasBackx commented on Nov 2, 2024

    @AndreasBackx
    Collaborator

    Let's try and finally get this in 8.2.0. If anyone has any feedback, I've put up #2796. Looking for feedback as I'm going to continue to look through the other open issues / PRs that are on the list to be included in 8.2.0, see the 8.2.0 Release Plan. It might be a better and it might be worse than #2210, let me know which one it is!

  24. racinmat commented on Nov 20, 2024

    @racinmat

    Regarding the typecheck of

    @click.argument("which_option", type=click.Choice(MyEnum))

    this is what works for me and the typecheck passes

    @click.argument("which_option", type=click.Choice(list(MyEnum)))
  25. added this to the 8.2.0 milestone on Jan 12, 2025
  26. locked as resolved and limited conversation to collaborators on Jan 27, 2025
  27. kdeldycke commented on Nov 18, 2025

    @kdeldycke
    Collaborator

    For the record, click.Choice is supporting Enums, but naively: the Enum.name property of each members is used for choices. It was designed that way to simplify the implementation, because it is the part of Enum that is guaranteed to be unique strings.

    If you need more flexible source of string values, you can use Click Extra's EnumChoice.

  28. added
    parsing
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    parsing

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions