Need help with OptionParser

Stargaming stargaming at gmail.com
Tue Mar 25 09:20:50 EDT 2008


On Tue, 25 Mar 2008 05:42:03 -0700, hellt wrote:
[snip]
>     usage = "usage: %prog [options]"
>     parser = OptionParser(usage)
>     parser.add_option("-f", "--file", dest="filename",
>                                     help="executable filename",
> metavar="FILE")
>     parser.add_option("-b", "--bighosts",
>                   action="store_true", dest="bighosts", default=False,
>                   help="with big hosts  [default: %default]")
>     (options, args) = parser.parse_args()
>     if not options.bighosts:
>         print parser.print_usage()

FWIW you should perhaps give a special error message rather than the 
generic help (something like "you need to specify bighosts, see --help 
for details").

If possible, you should use `args` rather than an option (they are, well, 
optional). But if your program takes a lot of mandatory arguments, using 
options in order to mix them at will might be fine.

HTH,



More information about the Python-list mailing list