开发者

Python : Not able to get the list of the options that have been added via 'add_option'?

My code portion looks like:

 parser.add_option("-h", "--help","-?",
                   action = "help",
     开发者_开发百科              help= """Print the help of the scipt"""
                 )

When I am trying to print the options available for the script, it returns an empty array.

  optlist = [x.get_opt_string() for x in parser._get_all_options()[1:]]
  print optlist

Printing optlist prints an empty array -> [ ].

I need to print an array with all the available options. In this case, an array that stores values: -h, --help and -?


In python 2.6.5 optparse objects have undocumented attributes _short_opts and _long_opts. For a bumpy list

[x._short_opts + x._long_opts for x in parser._get_all_options()]

Using join list of lists in python to flatten the list

sum([x._short_opts + x._long_opts for x in parser._get_all_options()],[])
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