Bug 1381802 - [mach] Allow subcommands to use the 'parser' argument, r=gps
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Tue, 18 Jul 2017 08:53:13 -0400
changeset 418222 58f0d7cf815080b7991a5c36f241a24753e8d333
parent 418221 6d94dc258f8f289afc29bc49bedc47faedcf9140
child 418223 c4735d730462920d02a9a919d634c4de9ece7163
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs1381802
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1381802 - [mach] Allow subcommands to use the 'parser' argument, r=gps This allows subcommands to use external argument parsers. MozReview-Commit-ID: 7TkbTff0Tv8
python/mach/mach/decorators.py
python/mach/mach/dispatcher.py
--- a/python/mach/mach/decorators.py
+++ b/python/mach/mach/decorators.py
@@ -250,19 +250,19 @@ class SubCommand(object):
 
         command -- The string of the command this sub command should be
         attached to.
 
         subcommand -- The string name of the sub command to register.
 
         description -- A textual description for this sub command.
     """
-    def __init__(self, command, subcommand, description=None):
+    def __init__(self, command, subcommand, description=None, parser=None):
         self._mach_command = _MachCommand(name=command, subcommand=subcommand,
-                                          description=description)
+                                          description=description, parser=parser)
 
     def __call__(self, func):
         if not hasattr(func, '_mach_command'):
             func._mach_command = _MachCommand()
 
         func._mach_command |= self._mach_command
 
         return func
--- a/python/mach/mach/dispatcher.py
+++ b/python/mach/mach/dispatcher.py
@@ -384,18 +384,19 @@ class CommandAction(argparse.Action):
     def _handle_subcommand_help(self, parser, handler, args):
         subcommand = set(args).intersection(handler.subcommand_handlers.keys())
         if not subcommand:
             return self._handle_subcommand_main_help(parser, handler)
 
         subcommand = subcommand.pop()
         subhandler = handler.subcommand_handlers[subcommand]
 
-        c_parser = argparse.ArgumentParser(add_help=False,
-            formatter_class=CommandFormatter)
+        c_parser = subhandler.parser or argparse.ArgumentParser(add_help=False)
+        c_parser.formatter_class = CommandFormatter
+
         group = c_parser.add_argument_group('Sub Command Arguments')
         self._populate_command_group(c_parser, subhandler, group)
 
         if subhandler.docstring:
             parser.description = format_docstring(subhandler.docstring)
 
         parser.formatter_class = argparse.RawDescriptionHelpFormatter
         parser.usage = '%(prog)s [global arguments] ' + handler.name + \