From 35663dd8d4e720a6fb556a513eb6f03704ff7225 Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Sat, 14 Aug 2021 17:45:10 -0500 Subject: [PATCH] Add list option This option lists enumeration identifiers, along with their string forms for clarity's sake. This makes it much easier to blacklist things without reading the source code. A note about listing has been added to the Blacklisting section of the README. A dedicated action has been created for the list command. --- README.md | 6 +++ bonemarketsolver/__main__.py | 11 ++++++ bonemarketsolver/objects/listaction.py | 53 ++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 bonemarketsolver/objects/listaction.py diff --git a/README.md b/README.md index 66a69c0..0de87b2 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,12 @@ You may wish to blacklist options that: * add Exhaustion * you simply don't have a good source for +To get a list of identifiers, use: +```sh +pipenv run bone_market_solver --list +``` +Specific enumerations may be provided to narrow what is shown. + ### Configuration Files Rather than typing out every argument each time you use the solver, you may provide a file path (prefixed with "@") to the CLI. diff --git a/bonemarketsolver/__main__.py b/bonemarketsolver/__main__.py index 1eb8024..039391e 100644 --- a/bonemarketsolver/__main__.py +++ b/bonemarketsolver/__main__.py @@ -4,6 +4,7 @@ import curses from .objects.blacklistaction import BlacklistAction from .objects.bonemarketargumentparser import BoneMarketArgumentParser from .objects.enumaction import EnumAction +from .objects.listaction import ListAction from .solve import * parser = BoneMarketArgumentParser( @@ -13,6 +14,16 @@ parser = BoneMarketArgumentParser( argument_default=argparse.SUPPRESS, ) +parser.add_argument( + "-l", "--list", + action=ListAction, + default=ListAction.list_options, + choices=ListAction.list_options, + nargs=argparse.ZERO_OR_MORE, + help="list specified enumerations and their names and exit", + dest=argparse.SUPPRESS + ) + world_qualities = parser.add_argument_group( "world qualities", diff --git a/bonemarketsolver/objects/listaction.py b/bonemarketsolver/objects/listaction.py new file mode 100644 index 0000000..41a7ec1 --- /dev/null +++ b/bonemarketsolver/objects/listaction.py @@ -0,0 +1,53 @@ +__all__ = ['ListAction'] +__author__ = "Jeremy Saklad" + +import argparse +from functools import reduce + +from ..data.adjustments import Adjustment +from ..data.appendages import Appendage +from ..data.buyers import Buyer +from ..data.declarations import Declaration +from ..data.embellishments import Embellishment +from ..data.skulls import Skull +from ..data.torsos import Torso + +class ListAction(argparse.Action): + """Lists enumerations referenced by provided strings then exits""" + + list_options = [enum.__name__.lower() for enum in [ + Torso, + Skull, + Appendage, + Adjustment, + Declaration, + Embellishment, + Buyer, + ]] + + def __init__(self, **kwargs): + nargs = kwargs.get('nargs') + default = kwargs.get('default') + + super(ListAction, self).__init__(**kwargs) + + self._nargs = nargs + self._default = default + + @staticmethod + def printable_list(enum): + def printable_item(member): + return f"\n\t{enum.__name__}.{member.name}:\n\t\t{member}" + + return "{}:{}".format(enum.__name__, str().join([printable_item(member) for member in enum])) + + def __call__(self, parser, namespace, values, option_string=None): + # Check whether this is a single value or a list of them + if self._nargs is None or self._nargs == argparse.OPTIONAL: + # Convert value into an Enum type and print + print(self.printable_list(globals()[values.capitalize()])) + else: + # Convert values back into Enum types and print + print(*[self.printable_list(globals()[value.capitalize()]) for value in (values if values else self._default)], sep="\n\n") + + parser.exit()