Bug 874471 - Handle mach command help more robustly; r=ted

DONTBUILD (NPOTB) on a CLOSED TREE
This commit is contained in:
Gregory Szorc 2013-05-21 15:37:06 -07:00
parent 0f6871932f
commit 2a834196e7

View File

@ -16,6 +16,13 @@ from .base import (
)
class CommandFormatter(argparse.HelpFormatter):
"""Custom formatter to format just a subcommand."""
def add_usage(self, *args):
pass
class CommandAction(argparse.Action):
"""An argparse action that handles mach commands.
@ -160,7 +167,20 @@ class CommandAction(argparse.Action):
if not handler:
raise UnknownCommandError(command, 'query')
group = parser.add_argument_group('Command Arguments')
# This code is worth explaining. Because we are doing funky things with
# argument registration to allow the same option in both global and
# command arguments, we can't simply put all arguments on the same
# parser instance because argparse would complain. We can't register an
# argparse subparser here because it won't properly show help for
# global arguments. So, we employ a strategy similar to command
# execution where we construct a 2nd, independent ArgumentParser for
# just the command data then supplement the main help's output with
# this 2nd parser's. We use a custom formatter class to ignore some of
# the help output.
c_parser = argparse.ArgumentParser(formatter_class=CommandFormatter,
add_help=False)
group = c_parser.add_argument_group('Command Arguments')
for arg in handler.arguments:
group.add_argument(*arg[0], **arg[1])
@ -173,4 +193,6 @@ class CommandAction(argparse.Action):
parser.usage = '%(prog)s [global arguments] ' + command + \
' [command arguments]'
parser.print_help()
print('')
c_parser.print_help()