Re: callback function: options.dest is None and prevents updating parser.values
"Joe Friedrichsen" <[email protected]> Wed, 20 Jun 2007 12:11:47 -0600
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Message-ID | <[email protected]> |
And so the drama for one ends :-) I was able to resolve my bug after a good rest and some further digging into the optparse module. I chased the bug through the module and it ran back into my callback function. On 6/19/07, Joe Friedrichsen <[email protected]> wrote: > > 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? By not using the same variable name for different things. I used (and thereby re-set) the variable 'option' when finding the parser's options. By changing the variable name used in the for loop to 'opt', option.dest remained the correct destination. Thanks for your patience, Joe The now bug-free code (with intelligent variable-length argument parsing): def variable_args(option, opt_str, value, parser): """Parse options that have a variable number of arguments """ assert value is None value = [] rargs = parser.rargs # Group all of the parser's options all_options = [] for opt in parser.option_list: all_options = all_options + opt._short_opts + opt._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] setattr(parser.values, option.dest, value) ------------------------------------------------------------------------- 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/