https://github.com/aatifsyed/enum-actions
For easy command-line selection of an enum
https://github.com/aatifsyed/enum-actions
argparse enum python
Last synced: 10 days ago
JSON representation
For easy command-line selection of an enum
- Host: GitHub
- URL: https://github.com/aatifsyed/enum-actions
- Owner: aatifsyed
- Created: 2021-07-01T21:56:57.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-10T17:04:51.000Z (almost 4 years ago)
- Last Synced: 2025-09-27T21:15:58.034Z (5 months ago)
- Topics: argparse, enum, python
- Language: Python
- Homepage:
- Size: 16.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://pypi.org/project/enum-actions/)
[](https://github.com/aatifsyed/enum-actions)
# `enum-actions`
For easy selection command-line selection of an `enum.Enum` variant with `argparse.Action`s.
Use it like this:
```python
>>> 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
```text
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
```python
parser.add_argument("--enum", action=enum_action(MyEnum), default="a")
parser.add_argument("--enum", action=enum_action(MyEnum), default=MyEnum.A)
```