From cae8ac505536c6fffbef13fcd02d314063aae6b5 Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Sat, 14 Aug 2021 13:05:17 -0500 Subject: [PATCH] Clarify nargs specifiers Rather than using raw strings, nargs specifiers are now expressed in terms of argparse's corresponding global variables. --- bonemarketsolver/__main__.py | 4 ++-- bonemarketsolver/objects/blacklistaction.py | 7 ++++--- bonemarketsolver/objects/enumaction.py | 7 ++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/bonemarketsolver/__main__.py b/bonemarketsolver/__main__.py index 386334d..452a1c4 100644 --- a/bonemarketsolver/__main__.py +++ b/bonemarketsolver/__main__.py @@ -68,7 +68,7 @@ skeleton_parameters.add_argument( skeleton_parameters.add_argument( "-b", "--buyer", "--desired-buyer", action=EnumAction, - nargs='+', + nargs=argparse.ONE_OR_MORE, type=Buyer, help="specific buyer that skeleton should be designed for (if multiple are specified, will choose from among them)", dest='desired_buyers' @@ -91,7 +91,7 @@ skeleton_parameters.add_argument( skeleton_parameters.add_argument( "--blacklist", action=BlacklistAction, - nargs='+', + nargs=argparse.ONE_OR_MORE, help="components, options, or buyers that should not be used by the solver", metavar="Enum.MEMBER", dest='blacklist' diff --git a/bonemarketsolver/objects/blacklistaction.py b/bonemarketsolver/objects/blacklistaction.py index 1a714b2..0c848ca 100644 --- a/bonemarketsolver/objects/blacklistaction.py +++ b/bonemarketsolver/objects/blacklistaction.py @@ -1,7 +1,7 @@ __all__ = ['BlacklistAction'] __author__ = "Jeremy Saklad" -from argparse import Action +import argparse from ..data.adjustments import Adjustment from ..data.appendages import Appendage @@ -17,7 +17,7 @@ def convert_to_enum(value): enum = globals()[split[0]] return enum[split[1]] -class BlacklistAction(Action): +class BlacklistAction(argparse.Action): def __init__(self, **kwargs): nargs = kwargs.get('nargs') @@ -26,7 +26,8 @@ class BlacklistAction(Action): self._nargs = nargs def __call__(self, parser, namespace, values, option_string=None): - if self._nargs is None or self._nargs == '?': + # Check whether this is a single value or a list of them + if self._nargs is None or self._nargs == argparse.OPTIONAL: # Convert value back into an Enum enum = convert_to_enum(value) diff --git a/bonemarketsolver/objects/enumaction.py b/bonemarketsolver/objects/enumaction.py index a53945c..9b162bd 100644 --- a/bonemarketsolver/objects/enumaction.py +++ b/bonemarketsolver/objects/enumaction.py @@ -1,9 +1,9 @@ __all__ = ['EnumAction'] __author__ = "Jeremy Saklad" -from argparse import Action +import argparse -class EnumAction(Action): +class EnumAction(argparse.Action): def __init__(self, **kwargs): # Pop off the type value enum = kwargs.pop('type', None) @@ -19,7 +19,8 @@ class EnumAction(Action): self._nargs = nargs def __call__(self, parser, namespace, values, option_string=None): - if self._nargs is None or self._nargs == '?': + # Check whether this is a single value or a list of them + if self._nargs is None or self._nargs == argparse.OPTIONAL: # Convert value back into an Enum enum = self._enum[values.upper()]