Python optparse Values Instance

PythonDictionaryOptparse

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

(Source is this python-ideas post.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmgagView Question on Stackoverflow
Solution 1 - PythonbcatView Answer on Stackoverflow