Fix handling of fallback to optparse.OptionParser

By the time the code here runs, it's too late to be modifying
base.BaseCommand.option_list; in fact doing so causes an OptionConflictError if
you later call another management command "recursively" while you're running
the first one.

We want to leave BaseCommand's option_list untouched (it's `()` by default in
Django 1.8+) in case the command we are wrapping has already upgraded to
argparse. BUT, if it hasn't, we'll get an OptionParser back, and can tack our
argument on at the last minute.
This commit is contained in:
John R Dietrick 2015-09-10 20:25:15 +08:00
parent 0ff4c7612d
commit f7f1f734d1

View file

@ -37,9 +37,10 @@ def install(check_options=False):
if isinstance(parser, OptionParser):
# in case the option_list is set the create_parser
# will actually return a OptionParser for backward
# compatibility. It uses BaseCommand.use_argparse
# to decide that, which checks for the option_list list
base.BaseCommand.option_list += configuration_options
# compatibility. In that case we should tack our
# options on to the end of the parser on the way out.
for option in configuration_options:
parser.add_option(option)
else:
# probably argparse, let's not import argparse though
parser.add_argument(CONFIGURATION_ARGUMENT,