Message350853
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} |
|
Date |
User |
Action |
Args |
2019-08-30 06:04:49 | rhettinger | set | recipients:
+ rhettinger, barry, bethard, paul.j3, desbma, leycec |
2019-08-30 06:04:49 | rhettinger | set | messageid: <1567145089.48.0.466664335213.issue25061@roundup.psfhosted.org> |
2019-08-30 06:04:49 | rhettinger | link | issue25061 messages |
2019-08-30 06:04:49 | rhettinger | create | |
|