开发者

Python optparse Values Instance

开发者 https://www.devze.com 2022-12-11 14:08 出处:网络
How can I take the opt result of opt, args = parser.parse_args() and place it in a dict?Python cal开发者_Go百科ls opt a \"Values Instance\" and I can\'t find any way to turn a Values Instance into

How can I take the opt result of

opt, args = parser.parse_args()

and place it in a dict? Python cal开发者_Go百科ls 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.


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

(Source is this python-ideas post.)

0

精彩评论

暂无评论...
验证码 换一张
取 消