Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cycling through options

2 views
Skip to first unread message

Dieter Faulbaum

unread,
Mar 16, 2010, 10:30:51 AM3/16/10
to pytho...@python.org

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

Gabriel Genellina

unread,
Mar 16, 2010, 8:02:38 PM3/16/10
to pytho...@python.org
En Tue, 16 Mar 2010 11:30:51 -0300, Dieter Faulbaum
<Dieter....@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

0 new messages