paul j3 added the comment:

Here's a type function that enumerates the enum in the error message:

def enumtype(astring):
    try:
        return CustomEnumType[astring.upper()]
    except KeyError:
        msg = ', '.join([t.name.lower() for t in CustomEnumType])
        msg = 'CustomEnumType: use one of {%s}'%msg
        raise argparse.ArgumentTypeError(msg)

You could do the same sort of enumeration in the `help` parameter (or even the 
metavar).

One further note - the input string is first passed through `type`, then it is 
checked against the `choices` (if any).  If it is converted to the enum in an 
type function, the choices will also have to be enums, not their string 
representation.  

String defaults are (usually) passed through `type`.  Nonstring defaults are 
not converted or tested.

So the value of this sort of type function depends on whether it is more 
convenient to work with the string representation or the enum itself.  When 
exactly do you want the commandline string to be converted to the enum?

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25061>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to