Backported LaxOptionParser from Django 1.7 to make it work on 1.8.

This commit is contained in:
Jannis Leidel 2015-01-06 21:16:14 +01:00
parent 5d1a8d7887
commit 41dfcee46c
2 changed files with 63 additions and 2 deletions

View file

@ -5,10 +5,9 @@ import sys
from optparse import make_option
from django.core.exceptions import ImproperlyConfigured
from django.core.management import LaxOptionParser
from django.conf import ENVIRONMENT_VARIABLE as SETTINGS_ENVIRONMENT_VARIABLE
from .utils import uppercase_attributes, reraise
from .utils import uppercase_attributes, reraise, LaxOptionParser
from .values import Value, setup_value
installed = False

View file

@ -60,3 +60,65 @@ def reraise(exc, prefix=None, suffix=None):
suffix = '(' + suffix + ')'
exc.args = ('{0} {1} {2}'.format(prefix, exc.args[0], suffix),) + args[1:]
raise
try:
from django.core.management import LaxOptionParser
except ImportError:
from optparse import OptionParser
class LaxOptionParser(OptionParser):
"""
An option parser that doesn't raise any errors on unknown options.
This is needed because the --settings and --pythonpath options affect
the commands (and thus the options) that are available to the user.
Backported from Django 1.7.x
"""
def error(self, msg):
pass
def print_help(self):
"""Output nothing.
The lax options are included in the normal option parser, so under
normal usage, we don't need to print the lax options.
"""
pass
def print_lax_help(self):
"""Output the basic options available to every command.
This just redirects to the default print_help() behavior.
"""
OptionParser.print_help(self)
def _process_args(self, largs, rargs, values):
"""
Overrides OptionParser._process_args to exclusively handle default
options and ignore args and other options.
This overrides the behavior of the super class, which stop parsing
at the first unrecognized option.
"""
while rargs:
arg = rargs[0]
try:
if arg[0:2] == "--" and len(arg) > 2:
# process a single long option (possibly with value(s))
# the superclass code pops the arg off rargs
self._process_long_opt(rargs, values)
elif arg[:1] == "-" and len(arg) > 1:
# process a cluster of short options (possibly with
# value(s) for the last one only)
# the superclass code pops the arg off rargs
self._process_short_opts(rargs, values)
else:
# it's either a non-default option or an arg
# either way, add it to the args list so we can keep
# dealing with options
del rargs[0]
raise Exception
except: # Needed because we might need to catch a SystemExit
largs.append(arg)