Fix Enum arguments that accept multiple values
EnumAction was erasing nargs before passing it to the superclass initializer, which broke handling for arguments with consecutive values. The help text for --desired-buyer has been updated to reflect that you actually can specify multiple values with a single declaration now.
This commit is contained in:
parent
4ef31a9cca
commit
cacb985be6
|
@ -67,7 +67,7 @@ skeleton_parameters.add_argument(
|
||||||
action=EnumAction,
|
action=EnumAction,
|
||||||
nargs='+',
|
nargs='+',
|
||||||
type=Buyer,
|
type=Buyer,
|
||||||
help="specific buyer that skeleton should be designed for (if declared repeatedly, will choose from among those provided)",
|
help="specific buyer that skeleton should be designed for (if multiple are specified, will choose from among them)",
|
||||||
dest='desired_buyers'
|
dest='desired_buyers'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ class EnumAction(Action):
|
||||||
# Pop off the type value
|
# Pop off the type value
|
||||||
enum = kwargs.pop('type', None)
|
enum = kwargs.pop('type', None)
|
||||||
|
|
||||||
nargs = kwargs.pop('nargs', None)
|
nargs = kwargs.get('nargs')
|
||||||
|
|
||||||
# Generate choices from the Enum
|
# Generate choices from the Enum
|
||||||
kwargs.setdefault('choices', tuple(member.name.lower() for member in enum))
|
kwargs.setdefault('choices', tuple(member.name.lower() for member in enum))
|
||||||
|
@ -19,12 +19,14 @@ class EnumAction(Action):
|
||||||
self._nargs = nargs
|
self._nargs = nargs
|
||||||
|
|
||||||
def __call__(self, parser, namespace, values, option_string=None):
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
|
if self._nargs is None or self._nargs == '?':
|
||||||
# Convert value back into an Enum
|
# Convert value back into an Enum
|
||||||
enum = self._enum[values.upper()]
|
enum = self._enum[values.upper()]
|
||||||
|
|
||||||
if self._nargs is None or self._nargs == '?':
|
|
||||||
setattr(namespace, self.dest, enum)
|
setattr(namespace, self.dest, enum)
|
||||||
else:
|
else:
|
||||||
items = getattr(namespace, self.dest, list())
|
# Convert values back into Enums
|
||||||
items.append(enum)
|
enums = [self._enum[value.upper()] for value in values]
|
||||||
|
|
||||||
|
items = getattr(namespace, self.dest, list()) + enums
|
||||||
setattr(namespace, self.dest, items)
|
setattr(namespace, self.dest, items)
|
||||||
|
|
Loading…
Reference in New Issue