diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
index 688340e..8d39b86 100644
a
|
b
|
class ManagementUtility:
|
311 | 311 | # Preprocess options to extract --settings and --pythonpath. |
312 | 312 | # These options could affect the commands that are available, so they |
313 | 313 | # 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) |
315 | 315 | parser.add_argument('--settings') |
316 | 316 | parser.add_argument('--pythonpath') |
317 | 317 | parser.add_argument('args', nargs='*') # catch-all |
diff --git a/django/core/management/base.py b/django/core/management/base.py
index 41b6b0f..1d41efc 100644
a
|
b
|
class CommandParser(ArgumentParser):
|
42 | 42 | SystemExit in several occasions, as SystemExit is unacceptable when a |
43 | 43 | command is called programmatically. |
44 | 44 | """ |
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) |
47 | 48 | super().__init__(**kwargs) |
48 | 49 | |
49 | 50 | def parse_args(self, args=None, namespace=None): |
50 | 51 | # Catch missing argument for a better error message |
51 | | if (hasattr(self.cmd, 'missing_args_message') and |
| 52 | if (self.missing_args_message and |
52 | 53 | 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) |
54 | 55 | return super().parse_args(args, namespace) |
55 | 56 | |
56 | 57 | def error(self, message): |
57 | | if self.cmd._called_from_command_line: |
| 58 | if self.called_from_command_line: |
58 | 59 | super().error(message) |
59 | 60 | else: |
60 | 61 | raise CommandError("Error: %s" % message) |
… |
… |
class BaseCommand:
|
225 | 226 | parse the arguments to this command. |
226 | 227 | """ |
227 | 228 | parser = CommandParser( |
228 | | self, prog="%s %s" % (os.path.basename(prog_name), subcommand), |
| 229 | prog='%s %s' % (os.path.basename(prog_name), subcommand), |
229 | 230 | 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), |
230 | 233 | ) |
| 234 | |
231 | 235 | parser.add_argument('--version', action='version', version=self.get_version()) |
232 | 236 | parser.add_argument( |
233 | 237 | '-v', '--verbosity', action='store', dest='verbosity', default=1, |