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

"Joe Friedrichsen" <[email protected]> Tue, 19 Jun 2007 23:13:51 -0600
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
On 6/19/07, Joe Friedrichsen <[email protected]> wrote:
>
> 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 :-)

Looks like it couldn't. The parser isn't updating the option object
when it calls the callback function: it's stuck on whichever option
was added last.

I changed the -c, -p, and -a options to action=callback and
callback=variable_args, and the parser broke again. When execution
enters my callback function, option.dest is _always_ the same. The
correct option is not being passed. How can I fix it?

I've included the updated code and console output again. I added the
-a option last, and each time variable_args is triggered, option.dest
is 'authors' and does not correspond to the option that triggered it.

Thanks,
Joe

#########
######### Code
#########

    def variable_args(option, opt_str, value, parser):
        """Parse options that have a variable number of arguments

        """
        me = 'variable_args'
        assert value is None
        value = []
        rargs = parser.rargs

        # Group all of the parser's options
        all_options = []
        for option in parser.option_list:
            all_options = all_options + option._short_opts + option._long_opts

        while rargs:
            arg = rargs[0]

            # Stop if we hit an arg in the parser
            if arg in all_options:
                break
            else:
                value.append(arg)
                del rargs[0]

        print "%s: updating parser.values dest=%s with values %s" % \
            (me, option.dest, value)
        setattr(parser.values, option.dest, value)

    # 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("-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.add_option("-p", "--posts",
            help="Posts to translate (comma-separated list of IDs or URLs)",
            metavar="'POST, LIST[, ...]'", dest="posts",
            action="callback", callback=variable_args)
    opt_parser.add_option("-c", "--categories",
            help="Categories to add posts to (commma-separated list)",
            metavar="'CATEGORY, LIST[, ...]'", dest="categories",
            action="callback", callback=variable_args)
    opt_parser.add_option("-a", "--authors",
            help="Author mappings (comma-separated list)",
            metavar="BB_AUTH:WP_AUTH[, ...]", dest="authors",
            action="callback", callback=variable_args)
    opt_parser.set_defaults(verbosity=1)

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

#########
######### Console
#########

$ ./phpbb2wp.py -c "Travel" "Teaching" -t 24 68 76 -a "BB Author 1: WP
Author" "BB Author 2: WP Author"
variable_args: updating parser.values dest=authors with values
['Travel', 'Teaching']
variable_args: updating parser.values dest=authors with values ['24',
'68', '76']
variable_args: updating parser.values dest=authors with values ['BB
Author 1: WP Author', 'BB Author 2: WP Author']
main: {'threads': None, 'verbosity': 1, 'posts': None, 'categories':
None, 'authors': ['BB Author 1: WP Author', 'BB Author 2: WP Author']}

-------------------------------------------------------------------------
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/