From cd0f1b4d0c6552a2bdfd21a30482516d51971dc8 Mon Sep 17 00:00:00 2001 From: John R Dietrick Date: Thu, 10 Sep 2015 20:25:15 +0800 Subject: [PATCH] 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. --- configurations/importer.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/configurations/importer.py b/configurations/importer.py index 66cff61..1dad4d7 100644 --- a/configurations/importer.py +++ b/configurations/importer.py @@ -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,