By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

optparse: list out entered values

P: n/a
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.

I could use dir(options) but it's kinda ugly and would require
eliminating the "unwanted" variables.

print dir(options)

['__doc__', '__eq__', '__init__', '__module__', '__ne__', '__repr__',
'__str__', '_update', '_update_careful', '_update_loose',
'ensure_value', 'ex', 'read_file', 'read_module']

Any help would be greatly appreciated.

Nirnimesh

May 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 25, 3:07 am, Nirnimesh <nirnim...@gmail.comwrote:
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.

I could use dir(options) but it's kinda ugly and would require
eliminating the "unwanted" variables.

print dir(options)

['__doc__', '__eq__', '__init__', '__module__', '__ne__', '__repr__',
'__str__', '_update', '_update_careful', '_update_loose',
'ensure_value', 'ex', 'read_file', 'read_module']

Any help would be greatly appreciated.

Nirnimesh
I dug around with the optparse module and got this:

for key, val in parser.values.__dict__.iteritems():
print key, val

May 25 '07 #2

P: n/a
Nirnimesh wrote:
On May 25, 3:07 am, Nirnimesh <nirnim...@gmail.comwrote:
>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
May 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.