0

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 -?

cdhowie
  • 158,093
  • 24
  • 286
  • 300
nsh
  • 1,499
  • 6
  • 13
  • 14
  • 1
    what type is your `parser` object? `argparse` module provides automatically generated help and usage messages, so you don't need to add those -> http://docs.python.org/dev/library/argparse.html – wim Aug 22 '11 at 03:51
  • I can't get your code to run with either `argparse` or `optparse` -- `argparse` doesn't have `add_option` and `optparse` dies with "conflicting option string(s)" – Owen Aug 22 '11 at 04:13

1 Answers1

1

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()],[])
Community
  • 1
  • 1
David Andersson
  • 755
  • 4
  • 9
  • 1
    attributes prepended with an underscore are dubious to use externally (they can change without notice, breaking your code) – wim Aug 22 '11 at 04:57