DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# This module provides functionality for the command-line build tool
# (mach). It is packaged as a module because everything is a library.


from __future__ import absolute_import, print_function, unicode_literals

import argparse
import codecs
import errno
import errno
import imp
import logging
import os
import sys
import traceback
import uuid
from collections import Iterable

from six import string_types
from mach.sentry import register_sentry, report_exception
from six import string_types

from .base import (
    CommandContext,
    MissingFileError,
    MachError,
    MissingFileError,
    NoCommandError,
    UnknownCommandError,
    UnrecognizedArgumentError,
)
    FailedCommandError,
)
from .config import ConfigSettings
from .decorators import (
    CommandProvider,
from .dispatcher import CommandAction
)
from .dispatcher import CommandAction
from .logging import LoggingManager
from .registrar import Registrar
from .util import setenv
SUGGEST_MACH_BUSTED = r'''

SUGGEST_MACH_BUSTED_TEMPLATE = r'''
You can invoke |./mach busted| to check if this issue is already on file. If it
isn't, please use |./mach busted file %s| to report it. If |./mach busted| is
misbehaving, you can also inspect the dependencies of bug 1543241.

'''.lstrip()

MACH_ERROR_TEMPLATE = r'''
The error occurred in mach itself. This is likely a bug in mach itself or a

fundamental problem with a loaded module.

'''.lstrip() + SUGGEST_MACH_BUSTED_TEMPLATE

ERROR_FOOTER = r'''
message.
If filing a bug, please include the full output of mach, including this error
message.

The details of the failure are as follows:
'''.lstrip()
COMMAND_ERROR = r'''

COMMAND_ERROR_TEMPLATE = r'''
The error occurred in the implementation of the invoked mach command.

This should never occur and is likely a bug in the implementation of that
'''.lstrip() + SUGGEST_MACH_BUSTED
command.
'''.lstrip() + SUGGEST_MACH_BUSTED_TEMPLATE

MODULE_ERROR_TEMPLATE = r'''
The error occurred in code that was called by the mach command. This is either
'''.lstrip() + SUGGEST_MACH_BUSTED
a bug in the called code itself or in the way that mach is calling it.
'''.lstrip() + SUGGEST_MACH_BUSTED_TEMPLATE

NO_COMMAND_ERROR = r'''
It looks like you tried to run mach without a command.
Run |mach help| to show a list of commands.

Run |mach help| to show a list of commands.
'''.lstrip()

UNKNOWN_COMMAND_ERROR = r'''
It looks like you are trying to %s an unknown mach command: %s
%s
%s
Run |mach help| to show a list of commands.
'''.lstrip()

SUGGESTED_COMMANDS_MESSAGE = r'''
Did you want to %s any of these commands instead: %s?
Did you want to %s any of these commands instead: %s?
'''

UNRECOGNIZED_ARGUMENT_ERROR = r'''
It looks like you passed an unrecognized argument into mach.

The %s command does not accept the arguments: %s
'''.lstrip()
'''.lstrip()

INVALID_ENTRY_POINT = r'''
Entry points should return a list of command providers or directories
containing command providers. The following entry point is invalid:


    %s

