optparse: list out entered values

Steven Bethard steven.bethard at gmail.com
Fri May 25 14:14:55 EDT 2007


Nirnimesh wrote:
> On May 25, 3:07 am, Nirnimesh <nirnim... at gmail.com> wrote:
>> I'm using optparse.OptionParser for parsing command line arguments.
>>
>>  parser = optparse.OptionParser()
>>   parser.add_option("-x", "--xample", help="example",
>> default="nothing",
>>       dest="ex")
>>   options = parser.parse_args()[0]
>>
>> python example.py -x value
>>
>> I'm in search of a method to list out all the entered options along
>> with the values. something which gives me:
>>   ex => value
>>   help => example
>>   version => blah blah..
>> I expected such a method to be there already.
[snip]
> I dug around with the optparse module and got this:
> 
> for key, val in parser.values.__dict__.iteritems():
>     print key, val

I would tend to write this as::

     for key, val in vars(options).iteritems():
         print key, val

STeVe



More information about the Python-list mailing list