With a little side of applesauce...

Thursday, February 1, 2007

Python / OptParse - Iterating through arguments

I wanted to attach command line arguments to my configuration object, (which is generated by ConfigObj), but didn't realize that in:

options, args = parser.parse_args()

options is a class. Therefore, to generate a dictionary with the values of these options, I used:

config['Options'] = {}
for opt, value in options.__dict__.items():
config['Options'][opt] = value

To populate config['Options'] with the key/value pairs in options.__dict__.items().

Taken from:

No comments: