Skip to main content

enumchoice

CI

enumchoice is a small helper for Click CLIs that lets a Python Enum be used directly as an option type. It exposes enum member names as the accepted command-line choices and returns the matching enum member to the Click command.

Why it exists

Click's built-in click.Choice validates strings. If an option represents an Enum, a command normally has to build a list of enum names for Click and then convert the selected strings back to enum members. EnumChoice keeps those two steps in one Click parameter type.

Installation

pip install enumchoice

The package requires Python 3.8 or newer and Click 7.0 or newer.

Usage

from enum import Enum

import click
from enumchoice import EnumChoice


class Color(Enum):
    red = "r"
    blue = "b"
    green = "g"


@click.command()
@click.option("--color", type=EnumChoice(Color), default=Color.red, show_default=True)
def cli(color):
    assert isinstance(color, Color)
    click.echo(color.name)

Matching is case-insensitive by default, so --color RED, --color red, and --color ReD all resolve to Color.red. Pass case_sensitive=True to require exact enum member names.

EnumChoice also works with Click's multiple=True options:

@click.option(
    "--color",
    multiple=True,
    default=list(Color),
    type=EnumChoice(Color),
)
def cli(color):
    # color is a tuple of Color members.
    ...

How it works

EnumChoice subclasses click.Choice. Its constructor builds the choice list from each enum member's name. During conversion it lets Click perform normal choice validation and case normalization, then looks up the normalized name in the enum class and returns that member.

None and already-converted Enum instances pass through unchanged, which allows enum-valued defaults such as default=Color.red or default=list(Color).

Behavior and limitations

  • Choices are enum member names, not enum values.
  • Click callbacks receive enum members, not strings.
  • No aliases are added beyond the enum's declared member names.
  • The package metadata currently marks the project as pre-alpha, version 0.0.1.

Development

pip install -r requirements_dev.txt
pip install -e .
make test
make lint
make docs

The repository includes pytest, pre-commit, coverage, Sphinx documentation, and release packaging configuration. The project is distributed under the MIT license.

Changelog

0.0.1

  • First release on PyPI.

Release files for enumchoice 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for enumchoice 0.0.2
File Size Uploaded
enumchoice-0.0.2.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for enumchoice 0.0.2
File Interpreter ABI Platform
enumchoice-0.0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size:13.6 kB

Release files / enumchoice-0.0.2.tar.gz

Download URL enumchoice-0.0.2.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b333167598359abdb920ff9c5a6b25aad4ce4edcc61c278759aa603fc6e43f4c
BLAKE2b-256 checksum
How to use checksums
947596fd26560b57483e42249ea734f6342846631c982f5d0552480f3f51ab63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / enumchoice-0.0.2-py2.py3-none-any.whl

Download URL enumchoice-0.0.2-py2.py3-none-any.whl
Size 4.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
826ba91ede7d1f91af800f5848e0363c1e1875fdb173d73b7eb7b7aee6d667c0
BLAKE2b-256 checksum
How to use checksums
908999ff6fde85aca0996eb0228edb07db844c506e4333921b8b2dba4cfe4fb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page