[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.
This commit is contained in:
derrod 2022-01-07 14:32:36 +01:00
parent cf8bccc569
commit 58bd76c39e
2 changed files with 30 additions and 0 deletions

View file

@ -23,6 +23,7 @@ from legendary.models.exceptions import InvalidCredentialsError
from legendary.models.game import SaveGameStatus, VerifyResult, Game 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.cli import get_boolean_choice, get_int_choice, sdl_prompt, strtobool
from legendary.utils.crossover import * 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.env import is_windows_mac_or_pyi
from legendary.utils.eos import add_registry_entries, query_registry_entries, remove_registry_entries from legendary.utils.eos import add_registry_entries, query_registry_entries, remove_registry_entries
from legendary.utils.lfs import validate_files, clean_filename from legendary.utils.lfs import validate_files, clean_filename
@ -2350,6 +2351,7 @@ class LegendaryCLI:
def main(): def main():
parser = argparse.ArgumentParser(description=f'Legendary v{__version__} - "{__codename__}"') parser = argparse.ArgumentParser(description=f'Legendary v{__version__} - "{__codename__}"')
parser.register('action', 'parsers', HiddenAliasSubparsersAction)
# general arguments # general arguments
parser.add_argument('-H', '--full-help', dest='full_help', action='store_true', parser.add_argument('-H', '--full-help', dest='full_help', action='store_true',

View file

@ -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