Clarify nargs specifiers

Rather than using raw strings, nargs specifiers are now expressed in
terms of argparse's corresponding global variables.
This commit is contained in:
Jeremy Saklad 2021-08-14 13:05:17 -05:00
parent 45fb0d14e8
commit cae8ac5055
Signed by: Jeremy Saklad
GPG Key ID: 9CA2149583EDBF84
3 changed files with 10 additions and 8 deletions

View File

@ -68,7 +68,7 @@ skeleton_parameters.add_argument(
skeleton_parameters.add_argument( skeleton_parameters.add_argument(
"-b", "--buyer", "--desired-buyer", "-b", "--buyer", "--desired-buyer",
action=EnumAction, action=EnumAction,
nargs='+', nargs=argparse.ONE_OR_MORE,
type=Buyer, type=Buyer,
help="specific buyer that skeleton should be designed for (if multiple are specified, will choose from among them)", help="specific buyer that skeleton should be designed for (if multiple are specified, will choose from among them)",
dest='desired_buyers' dest='desired_buyers'
@ -91,7 +91,7 @@ skeleton_parameters.add_argument(
skeleton_parameters.add_argument( skeleton_parameters.add_argument(
"--blacklist", "--blacklist",
action=BlacklistAction, action=BlacklistAction,
nargs='+', nargs=argparse.ONE_OR_MORE,
help="components, options, or buyers that should not be used by the solver", help="components, options, or buyers that should not be used by the solver",
metavar="Enum.MEMBER", metavar="Enum.MEMBER",
dest='blacklist' dest='blacklist'

View File

@ -1,7 +1,7 @@
__all__ = ['BlacklistAction'] __all__ = ['BlacklistAction']
__author__ = "Jeremy Saklad" __author__ = "Jeremy Saklad"
from argparse import Action import argparse
from ..data.adjustments import Adjustment from ..data.adjustments import Adjustment
from ..data.appendages import Appendage from ..data.appendages import Appendage
@ -17,7 +17,7 @@ def convert_to_enum(value):
enum = globals()[split[0]] enum = globals()[split[0]]
return enum[split[1]] return enum[split[1]]
class BlacklistAction(Action): class BlacklistAction(argparse.Action):
def __init__(self, **kwargs): def __init__(self, **kwargs):
nargs = kwargs.get('nargs') nargs = kwargs.get('nargs')
@ -26,7 +26,8 @@ class BlacklistAction(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 == '?': # 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 # Convert value back into an Enum
enum = convert_to_enum(value) enum = convert_to_enum(value)

View File

@ -1,9 +1,9 @@
__all__ = ['EnumAction'] __all__ = ['EnumAction']
__author__ = "Jeremy Saklad" __author__ = "Jeremy Saklad"
from argparse import Action import argparse
class EnumAction(Action): class EnumAction(argparse.Action):
def __init__(self, **kwargs): def __init__(self, **kwargs):
# Pop off the type value # Pop off the type value
enum = kwargs.pop('type', None) enum = kwargs.pop('type', None)
@ -19,7 +19,8 @@ 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 == '?': # 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 # Convert value back into an Enum
enum = self._enum[values.upper()] enum = self._enum[values.upper()]