Skip to content

Commit e87d23d

Browse files
committed
Update the management command to use argparse for Django >= 1.8
1 parent 35f9abf commit e87d23d

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

devserver/management/commands/runserver.py

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,44 @@ def __init__(self, *args, **kwargs):
4343
httpd.serve_forever()
4444

4545

46+
ADDITIONAL_ARGUMENTS = {
47+
'--werkzeug': dict(
48+
action='store_true',
49+
dest='use_werkzeug',
50+
default=False,
51+
help='Tells Django to use the Werkzeug interactive debugger.'),
52+
'--forked': dict(
53+
action='store_true',
54+
dest='use_forked',
55+
default=False,
56+
help='Use forking instead of threading for multiple web requests.'),
57+
'--dozer': dict(
58+
action='store_true',
59+
dest='use_dozer',
60+
default=False,
61+
help='Enable the Dozer memory debugging middleware.'),
62+
'--wsgi-app': dict(
63+
dest='wsgi_app',
64+
default=None,
65+
help='Load the specified WSGI app as the server endpoint.'),
66+
}
67+
68+
if any(map(lambda app: app in settings.INSTALLED_APPS, STATICFILES_APPS)):
69+
ADDITIONAL_ARGUMENTS.update({
70+
'--nostatic': dict(
71+
dest='use_static_files',
72+
action='store_false',
73+
default=True,
74+
help='Tells Django to NOT automatically serve static files at STATIC_URL.')
75+
})
76+
4677
class Command(BaseCommand):
47-
option_list = BaseCommand.option_list + (
48-
make_option(
49-
'--werkzeug', action='store_true', dest='use_werkzeug', default=False,
50-
help='Tells Django to use the Werkzeug interactive debugger.'),
51-
make_option(
52-
'--forked', action='store_true', dest='use_forked', default=False,
53-
help='Use forking instead of threading for multiple web requests.'),
54-
make_option(
55-
'--dozer', action='store_true', dest='use_dozer', default=False,
56-
help='Enable the Dozer memory debugging middleware.'),
57-
make_option(
58-
'--wsgi-app', dest='wsgi_app', default=None,
59-
help='Load the specified WSGI app as the server endpoint.'),
60-
)
61-
if any(map(lambda app: app in settings.INSTALLED_APPS, STATICFILES_APPS)):
62-
option_list += make_option(
63-
'--nostatic', dest='use_static_files', action='store_false', default=True,
64-
help='Tells Django to NOT automatically serve static files at STATIC_URL.'),
78+
if BaseCommand.option_list:
79+
# Handle Django < 1.8
80+
option_list = BaseCommand.option_list + (
81+
make_option(name, **kwargs)
82+
for name, kwargs in ADDITIONAL_ARGUMENTS.items()
83+
)
6584

6685
help = "Starts a lightweight Web server for development which outputs additional debug information."
6786
args = '[optional port number, or ipaddr:port]'
@@ -77,6 +96,11 @@ def __init__(self):
7796
requires_model_validation = False # Django < 1.7
7897
super(Command, self).__init__()
7998

99+
def add_arguments(self, parser):
100+
super(Command, self).add_arguments(parser)
101+
for name, kwargs in ADDITIONAL_ARGUMENTS.items():
102+
parser.add_argument(name, **kwargs)
103+
80104
def run_from_argv(self, argv):
81105
parser = self.create_parser(argv[0], argv[1])
82106
default_args = getattr(settings, 'DEVSERVER_ARGS', None)
@@ -85,12 +109,24 @@ def run_from_argv(self, argv):
85109
else:
86110
options = None
87111

88-
options, args = parser.parse_args(argv[2:], options)
112+
if getattr(self, 'use_argparse', False):
113+
options = parser.parse_args(argv[2:], options)
114+
cmd_options = vars(options)
115+
args = cmd_options.pop('args', ())
116+
else:
117+
options, args = parser.parse_args(argv[2:], options)
118+
cmd_options = var(options)
89119

90120
handle_default_options(options)
91121
self.execute(*args, **options.__dict__)
92122

93-
def handle(self, addrport='', *args, **options):
123+
def handle(self, *args, **options):
124+
options.pop('addrport', None)
125+
if args:
126+
addrport, args = args[0], args[1:]
127+
else:
128+
addrport, args = '', args
129+
94130
if args:
95131
raise CommandError('Usage is runserver %s' % self.args)
96132

0 commit comments

Comments
 (0)