I'm trying to build a management command for Django and I've run into an issue. It seems that the option_list
variable needs to be a flattened list of options.
Here's the code — edited for brevity — that's executed:
def add_options(self, parser):
group = OptionGroup(parser, "Global Options")
group.add_option("--logfile", metavar="FILE", \
help="log file. if omitted stderr will be used")
...
...
...
group.add_option("-s", "--set", action="append", default=[], metavar="NAME=VALUE", \
help="set/override setting (may be repeated)")
parser.add_option_group(group)
parser.add_option("-t", "--output-format", metavar="FORMAT", default="jsonlines", \
help="format to use for dumping items with -o (default: %default)")
I need to take all the options parser
variable, flatted then i.e. remove the OptionGroup
, while keeping the options and put them into a new variable.
Django needs a class to specify it's options like this so it can iterate over it.
option_list = (
make_option('-v', '--verbosity', action='store', dest='verbosity', default='1',
type='choice', choices=['0', '1', '2', '3'],
help='Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output'),
make_option('--settings',
help='The Python path to a settings module, e.g. "myproject.settings.main". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.'),
make_option('--pythonpath',
help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".'),
make_option('--traceback', action='store_true',
help='Print traceback on exception'),
)
I'm very lost with how to accomplish this.
You can get the option using the
option_list
attribute:Unfortunately, that will miss out on the option group. For that, you will have to iterate over the groups in addition. Then, you can do something like (untested):
That will lose the grouping of options, but if you need that, you can probably fiddle around with things to get there.
In short, just use the
option_list
andoption_groups
attributes. How to find out yourself: usedir(parser)
and look for the most applicable attributes; then it's a bit of trial and error.