shangxiao <[email protected]> added the comment:
Oh apologies, I had "open" selected when I searched for prior issues.
Upon reading that issue I agree with Mr Hettinger's points about enum values
not being a concern of the parser.
The solution for my needs was simple enough: I made my own action which simply
set choices based on member values (I'm using user-friendly strings) and
converted to the correct type upon being called [1].
Instead of removing any mention of enums from the docs - would a small example
showing how to deal with them be worthwhile?
[1]
def enum_action_factory(enum_class):
class EnumAction(argparse.Action):
def __init__(self, option_strings, dest, **kwargs):
kwargs["choices"] = [member.value for member in enum_class]
super().__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string):
if isinstance(values, str):
converted_values = enum_class(values)
else:
converted_values = [enum_class(value) for value in values]
setattr(namespace, self.dest, converted_values)
return EnumAction
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42501>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com