Python:无法获取通过';添加的选项列表;添加选项';?
我的代码部分如下所示:Python:无法获取通过';添加的选项列表;添加选项';?,python,python-2.x,Python,Python 2.x,我的代码部分如下所示: parser.add_option("-h", "--help","-?", action = "help", help= """Print the help of the scipt""" ) 当我试图打印脚本可用的选项时,它返回一个空数组 optlist = [x.get_opt_string() for x in parser._get_all_op
parser.add_option("-h", "--help","-?",
action = "help",
help= """Print the help of the scipt"""
)
当我试图打印脚本可用的选项时,它返回一个空数组
optlist = [x.get_opt_string() for x in parser._get_all_options()[1:]]
print optlist
打印optlist打印空数组->
[…]
我需要打印一个包含所有可用选项的数组。在这种情况下,在Python2.6.5 optpasse对象中存储值:-h、-help和-?的数组具有未记录的属性&u short\u opts和&u long\u opts。为了一份颠簸的名单
[x._short_opts + x._long_opts for x in parser._get_all_options()]
使用将列表展平
sum([x._short_opts + x._long_opts for x in parser._get_all_options()],[])
您的
解析器
对象是什么类型的argparse
模块提供自动生成的帮助和用法消息,因此您无需添加这些消息->我无法让您的代码运行argparse
或optparse
--argparse
没有add_选项
并且optparse
与“冲突的选项字符串”一起消失在外部使用带有下划线的属性是可疑的(它们可以在不通知的情况下更改,从而破坏您的代码)