Ticket #25680: add_command_option_to_shell.diff.txt

File add_command_option_to_shell.diff.txt, 2.6 KB (added by Niels Van Och, 9 years ago)
Line 
1diff --git a/django/core/management/commands/shell.py b/django/core/management/commands/shell.py
2index d8bded0..880114f 100644
3--- a/django/core/management/commands/shell.py
4+++ b/django/core/management/commands/shell.py
5@@ -15,6 +15,8 @@ def add_arguments(self, parser):
6 help='When using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.')
7 parser.add_argument('-i', '--interface', choices=self.shells, dest='interface',
8 help='Specify an interactive interpreter interface. Available options: "ipython" and "bpython"')
9+ parser.add_argument('-c', '--command', dest='command',
10+ help='Instead of opening an interactive shell, run one command as Django and exit.')
11
12 def _ipython_pre_011(self):
13 """Start IPython pre-0.11"""
14@@ -62,6 +64,11 @@ def run_shell(self, shell=None):
15
16 def handle(self, **options):
17 try:
18+ # Just execute this command and get out.
19+ if options['command']:
20+ exec(options['command'])
21+ return
22+
23 if options['plain']:
24 # Don't bother loading IPython, because the user wants plain Python.
25 raise ImportError
26diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
27index 8d45439..6a99666 100644
28--- a/docs/ref/django-admin.txt
29+++ b/docs/ref/django-admin.txt
30@@ -966,6 +966,12 @@ behavior you can use the ``--no-startup`` option. e.g.::
31
32 django-admin shell --plain --no-startup
33
34+.. versionadded:: 1.10
35+
36+You can also pass a command as a string to execute it as Django, like so::
37+
38+ django-admin shell --command="import django; print(django.__version__)"
39+
40 showmigrations [<app_label> [<app_label>]]
41 ------------------------------------------
42
43diff --git a/tests/shell/__init__.py b/tests/shell/__init__.py
44new file mode 100644
45index 0000000..e69de29
46diff --git a/tests/shell/tests.py b/tests/shell/tests.py
47new file mode 100644
48index 0000000..ae3a4e2
49--- /dev/null
50+++ b/tests/shell/tests.py
51@@ -0,0 +1,15 @@
52+from django import __version__
53+from django.core.management import call_command
54+from django.test import SimpleTestCase
55+from django.test.utils import patch_logger
56+
57+
58+class ShellCommandTestCase(SimpleTestCase):
59+
60+ def test_command_option(self):
61+ with patch_logger('test', 'info') as logger:
62+ call_command('shell',
63+ command='import django; from logging import getLogger;'
64+ 'getLogger("test").info(django.__version__)')
65+ self.assertEqual(len(logger), 1)
66+ self.assertEqual(logger[0], __version__)
Back to Top