Skip to main content

Enum Choice for Click

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

enumchoice-0.0.2.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

enumchoice-0.0.2-py2.py3-none-any.whl (4.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file enumchoice-0.0.2.tar.gz.

File metadata

  • Download URL: enumchoice-0.0.2.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for enumchoice-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b333167598359abdb920ff9c5a6b25aad4ce4edcc61c278759aa603fc6e43f4c
MD5 d4b62fcabb7d366995805db0be5c04ba
BLAKE2b-256 947596fd26560b57483e42249ea734f6342846631c982f5d0552480f3f51ab63

See more details on using hashes here.

File details

Details for the file enumchoice-0.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: enumchoice-0.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for enumchoice-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 826ba91ede7d1f91af800f5848e0363c1e1875fdb173d73b7eb7b7aee6d667c0
MD5 66eb0b16304d44f35550bfee78f1636b
BLAKE2b-256 908999ff6fde85aca0996eb0228edb07db844c506e4333921b8b2dba4cfe4fb4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page