Fixed #28398 -- Added suggestions for mistyped management commands.
This commit is contained in:
parent
f7b46f0b58
commit
33ac036a6b
|
@ -3,6 +3,7 @@ import os
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import sys
|
import sys
|
||||||
from collections import OrderedDict, defaultdict
|
from collections import OrderedDict, defaultdict
|
||||||
|
from difflib import get_close_matches
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
import django
|
import django
|
||||||
|
@ -203,10 +204,11 @@ class ManagementUtility:
|
||||||
settings.INSTALLED_APPS
|
settings.INSTALLED_APPS
|
||||||
else:
|
else:
|
||||||
sys.stderr.write("No Django settings specified.\n")
|
sys.stderr.write("No Django settings specified.\n")
|
||||||
sys.stderr.write(
|
possible_matches = get_close_matches(subcommand, commands)
|
||||||
"Unknown command: %r\nType '%s help' for usage.\n"
|
sys.stderr.write('Unknown command: %r' % subcommand)
|
||||||
% (subcommand, self.prog_name)
|
if possible_matches:
|
||||||
)
|
sys.stderr.write('. Did you mean %s?' % possible_matches[0])
|
||||||
|
sys.stderr.write("\nType '%s help' for usage.\n" % self.prog_name)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if isinstance(app_name, BaseCommand):
|
if isinstance(app_name, BaseCommand):
|
||||||
# If the command is already loaded, use it directly.
|
# If the command is already loaded, use it directly.
|
||||||
|
|
|
@ -2256,3 +2256,23 @@ class MainModule(AdminScriptTestCase):
|
||||||
def test_program_name_in_help(self):
|
def test_program_name_in_help(self):
|
||||||
out, err = self.run_test('-m', ['django', 'help'])
|
out, err = self.run_test('-m', ['django', 'help'])
|
||||||
self.assertOutput(out, "Type 'python -m django help <subcommand>' for help on a specific subcommand.")
|
self.assertOutput(out, "Type 'python -m django help <subcommand>' for help on a specific subcommand.")
|
||||||
|
|
||||||
|
|
||||||
|
class DjangoAdminSuggestions(AdminScriptTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.write_settings('settings.py')
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.remove_settings('settings.py')
|
||||||
|
|
||||||
|
def test_suggestions(self):
|
||||||
|
args = ['rnserver', '--settings=test_project.settings']
|
||||||
|
out, err = self.run_django_admin(args)
|
||||||
|
self.assertNoOutput(out)
|
||||||
|
self.assertOutput(err, "Unknown command: 'rnserver'. Did you mean runserver?")
|
||||||
|
|
||||||
|
def test_no_suggestions(self):
|
||||||
|
args = ['abcdef', '--settings=test_project.settings']
|
||||||
|
out, err = self.run_django_admin(args)
|
||||||
|
self.assertNoOutput(out)
|
||||||
|
self.assertNotInOutput(err, 'Did you mean')
|
||||||
|
|
Loading…
Reference in New Issue