Ticket #29295: 29295.diff

File 29295.diff, 2.8 KB (added by Tim Graham, 7 years ago)
  • django/core/management/__init__.py

    diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
    index 688340e..8d39b86 100644
    a b class ManagementUtility:  
    311311        # Preprocess options to extract --settings and --pythonpath.
    312312        # These options could affect the commands that are available, so they
    313313        # must be processed early.
    314         parser = CommandParser(None, usage="%(prog)s subcommand [options] [args]", add_help=False)
     314        parser = CommandParser(usage='%(prog)s subcommand [options] [args]', add_help=False)
    315315        parser.add_argument('--settings')
    316316        parser.add_argument('--pythonpath')
    317317        parser.add_argument('args', nargs='*')  # catch-all
  • django/core/management/base.py

    diff --git a/django/core/management/base.py b/django/core/management/base.py
    index 41b6b0f..1d41efc 100644
    a b class CommandParser(ArgumentParser):  
    4242    SystemExit in several occasions, as SystemExit is unacceptable when a
    4343    command is called programmatically.
    4444    """
    45     def __init__(self, cmd, **kwargs):
    46         self.cmd = cmd
     45    def __init__(self, **kwargs):
     46        self.missing_args_message = kwargs.pop('missing_args_message', None)
     47        self.called_from_command_line = kwargs.pop('called_from_command_line', None)
    4748        super().__init__(**kwargs)
    4849
    4950    def parse_args(self, args=None, namespace=None):
    5051        # Catch missing argument for a better error message
    51         if (hasattr(self.cmd, 'missing_args_message') and
     52        if (self.missing_args_message and
    5253                not (args or any(not arg.startswith('-') for arg in args))):
    53             self.error(self.cmd.missing_args_message)
     54            self.error(self.missing_args_message)
    5455        return super().parse_args(args, namespace)
    5556
    5657    def error(self, message):
    57         if self.cmd._called_from_command_line:
     58        if self.called_from_command_line:
    5859            super().error(message)
    5960        else:
    6061            raise CommandError("Error: %s" % message)
    class BaseCommand:  
    225226        parse the arguments to this command.
    226227        """
    227228        parser = CommandParser(
    228             self, prog="%s %s" % (os.path.basename(prog_name), subcommand),
     229            prog='%s %s' % (os.path.basename(prog_name), subcommand),
    229230            description=self.help or None,
     231            missing_args_message=getattr(self, 'missing_args_message', None),
     232            called_from_command_line=getattr(self, '_called_from_command_line', None),
    230233        )
     234
    231235        parser.add_argument('--version', action='version', version=self.get_version())
    232236        parser.add_argument(
    233237            '-v', '--verbosity', action='store', dest='verbosity', default=1,
Back to Top