From: Dieter Faulbaum on 16 Mar 2010 10:30 Hello, is there a better way for cycling through all options than this: (options, args) = parser.parse_args() for opt in options.__dict__.keys(): print opt, ":", options.__dict__[opt] Thanks for any nicer solution -- Dieter Faulbaum
From: Gabriel Genellina on 16 Mar 2010 20:02 En Tue, 16 Mar 2010 11:30:51 -0300, Dieter Faulbaum <Dieter.Faulbaum(a)bessy.de> escribi�: > is there a better way for cycling through all options than this: > > > (options, args) = parser.parse_args() > for opt in options.__dict__.keys(): > print opt, ":", options.__dict__[opt] (I assume parser is an optparse.OptionParser instance) You may rewrite it as: for oname, ovalue in vars(options).iteritems(): print oname, ":", ovalue This assumes that the only instance attributes existing in 'options' are, uh, parsed options from the command line. I could not find such guarantee in the documentation. Another way would be to ask the parser about the options it knows of: for oname in (o.dest for o in parser.option_list if o.dest): print oname, ":", getattr(options, oname, None) -- Gabriel Genellina
|
Pages: 1 Prev: How to add a library path to pythonpath ? Next: Python bindings tutorial |