Restored command error behavior when called from command line
Refs #19973.
This commit is contained in:
parent
2ca5fc55b0
commit
5949c2118d
|
@ -52,11 +52,14 @@ class CommandParser(ArgumentParser):
|
||||||
# Catch missing argument for a better error message
|
# Catch missing argument for a better error message
|
||||||
if (hasattr(self.cmd, 'missing_args_message') and
|
if (hasattr(self.cmd, 'missing_args_message') and
|
||||||
not (args or any([not arg.startswith('-') for arg in args]))):
|
not (args or any([not arg.startswith('-') for arg in args]))):
|
||||||
raise CommandError("Error: %s" % self.cmd.missing_args_message)
|
self.error(self.cmd.missing_args_message)
|
||||||
return super(CommandParser, self).parse_args(args, namespace)
|
return super(CommandParser, self).parse_args(args, namespace)
|
||||||
|
|
||||||
def error(self, message):
|
def error(self, message):
|
||||||
raise CommandError("Error: %s" % message)
|
if self.cmd._called_from_command_line:
|
||||||
|
super(CommandParser, self).error(message)
|
||||||
|
else:
|
||||||
|
raise CommandError("Error: %s" % message)
|
||||||
|
|
||||||
|
|
||||||
def handle_default_options(options):
|
def handle_default_options(options):
|
||||||
|
@ -208,6 +211,7 @@ class BaseCommand(object):
|
||||||
args = ''
|
args = ''
|
||||||
|
|
||||||
# Configuration shortcuts that alter various logic.
|
# Configuration shortcuts that alter various logic.
|
||||||
|
_called_from_command_line = False
|
||||||
can_import_settings = True
|
can_import_settings = True
|
||||||
output_transaction = False # Whether to wrap the output in a "BEGIN; COMMIT;"
|
output_transaction = False # Whether to wrap the output in a "BEGIN; COMMIT;"
|
||||||
leave_locale_alone = False
|
leave_locale_alone = False
|
||||||
|
@ -338,6 +342,7 @@ class BaseCommand(object):
|
||||||
to stderr. If the ``--traceback`` option is present or the raised
|
to stderr. If the ``--traceback`` option is present or the raised
|
||||||
``Exception`` is not ``CommandError``, raise it.
|
``Exception`` is not ``CommandError``, raise it.
|
||||||
"""
|
"""
|
||||||
|
self._called_from_command_line = True
|
||||||
parser = self.create_parser(argv[0], argv[1])
|
parser = self.create_parser(argv[0], argv[1])
|
||||||
|
|
||||||
if self.use_argparse:
|
if self.use_argparse:
|
||||||
|
|
|
@ -1494,7 +1494,7 @@ class CommandTypes(AdminScriptTestCase):
|
||||||
"NoArg Commands raise an error if an argument is provided"
|
"NoArg Commands raise an error if an argument is provided"
|
||||||
args = ['noargs_command', 'argument']
|
args = ['noargs_command', 'argument']
|
||||||
out, err = self.run_manage(args)
|
out, err = self.run_manage(args)
|
||||||
self.assertOutput(err, "Error: unrecognized arguments: argument")
|
self.assertOutput(err, "error: unrecognized arguments: argument")
|
||||||
|
|
||||||
def test_app_command(self):
|
def test_app_command(self):
|
||||||
"User AppCommands can execute when a single app name is provided"
|
"User AppCommands can execute when a single app name is provided"
|
||||||
|
@ -1508,7 +1508,7 @@ class CommandTypes(AdminScriptTestCase):
|
||||||
"User AppCommands raise an error when no app name is provided"
|
"User AppCommands raise an error when no app name is provided"
|
||||||
args = ['app_command']
|
args = ['app_command']
|
||||||
out, err = self.run_manage(args)
|
out, err = self.run_manage(args)
|
||||||
self.assertOutput(err, 'Error: Enter at least one application label.')
|
self.assertOutput(err, 'error: Enter at least one application label.')
|
||||||
|
|
||||||
def test_app_command_multiple_apps(self):
|
def test_app_command_multiple_apps(self):
|
||||||
"User AppCommands raise an error when multiple app names are provided"
|
"User AppCommands raise an error when multiple app names are provided"
|
||||||
|
@ -1642,9 +1642,10 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
|
||||||
]
|
]
|
||||||
|
|
||||||
def test_wrong_args(self):
|
def test_wrong_args(self):
|
||||||
"Make sure passing the wrong kinds of arguments raises a CommandError"
|
"Make sure passing the wrong kinds of arguments outputs an error and prints usage"
|
||||||
out, err = self.run_django_admin(['startproject'])
|
out, err = self.run_django_admin(['startproject'])
|
||||||
self.assertNoOutput(out)
|
self.assertNoOutput(out)
|
||||||
|
self.assertOutput(err, "usage:")
|
||||||
self.assertOutput(err, "You must provide a project name.")
|
self.assertOutput(err, "You must provide a project name.")
|
||||||
|
|
||||||
def test_simple_project(self):
|
def test_simple_project(self):
|
||||||
|
|
Loading…
Reference in New Issue