[New-bugs-announce] [issue25061] Add native enum support for argparse

desbma report at bugs.python.org
Thu Sep 10 20:04:05 CEST 2015


New submission from desbma:

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.

----------
messages: 250399
nosy: desbma
priority: normal
severity: normal
status: open
title: Add native enum support for argparse
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25061>
_______________________________________


More information about the New-bugs-announce mailing list