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 paul.j3
Recipients barry, desbma, paul.j3
Date 2015-09-11.01:00:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1441933256.77.0.625122143232.issue25061@psf.upfronthosting.co.za>
In-reply-to
Content
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?
History
Date User Action Args
2015-09-11 01:00:56paul.j3setrecipients: + paul.j3, barry, desbma
2015-09-11 01:00:56paul.j3setmessageid: <1441933256.77.0.625122143232.issue25061@psf.upfronthosting.co.za>
2015-09-11 01:00:56paul.j3linkissue25061 messages
2015-09-11 01:00:55paul.j3create