Message250399
I often find myself using the following pattern with argparse:
import argparse
import enum
CustomEnumType = enum.Enum("CustomEnumType",
("VAL1", "VAL2", "VAL3", ...))
arg_parser = argparse.ArgumentParser(...)
...
arg_parser.add_argument("-p",
"--param",
type="string",
action="store",
choices=tuple(t.name.lower() for t in CustomEnumType),
default=CustomEnumType.VAL1.name.lower(),
dest="param"
...)
args = arg_parser.parse_args()
args.param = CustomEnumType[args.param.upper()]
I think it would be a great addition to be able to pass the enum type to the add_argument 'type' parameter directly, and have it validate the input and store the resulting enum. |
|
Date |
User |
Action |
Args |
2015-09-10 18:04:05 | desbma | set | recipients:
+ desbma |
2015-09-10 18:04:05 | desbma | set | messageid: <1441908245.42.0.23532628335.issue25061@psf.upfronthosting.co.za> |
2015-09-10 18:04:05 | desbma | link | issue25061 messages |
2015-09-10 18:04:05 | desbma | create | |
|