overriding OptionParser's add_option function in python

♀尐吖头ヾ 提交于 2019-12-24 01:42:53

问题


I have written a subclass for Option and OptionParser in optparse. I am overriding the add_option function in OptionParser to make it parse a new keyword. Following is my code.

from optparse import Option, OptionError, OptionParser

class MyOptionParser(OptionParser):
    def add_option(self,*args,**kwargs):
    ##add the new keyword "deft". If deft="some value", kwargs[default] = deft
    ##else process args to get deft
        if kwargs.has_key["deft"]:
            newDef = kwargs["deft"]
            del kwargs["deft"]
        else:
            newDef = process_args_for_deft(args)
        if newDef!= None
            kwargs["default"] = newDef
        modOpt = OptionParser.add_option(self,*args,**kwargs)

class MyOption(Option):
    def _set_opt_strings(self, largs, rargs, values):
        #overridden method over here 

Now if I do this, it works fine.

parser = MyOptionParser()
parser.add_option("-f", "--file", dest="filename",
                 help="write report to FILE", metavar="FILE", deft ="xyz")

But if I do this (which I want to) it gives me an error: MyOption instance has no attribute 'getitem' if def is not specified and if def is specified it gives me an error saying no type like def.

parser = MyOptionParser()
parser.add_option(MyOption("-f", "--file", dest="filename",
                 help="write report to FILE", metavar="FILE", def ="xyz"))

I think the problem is that I am passing an object when I use MyOption (opts,args) and hence it is not able to access the args/kwargs. How do I get around this problem?


回答1:


add_option basically directly calls the constructor for option_class (which is a default argument to the OptionParser constructor that defaults to optparse.Option).

So, you should probably just override MyOption.__init__ instead (without overriding OptionParser.add_option):

class MyOption(Option):
    def __init__(self, *args, **kwargs):
        if 'deft' in kwargs:
            newDef = kwargs.pop('deft')
        else:
            newDef = process_args_for_deft(args)
        if newDef is not None:
            kwargs["default"] = newDef

        Option.__init__(self, *args, **kwargs)

If you want to be able to support syntax like

parser.add_option("-f", "--file", dest="filename",
             help="write report to FILE", metavar="FILE", deft ="xyz")

then just make sure to set option_class when making your OptionParser:

parser = MyOptionParser(option_class=MyOption)


来源:https://stackoverflow.com/questions/15821463/overriding-optionparsers-add-option-function-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!