Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python optparse Values Instance

How can I take the opt result of

opt, args = parser.parse_args() 

and place it in a dict? Python calls opt a "Values Instance" and I can't find any way to turn a Values Instance into a list or dict. One can't copy items from opt in this way,

for i in opt:  myDict[i] = opt[i] 

instead, its a clumsy,

myDict[parm1] = opt.parm1 myDict[parm2] = opt.parm2 

which implies that every time I add an option, I have to update this code as well; there should be a way to let this take care of itself.

like image 752
mgag Avatar asked Nov 18 '09 03:11

mgag


People also ask

Is Optparse deprecated?

Deprecated since version 3.2: The optparse module is deprecated and will not be developed further; development will continue with the argparse module. optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module.

What is import Optparse in Python?

Optparse module makes easy to write command-line tools. It allows argument parsing in the python program. optparse make it easy to handle the command-line argument. It comes default with python. It allows dynamic data input to change the output.


1 Answers

options, args = parser.parse_args() option_dict = vars(options) 

(Source is this python-ideas post.)

like image 62
bcat Avatar answered Sep 27 '22 20:09

bcat