Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:无法获取通过';添加的选项列表;添加选项';?_Python_Python 2.x - Fatal编程技术网

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
与“冲突的选项字符串”一起消失在外部使用带有下划线的属性是可疑的(它们可以在不通知的情况下更改,从而破坏您的代码)