legendary/legendary/utils/custom_parser.py
derrod 58bd76c39e [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.
2022-01-07 14:32:36 +01:00

29 lines
984 B
Python

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