For easy selection command-line selection of an `enum.Enum` variant with `argparse.Action`s.
Project description
enum-actions
For easy selection command-line selection of an enum.Enum
variant with argparse.Action
s.
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
Release history Release notifications | RSS feed
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.1.tar.gz
(2.6 kB
view hashes)
Built Distribution
Close
Hashes for enum_actions-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3c8a693a6f2cc9f3c97c82087c5ac2e94963894051926c31496311391da2187 |
|
MD5 | 26088646c6d47bc4abb7464101604406 |
|
BLAKE2b-256 | ac70b5b576c9a795bc6cf8a0c1a9a9f2f38ac7677c1549700d86fcb47d0b91d6 |