From cacb985be6121597fda46fc1b4c725f0b5e3fb5f Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Fri, 13 Aug 2021 08:27:23 -0500 Subject: [PATCH] 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. --- bonemarketsolver/__main__.py | 2 +- bonemarketsolver/objects/enumaction.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/bonemarketsolver/__main__.py b/bonemarketsolver/__main__.py index 2f1eaad..8b0d085 100644 --- a/bonemarketsolver/__main__.py +++ b/bonemarketsolver/__main__.py @@ -67,7 +67,7 @@ skeleton_parameters.add_argument( action=EnumAction, nargs='+', 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' ) diff --git a/bonemarketsolver/objects/enumaction.py b/bonemarketsolver/objects/enumaction.py index be1c935..a53945c 100644 --- a/bonemarketsolver/objects/enumaction.py +++ b/bonemarketsolver/objects/enumaction.py @@ -8,7 +8,7 @@ class EnumAction(Action): # Pop off the type value enum = kwargs.pop('type', None) - nargs = kwargs.pop('nargs', None) + nargs = kwargs.get('nargs') # Generate choices from the Enum kwargs.setdefault('choices', tuple(member.name.lower() for member in enum)) @@ -19,12 +19,14 @@ class EnumAction(Action): self._nargs = nargs def __call__(self, parser, namespace, values, option_string=None): - # Convert value back into an Enum - enum = self._enum[values.upper()] - if self._nargs is None or self._nargs == '?': + # Convert value back into an Enum + enum = self._enum[values.upper()] + setattr(namespace, self.dest, enum) else: - items = getattr(namespace, self.dest, list()) - items.append(enum) + # Convert values back into Enums + enums = [self._enum[value.upper()] for value in values] + + items = getattr(namespace, self.dest, list()) + enums setattr(namespace, self.dest, items)