[issue25061] Add native enum support for argparse

desbma report at bugs.python.org
Mon Sep 14 18:29:03 CEST 2015


desbma added the comment:

> With my type function, the string input is converted to an enum object and that is stored in the Namespace.  You can't be any more direct than that.

Yes I know, but in that case it's missing the autogenerated help message with the possible choices.

I know I can generate it manually, it just does not feel right for doing something so simple. IMO the goal of argparse is to unload the burden of checking/validating parameters manually, generating help messages with default/possible values, etc.

Your solution with a dictionnary is similar to what I currently use and wrote in my example, with the added drawback that the keys are randomly ordered in the help message, unless I use OrderedDict (one more import and more boilerplate code).

Each approach has its drawbacks, unless you write some additional code to workaround each limitation.

In a perfect world, argparse would:
* only show to the user the enum names, and use it in help/error messages, possible choice set, etc.
* after parsing, set the real enum value in the namespace
* and most importantly: to do that, don't require more code than just passing the enum

----------

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


More information about the Python-bugs-list mailing list