From 58bd76c39e07450d0193f99339a53a3b788cbdab Mon Sep 17 00:00:00 2001 From: derrod Date: Fri, 7 Jan 2022 14:32:36 +0100 Subject: [PATCH] [cli/utils] Reintroduce custom parser for hidden aliases I don't want to break people's muscle memory, but I also don't want to have the help output be messier than it needs to be. --- legendary/cli.py | 2 ++ legendary/utils/custom_parser.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 legendary/utils/custom_parser.py diff --git a/legendary/cli.py b/legendary/cli.py index 5383e48..feb915d 100644 --- a/legendary/cli.py +++ b/legendary/cli.py @@ -23,6 +23,7 @@ from legendary.models.exceptions import InvalidCredentialsError from legendary.models.game import SaveGameStatus, VerifyResult, Game from legendary.utils.cli import get_boolean_choice, get_int_choice, sdl_prompt, strtobool from legendary.utils.crossover import * +from legendary.utils.custom_parser import HiddenAliasSubparsersAction from legendary.utils.env import is_windows_mac_or_pyi from legendary.utils.eos import add_registry_entries, query_registry_entries, remove_registry_entries from legendary.utils.lfs import validate_files, clean_filename @@ -2350,6 +2351,7 @@ class LegendaryCLI: def main(): parser = argparse.ArgumentParser(description=f'Legendary v{__version__} - "{__codename__}"') + parser.register('action', 'parsers', HiddenAliasSubparsersAction) # general arguments parser.add_argument('-H', '--full-help', dest='full_help', action='store_true', diff --git a/legendary/utils/custom_parser.py b/legendary/utils/custom_parser.py new file mode 100644 index 0000000..c31ea3d --- /dev/null +++ b/legendary/utils/custom_parser.py @@ -0,0 +1,28 @@ +import argparse + + +class HiddenAliasSubparsersAction(argparse._SubParsersAction): + def add_parser(self, name, **kwargs): + # set prog from the existing prefix + if kwargs.get('prog') is None: + kwargs['prog'] = '%s %s' % (self._prog_prefix, name) + + aliases = kwargs.pop('aliases', ()) + hide_aliases = kwargs.pop('hide_aliases', False) + + # create a pseudo-action to hold the choice help + if 'help' in kwargs: + help = kwargs.pop('help') + _aliases = None if hide_aliases else aliases + choice_action = self._ChoicesPseudoAction(name, _aliases, help) + self._choices_actions.append(choice_action) + + # create the parser and add it to the map + parser = self._parser_class(**kwargs) + self._name_parser_map[name] = parser + + # make parser available under aliases also + for alias in aliases: + self._name_parser_map[alias] = parser + + return parser