Re: callback function: options.dest is None and prevents updating parser.values

"Joe Friedrichsen" <[email protected]> Tue, 19 Jun 2007 22:45:32 -0600
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
On 6/18/07, Joe Friedrichsen <[email protected]> wrote:
> I've added all of the details below, both code and console output. The
> print statements show that things aren't quite right. If anyone has
> any insight about why I would get this behavior and how I can get it
> working correctly, please tell me.

Hi folks,

I have an update -- I was able to solve my problem, but I've
encountered some unexpected behavior.

I fixed the option.dest = None problem by changing the way I set up
the parser. Rather than using make_option and passing an option list
to the constructor when I instantiated my parser (as seen here:
http://www.python.org/doc/2.4.4/lib/optparse-populating-parser.html ),
I instead created my parser object and then used its add_option method
(common in most of the other examples).

However, my arguments were going to the wrong option. The '-t' option
should be adding 24, 68, and 76 to the 'threads' attribute, but the
parser added them to the 'verbosity' attribute instead. This is
unexpected since I didn't even use the -v option:

$ ./phpbb2wp.py -c "Travel" -t 24 68 76 -a "BB Author 1: WP Author, BB
Author 2: WP Author"
variable_args: parser options = ['--version', '-h', '--help', '-t',
'--threads', '-p', '--posts', '-c', '--categories', '-a', '--authors',
'-v', '--verbosity', '--verbose', '-q', '--quiet']
variable_args: rargs = ['24', '68', '76', '-a', 'BB Author 1: WP
Author, BB Author 2: WP Author']
variable_args: adding argument '24'
variable_args: adding argument '68'
variable_args: adding argument '76'
variable_args: breaking... found '-a'
variable_args: parser.values = {'threads': None, 'verbosity': 1,
'posts': None, 'categories': 'Travel', 'authors': None}
variable_args: option.dest = verbosity
variable_args: value = ['24', '68', '76']
variable_args: option.action = store_const
main: {'threads': None, 'verbosity': ['24', '68', '76'], 'posts':
None, 'categories': 'Travel', 'authors': 'BB Author 1: WP Author, BB
Author 2: WP Author'}

So, I tried changing the order in which I added options to the parser,
and I finally got the behavior I wanted. I added the '-t' option last,
and the parser is finally sending things to the right places.

The correct code is:

    # Setup the parser
    opt_parser = OptionParser(usage="%prog [options]", version="%prog 0.20",
            description="Move PHPbb threads/posts to a WordPress blog")
    opt_parser.add_option("-p", "--posts",
            help="Posts to translate (comma-separated list of IDs or URLs)",
            metavar="'POST, LIST[, ...]'", dest="posts")
    opt_parser.add_option("-c", "--categories",
            help="Categories to add posts to (commma-separated list)",
            metavar="'CATEGORY, LIST[, ...]'", dest="categories")
    opt_parser.add_option("-a", "--authors",
            help="Author mappings (comma-separated list)",
            metavar="BB_AUTH:WP_AUTH[, ...]", dest="authors", )
    opt_parser.add_option("-v", "--verbosity",
            help="Set the amount of output messages given", metavar="NUM",
            dest="verbosity", type="int")
    opt_parser.add_option("--verbose",
            help="Set verbosity to 5",
            dest="verbosity", action="store_const", const=5)
    opt_parser.add_option("-q", "--quiet",
            help="Set verbosity to 0 (only error messages)",
            dest="verbosity", action="store_const", const=0)
    opt_parser.add_option("-t", "--threads",
        help="Threads to translate (comma-separated list of IDs or URLs)",
        metavar="'THREAD, LIST[, ...]'", dest="threads",
        action="callback", callback=variable_args)
    opt_parser.set_defaults(verbosity=1)

    # Grab the command line options
    (opts, args) = opt_parser.parse_args()

If anyone has any ideas about why this is happening, please tell me.
Now that I have the behavior that I want, I'll be changing the -c, -p,
and -a options to this same callback function, so I hope the parser
can keep up :-)

Thanks,
Joe

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/