Skip to main content

For easy selection command-line selection of an `enum.Enum` variant with `argparse.Action`s.

Project description

pypi github

enum-actions

For easy selection command-line selection of an enum.Enum variant with argparse.Actions.

Use it like this:

>>> from enum_actions import enum_action
>>> from argparse import ArgumentParser
>>> import enum

>>> class MyEnum(enum.Enum):
...     A = 1
...     B = 2

>>> parser = ArgumentParser()
>>> _ = parser.add_argument("-e", "--enum", action=enum_action(MyEnum), default="a", help="pick a variant") # create an action for your enum
>>> args = parser.parse_args() # there will be an instance of MyEnum in the args object

Features

Choices are handled transparently

foo.py --help

usage: foo.py [-h] [-e {a,b}]

optional arguments:
  -h, --help            show this help message and exit
  -e {a,b}, --enum {a,b}
                        pick a variant (default: b)

Defaults are handled transparently

Having a default string or enum will both work

parser.add_argument("--enum", action=enum_action(MyEnum), default="a")
parser.add_argument("--enum", action=enum_action(MyEnum), default=MyEnum.A)

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

enum-actions-0.1.2.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

enum_actions-0.1.2-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file enum-actions-0.1.2.tar.gz.

File metadata

  • Download URL: enum-actions-0.1.2.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.10.60.1-microsoft-standard-WSL2

File hashes

Hashes for enum-actions-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6e8b03cc4a4f040b397b5ba8d8ecf2802b7c74bf356d2bb49669c548e5697f3a
MD5 233d9f470ad4fbdd1bd5094a524683e6
BLAKE2b-256 00eec8982025d7ad0b0d5d2f647af5138050649df46ae799829926b039a2046a

See more details on using hashes here.

File details

Details for the file enum_actions-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: enum_actions-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.10.60.1-microsoft-standard-WSL2

File hashes

Hashes for enum_actions-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 25bcf6d93e32be20856809bffcab9384e2c0c82cbe7e69a819a152834a9db88f
MD5 a09f4c5df0b23ae5b4b2e1d20fe82b7a
BLAKE2b-256 1c7020d7126276f9f37f69c52d7315ba8f872274136472ba595c74489e5f00b2

See more details on using hashes here.

Supported by

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