This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author desbma
Recipients desbma
Date 2015-09-10.18:04:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1441908245.42.0.23532628335.issue25061@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2015-09-10 18:04:05desbmasetrecipients: + desbma
2015-09-10 18:04:05desbmasetmessageid: <1441908245.42.0.23532628335.issue25061@psf.upfronthosting.co.za>
2015-09-10 18:04:05desbmalinkissue25061 messages
2015-09-10 18:04:05desbmacreate