diff options
author | Hasan Ramezani <hasan.r67@gmail.com> | 2018-04-21 23:04:00 +0430 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2018-04-21 17:33:17 -0400 |
commit | dd68b51e1da54267bde4799fa0d9fbd4290eb8b5 (patch) | |
tree | 31204a0acaf1d4a94dfd683111a88d0f5b0b7572 /django | |
parent | 21420096c4db78ccb8f549a29d662cff870d363c (diff) | |
download | django-dd68b51e1da54267bde4799fa0d9fbd4290eb8b5.tar.gz |
Fixed #29295 -- Fixed management command crash when using subparsers.
Thanks Tim Graham for the fix.
Diffstat (limited to 'django')
-rw-r--r-- | django/core/management/__init__.py | 2 | ||||
-rw-r--r-- | django/core/management/base.py | 15 |
2 files changed, 10 insertions, 7 deletions
diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index 688340e49d..8d39b86472 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -311,7 +311,7 @@ class ManagementUtility: # Preprocess options to extract --settings and --pythonpath. # These options could affect the commands that are available, so they # must be processed early. - parser = CommandParser(None, usage="%(prog)s subcommand [options] [args]", add_help=False) + parser = CommandParser(usage='%(prog)s subcommand [options] [args]', add_help=False) parser.add_argument('--settings') parser.add_argument('--pythonpath') parser.add_argument('args', nargs='*') # catch-all diff --git a/django/core/management/base.py b/django/core/management/base.py index 9461f8654a..45e97c7398 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -42,19 +42,20 @@ class CommandParser(ArgumentParser): SystemExit in several occasions, as SystemExit is unacceptable when a command is called programmatically. """ - def __init__(self, cmd, **kwargs): - self.cmd = cmd + def __init__(self, **kwargs): + self.missing_args_message = kwargs.pop('missing_args_message', None) + self.called_from_command_line = kwargs.pop('called_from_command_line', None) super().__init__(**kwargs) def parse_args(self, args=None, namespace=None): # Catch missing argument for a better error message - if (hasattr(self.cmd, 'missing_args_message') and + if (self.missing_args_message and not (args or any(not arg.startswith('-') for arg in args))): - self.error(self.cmd.missing_args_message) + self.error(self.missing_args_message) return super().parse_args(args, namespace) def error(self, message): - if self.cmd._called_from_command_line: + if self.called_from_command_line: super().error(message) else: raise CommandError("Error: %s" % message) @@ -225,8 +226,10 @@ class BaseCommand: parse the arguments to this command. """ parser = CommandParser( - self, prog="%s %s" % (os.path.basename(prog_name), subcommand), + prog='%s %s' % (os.path.basename(prog_name), subcommand), description=self.help or None, + missing_args_message=getattr(self, 'missing_args_message', None), + called_from_command_line=getattr(self, '_called_from_command_line', None), ) # Add command-specific arguments first so that they appear in the # --help output before arguments common to all commands. |