You are seeing this because there is an error in an external module attempting
your system.
to implement a mach command. Please fix the error, or uninstall the module from
your system.
'''.lstrip()


class ArgumentParser(argparse.ArgumentParser):

    """Custom implementation argument parser to make things look pretty."""

    def error(self, message):
        """Custom error reporter to give more helpful text on bad commands."""
        if not message.startswith('argument command: invalid choice'):
            assert False
            argparse.ArgumentParser.error(self, message)
            assert False

        print('Invalid command specified. The list of commands is below.\n')
        self.print_help()

        sys.exit(1)

    def format_help(self):
        text = argparse.ArgumentParser.format_help(self)

        #
        # Strip out the silly command list that would preceed the pretty list.
        #
        # Commands:
        #   {foo,bar}
        #     foo  Do foo.
        #     bar  Do bar.
        search = 'Commands:\n  {'
        start = text.find(search)
        start = text.find(search)

        if start != -1:
            end = text.find('}\n', start)
            assert end != -1


            real_start = start + len('Commands:\n')
            real_end = end + len('}\n')

            text = text[0:real_start] + text[real_end:]


        return text


class ContextWrapper(object):
    def __init__(self, context, handler):
        object.__setattr__(self, '_handler', handler)
        object.__setattr__(self, '_context', context)
        object.__setattr__(self, '_handler', handler)

    def __getattribute__(self, key):
        try:
        except AttributeError as e:
            return getattr(object.__getattribute__(self, '_context'), key)
        except AttributeError as e:
            try:
                ret = object.__getattribute__(self, '_handler')(key)
            except (AttributeError, TypeError):
            except (AttributeError, TypeError):
                # TypeError is in case the handler comes from old code not
                # taking a key argument.
                raise e
            setattr(self, key, ret)
            return ret
            return ret

    def __setattr__(self, key, value):
        setattr(object.__getattribute__(self, '_context'), key, value)



@CommandProvider
class Mach(object):
    """Main mach driver type.

    This type is responsible for holding global mach state and dispatching
    This type is responsible for holding global mach state and dispatching
    a command from arguments.

    The following attributes may be assigned to the instance to influence
    behavior:
    behavior:

        populate_context_handler -- If defined, it must be a callable. The
            callable signature is the following:
                populate_context_handler(key=None)
            It acts as a fallback getter for the mach.base.CommandContext
            instance.
            for use in command handlers.
            This allows to augment the context instance with arbitrary data
            for use in command handlers.

        require_conditions -- If True, commands that do not have any condition
            functions applied will be skipped. Defaults to False.
        require_conditions -- If True, commands that do not have any condition

        settings_paths -- A list of files or directories in which to search
            for settings files to load.

    """


    USAGE = """%(prog)s [global arguments] command [command arguments]

mach (German for "do") is the main interface to the Mozilla build system and
common developer tasks.
mach (German for "do") is the main interface to the Mozilla build system and

You tell mach the command you want to perform and it does it for you.

Some common commands are:

Some common commands are:
    %(prog)s build     Build/compile the source tree.
    %(prog)s help      Show full help, including the list of all commands.

To see more help for a specific command, run:


  %(prog)s help <command>
"""

    def __init__(self, cwd):
        assert os.path.isdir(cwd)


        self.cwd = cwd
        self.log_manager = LoggingManager()
        self.logger = logging.getLogger(__name__)
        self.log_manager = LoggingManager()
        self.settings = ConfigSettings()
        self.settings_paths = []

        if 'MACHRC' in os.environ:
            self.settings_paths.append(os.environ['MACHRC'])


        self.log_manager.register_structured_logger(self.logger)
        self.global_arguments = []
        self.populate_context_handler = None
        self.global_arguments = []

    def add_global_argument(self, *args, **kwargs):
        """Register a global argument with the argument parser.

        Arguments are proxied to ArgumentParser.add_argument()

        """

        self.global_arguments.append((args, kwargs))

    def load_commands_from_directory(self, path):

        """Scan for mach commands from modules in a directory.

        This takes a path to a directory, loads the .py files in it, and
        registers and found mach command providers with this mach instance.
        """
        registers and found mach command providers with this mach instance.
        for f in sorted(os.listdir(path)):
            if not f.endswith('.py') or f == '__init__.py':
                continue

            full_path = os.path.join(path, f)

            module_name = 'mach.commands.%s' % f[0:-3]

            self.load_commands_from_file(full_path, module_name=module_name)

    def load_commands_from_file(self, path, module_name=None):
        """Scan for mach commands from a file.


        This takes a path to a file and loads it as a Python module under the
        module name specified. If no name is specified, a random one will be
        chosen.
        """
        module name specified. If no name is specified, a random one will be
        if module_name is None:
            # Ensure parent module is present otherwise we'll (likely) get
            # an error due to unknown parent.
            if 'mach.commands' not in sys.modules:
                mod = imp.new_module('mach.commands')
            # an error due to unknown parent.
                sys.modules['mach.commands'] = mod

            module_name = 'mach.commands.%s' % uuid.uuid4().hex

        try:
            module_name = 'mach.commands.%s' % uuid.uuid4().hex
            imp.load_source(module_name, path)
        except IOError as e:
            if e.errno != errno.ENOENT:
                raise

            if e.errno != errno.ENOENT:
            raise MissingFileError('%s does not exist' % path)

    def load_commands_from_entry_point(self, group='mach.providers'):
        """Scan installed packages for mach command provider entry points. An
    def load_commands_from_entry_point(self, group='mach.providers'):
        entry point is a function that returns a list of paths to files or
        directories containing command providers.

        This takes an optional group argument which specifies the entry point
        group to use. If not specified, it defaults to 'mach.providers'.
        """
        try:
        """
            import pkg_resources
        except ImportError:
            print("Could not find setuptools, ignoring command entry points",
                  file=sys.stderr)
            return
                  file=sys.stderr)

        for entry in pkg_resources.iter_entry_points(group=group, name=None):
            paths = entry.load()()
            if not isinstance(paths, Iterable):
                print(INVALID_ENTRY_POINT % entry)
            if not isinstance(paths, Iterable):
                sys.exit(1)

            for path in paths:
                if os.path.isfile(path):
                    self.load_commands_from_file(path)
                elif os.path.isdir(path):
                    self.load_commands_from_directory(path)
                else:
                    print("command provider '%s' does not exist" % path)
                else:

    def define_category(self, name, title, description, priority=50):

        """Provide a description for a named command category."""

        Registrar.register_category(name, title, description, priority)

    @property

    def require_conditions(self):
        return Registrar.require_conditions

    @require_conditions.setter
    def require_conditions(self, value):
    @require_conditions.setter
        Registrar.require_conditions = value

    def run(self, argv, stdin=None, stdout=None, stderr=None):
        """Runs mach with arguments provided from the command line.

    def run(self, argv, stdin=None, stdout=None, stderr=None):
        Returns the integer exit code that should be used. 0 means success. All
        other values indicate failure.
        """
        if self.populate_context_handler:
            topsrcdir = self.populate_context_handler('topdir')
        """
            register_sentry(topsrcdir)
        else:
            register_sentry()

        # If no encoding is defined, we default to UTF-8 because without this
        # the streams and restore once we have finished.
        # Python 2.7 will assume the default encoding of ASCII. This will blow
        # up with UnicodeEncodeError as soon as it encounters a non-ASCII
        # character in a unicode instance. We simply install a wrapper around
        # the streams and restore once we have finished.
        stdin = sys.stdin if stdin is None else stdin
        orig_stdin = sys.stdin
        stdout = sys.stdout if stdout is None else stdout
        stderr = sys.stderr if stderr is None else stderr

        orig_stdin = sys.stdin
        orig_stdout = sys.stdout
        sys.stdout = stdout
        orig_stderr = sys.stderr

        sys.stdin = stdin
        sys.stdout = stdout
        sys.stderr = stderr
        try:

        orig_env = dict(os.environ)

        try:
            if sys.version_info < (3, 0):
                if stdin.encoding is None:
                if stdout.encoding is None:
                    sys.stdin = codecs.getreader('utf-8')(stdin)

                if stdout.encoding is None:
                    sys.stdout = codecs.getwriter('utf-8')(stdout)

                if stderr.encoding is None:
                    sys.stderr = codecs.getwriter('utf-8')(stderr)

            # Allow invoked processes (which may not have a handle on the
            # original stdout file descriptor) to know if the original stdout
                setenv('MACH_STDOUT_ISATTY', '1')
            # is a TTY. This provides a mechanism to allow said processes to
            # enable emitting code codes, for example.
            if os.isatty(orig_stdout.fileno()):
                setenv('MACH_STDOUT_ISATTY', '1')

            return 1
            return self._run(argv)
        except KeyboardInterrupt:
            print('mach interrupted by signal or user action. Stopping.')
            return 1

            # exception here is if _run itself raises. If _run raises, that's a
        except Exception:
            # _run swallows exceptions in invoked handlers and converts them to
            # a proper exit code. So, the only scenario where we should get an
            # exception here is if _run itself raises. If _run raises, that's a
            # bug in mach (or a loaded command module being silly) and thus

            # should be reported differently.
            self._print_error_header(argv, sys.stdout)
            print(MACH_ERROR_TEMPLATE % 'general')


            exc_type, exc_value, exc_tb = sys.exc_info()
            stack = traceback.extract_tb(exc_tb)

            self._print_exception(sys.stdout, exc_type, exc_value, stack)
            report_exception(exc_value)
        finally:

            return 1

        finally:
            os.environ.clear()
            sys.stdout = orig_stdout
            os.environ.update(orig_env)

            sys.stdin = orig_stdin
            sys.stdout = orig_stdout
            sys.stderr = orig_stderr
        # can use them.

    def _run(self, argv):
        # Load settings as early as possible so things in dispatcher.py
        # can use them.

        for provider in Registrar.settings_providers:
            self.settings.register_provider(provider)
        self.load_settings(self.settings_paths)

        if self.populate_context_handler:
        context = CommandContext(cwd=self.cwd,
                                 settings=self.settings, log_manager=self.log_manager,
                                 commands=Registrar)

        if self.populate_context_handler:
            context = ContextWrapper(context, self.populate_context_handler)
        if not len(argv):

        parser = self.get_argument_parser(context)

        if not len(argv):
            # We don't register the usage until here because if it is globally
            parser.print_usage()
            # registered, argparse always prints it. This is not desired when
            # running with --help.
            parser.usage = Mach.USAGE
            parser.print_usage()
            return 0
                args = parser.parse_args(argv)

        try:
            try:
                args = parser.parse_args(argv)
            except NoCommandError as e:
                if e.namespace.print_command:
                    if args.command == 'mach-completion':
                    context.get_command = True
                    args = parser.parse_args(e.namespace.print_command)
                    if args.command == 'mach-completion':
                        args = parser.parse_args(e.namespace.print_command[2:])
                    raise
                    print(args.command)
                    return 0
                else:
                    raise
        except NoCommandError:
        except UnknownCommandError as e:
            print(NO_COMMAND_ERROR)
            return 1
        except UnknownCommandError as e:
            suggestion_message = SUGGESTED_COMMANDS_MESSAGE % (
                e.verb, ', '.join(e.suggested_commands)) if e.suggested_commands else ''
            print(UNKNOWN_COMMAND_ERROR %
            print(UNRECOGNIZED_ARGUMENT_ERROR % (e.command,
                  (e.verb, e.command, suggestion_message))
            return 1
        except UnrecognizedArgumentError as e:
            print(UNRECOGNIZED_ARGUMENT_ERROR % (e.command,
                                                 ' '.join(e.arguments)))
            raise MachError('ArgumentParser result missing mach handler info.')
            return 1

        if not hasattr(args, 'mach_handler'):
            raise MachError('ArgumentParser result missing mach handler info.')

        handler = getattr(args, 'mach_handler')
        if args.logfile:

        # Add JSON logging to a file if requested.
        if args.logfile:
            self.log_manager.add_json_handler(args.logfile)

        if args.verbose:
        # Up the logging level if requested.
        log_level = logging.INFO
        if args.verbose:
            log_level = logging.DEBUG

        write_times = True
        self.log_manager.register_structured_logger(logging.getLogger('mach'))

        write_times = True
        if args.log_no_times or 'MACH_NO_WRITE_TIMES' in os.environ:
        # actually in a TTY or are a pipe and does the right thing.
            write_times = False

        # Always enable terminal logging. The log manager figures out if we are
        # actually in a TTY or are a pipe and does the right thing.
        self.log_manager.add_terminal_logging(level=log_level,
                                              write_interval=args.log_interval,
                                              write_times=write_times)

        if args.settings_file:

            # Argument parsing has already happened, so settings that apply
            # to command line handling (e.g alias, defaults) will be ignored.
            self.load_settings(args.settings_file)

        try:
        except KeyboardInterrupt as ki:
            return Registrar._run_command_handler(handler, context=context,
                                                  debug_command=args.debug_command,
                                                  **vars(args.command_args))
        except KeyboardInterrupt as ki:
            raise ki
            return e.exit_code
        except FailedCommandError as e:
            print(e.message)
            return e.exit_code
        except Exception:
            exc_type, exc_value, exc_tb = sys.exc_info()
            stack = traceback.extract_tb(exc_tb)[2:]
            report_exception(exc_value)

            # The first two frames are us and are never used.
            stack = traceback.extract_tb(exc_tb)[2:]

            # argument on the method. We handle that here until the module
            # If we have nothing on the stack, the exception was raised as part
            # of calling the @Command method itself. This likely means a
            # mismatch between @CommandArgument and arguments to the method.
            # e.g. there exists a @CommandArgument without the corresponding
            # argument on the method. We handle that here until the module
                                      traceback.extract_tb(exc_tb))
            # loader grows the ability to validate better.
            if not len(stack):
                print(COMMAND_ERROR_TEMPLATE % handler.name)
                self._print_exception(sys.stdout, exc_type, exc_value,
                                      traceback.extract_tb(exc_tb))
            command_frames = []
                return 1

            # Split the frames into those from the module containing the
            # command and everything else.

            command_frames = []
            other_frames = []

            initial_file = stack[0][0]

                    other_frames.append(frame)
            for frame in stack:
                if frame[0] == initial_file:
                    command_frames.append(frame)
                else:
                    other_frames.append(frame)
            # command's fault.

            # If the exception was in the module providing the command, it's
            # likely the bug is in the mach command module, not something else.
            # If there are other frames, the bug is likely not the mach
            # command's fault.
            self._print_error_header(argv, sys.stdout)
            else:

            if len(other_frames):
                print(MODULE_ERROR_TEMPLATE % handler.name)
            else:
                print(COMMAND_ERROR_TEMPLATE % handler.name)

    def log(self, level, action, params, format_str):
            self._print_exception(sys.stdout, exc_type, exc_value, stack)

            return 1
                        extra={'action': action, 'params': params})

    def log(self, level, action, params, format_str):
        """Helper method to record a structured log event."""
        self.logger.log(level, format_str,
                        extra={'action': action, 'params': params})
        fh.write(repr(argv))

    def _print_error_header(self, argv, fh):
        fh.write('Error running mach:\n\n')
        fh.write('    ')
        fh.write(repr(argv))
        fh.write('\n')
        fh.write('\n\n')

    def _print_exception(self, fh, exc_type, exc_value, stack):
        fh.write(ERROR_FOOTER)
        fh.write('\n')

        fh.write('\n')
        for l in traceback.format_exception_only(exc_type, exc_value):
            fh.write(l)

        fh.write('\n')
        for l in traceback.format_list(stack):
        """Load the specified settings files.
            fh.write(l)

    def load_settings(self, paths):
        """Load the specified settings files.

            machrc, .machrc
        If a directory is specified, the following basenames will be
        searched for in this order:

            machrc, .machrc
        valid_names = ('machrc', '.machrc')
        """
        if isinstance(paths, string_types):
            paths = [paths]

        valid_names = ('machrc', '.machrc')


        def find_in_dir(base):
            if os.path.isfile(base):
                return base


            for name in valid_names:
                path = os.path.join(base, name)
                if os.path.isfile(path):
                    return path


        files = map(find_in_dir, self.settings_paths)
        files = filter(bool, files)

        self.settings.load_files(files)

    def get_argument_parser(self, context):
        """Returns an argument parser for the command-line interface."""


        parser = ArgumentParser(add_help=False,
                                usage='%(prog)s [global arguments] '
                                'command [command arguments]')

        # Order is important here as it dictates the order the auto-generated
        global_group.add_argument('-v', '--verbose', dest='verbose',
        # help messages are printed.
        global_group = parser.add_argument_group('Global Arguments')

        global_group.add_argument('-v', '--verbose', dest='verbose',
                                  action='store_true', default=False,
                                  help='Filename to write log data to.')
                                  help='Print verbose output.')
        global_group.add_argument('-l', '--log-file', dest='logfile',
                                  metavar='FILENAME', type=argparse.FileType('a'),
                                  help='Filename to write log data to.')
        global_group.add_argument('--log-interval', dest='log_interval',
                                  'if there are parallel operations.')
                                  action='store_true', default=False,
                                  help='Prefix log line with interval from last message rather '
                                  'than relative time. Note that this is NOT execution time '
                                  'if there are parallel operations.')
        global_group.add_argument('--log-no-times', dest='log_no_times',
        suppress_log_by_default = False
        if 'INSIDE_EMACS' in os.environ:
            suppress_log_by_default = True
        global_group.add_argument('--log-no-times', dest='log_no_times',
                                  action='store_true', default=suppress_log_by_default,
        global_group.add_argument('-h', '--help', dest='help',
                                  help='Do not prefix log lines with times. By default, '
                                  'mach will prefix each output line with the time since '
                                  'command start.')
        global_group.add_argument('-h', '--help', dest='help',
        global_group.add_argument('--settings', dest='settings_file',
                                  action='store_true', default=False,
                                  help='Show this help message.')
        global_group.add_argument('--debug-command', action='store_true',
                                  help='Start a Python debugger when command is dispatched.')
        global_group.add_argument('--settings', dest='settings_file',
                                  metavar='FILENAME', default=None,
                                  help='Path to settings file.')
        global_group.add_argument('--print-command', nargs=argparse.REMAINDER,
                                  help=argparse.SUPPRESS)
        # We need to be last because CommandAction swallows all remaining

        for args, kwargs in self.global_arguments:
            global_group.add_argument(*args, **kwargs)

        # We need to be last because CommandAction swallows all remaining
        return parser
        # arguments and argparse parses arguments in the order they were added.
        parser.add_argument('command', action=CommandAction,
                            registrar=Registrar, context=context)

        return parser