從OPTPARSE庫引用:Python的optparse make_option()相當於argparse
option_list = [
make_option("-f", "--filename",
action="store", type="string", dest="filename"),
make_option("-q", "--quiet",
action="store_false", dest="verbose"),
]
parser = OptionParser(option_list=option_list)
像上面的例子,我想用make_option做一個選項列表,並將其傳遞給該實例化一個裝飾解析器並添加參數。
這怎麼能在argparse中實現?有沒有一種方法來填充parse_args()以外的解析器?
你說的意思 「它傳遞給裝飾」。我在這裏看不到任何裝飾器......另外,我不認爲'parse_args'填充瞭解析器。 'add_argument'填充解析器。 – mgilson
有趣的是,不推薦使用'option_list',並建議在創建解析器之後使用'add_option()'。「 http://docs.python.org/2/library/optparse.html#creating-the-parser – bernie