[issue25061] Add native enum support for argparse
Raymond Hettinger
report at bugs.python.org
Fri Aug 30 02:04:49 EDT 2019
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
Depending on how you want to expose enums to end-users, some reasonable options already exist:
import argparse
from enum import Enum
class Shake(Enum):
VANILLA = 7
CHOCOLATE = 4
COOKIES = 9
MINT = 3
# Option 1
ap = argparse.ArgumentParser()
ap.add_argument('shakes', nargs=2, choices=Shake, type=Shake.__getitem__)
ns = ap.parse_args(['VANILLA', 'MINT'])
print(ns)
# Option 2
ap = argparse.ArgumentParser()
ap.add_argument('shakes', nargs=2, choices=Shake.__members__)
ns = ap.parse_args(['VANILLA', 'MINT'])
ns.shakes = [Shake[name] for name in ns.shakes]
print(ns)
In Option 1, the user sees choices of:
{Shake.VANILLA,Shake.CHOCOLATE,Shake.COOKIES,Shake.MINT}
In Option 2, the user sees choices of:
{VANILLA,CHOCOLATE,COOKIES,MINT}
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue25061>
_______________________________________
More information about the Python-bugs-list
mailing list