0

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.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • Sorry, but your question is unclear. Why can't you directly use something like the second code block? Why go through an `OptionGroup` first? –  Oct 23 '12 at 08:26
  • I'm building a management command wrapper around a third-party package and don't want to modify any of their code. – Mridang Agarwalla Oct 23 '12 at 08:32
  • Ok, that also explains why you're stuck with optparse, which has been deprecated and essentially replaced by argparse. –  Oct 23 '12 at 09:02
  • **Note**: Using *optparse* is discouraged since python version 2.7. The optparse module is deprecated and will not be developed further; development will continue with the *argparse* module. See [PEP 0389](http://www.python.org/dev/peps/pep-0389/) for more info. – shakaran Apr 03 '13 at 23:56

2 Answers2

3

You can get the option using the option_list attribute:

>>> print parser.option_list
[<Option at 0x7f938c8243f8: -h/--help>, <Option at 0x7f938c82b3f8: -t/--output-format>]

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):

for group in parser.option_groups:
    option_list += tuple(group.option_list)
option_list += tuple(parser.option_list)

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 and option_groups attributes. How to find out yourself: use dir(parser) and look for the most applicable attributes; then it's a bit of trial and error.

0

You should be able to just add the options like so:

option_list += (
    make_option("--logfile", metavar="FILE", \
        help="log file. if omitted stderr will be used"),
    make_option("-s", "--set", action="append", default=[], metavar="NAME=VALUE", \
        help="set/override setting (may be repeated)"),
)
Marwan Alsabbagh
  • 25,364
  • 9
  • 55
  • 65