Need help with OptionParser

H

hellt

today i've decided to use optionparser instead of GetOpt

and unfortunately i've got an error which i cant handle

my pice of code:

from optparse import OptionParser

def main():
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()


if __name__=="__main__":
main()


if i run test3.py without any arguments, i wait for generated help,
but i see the following

Usage: test3.py [options]

None

why None? Where are all of my options
 
H

hellt

today i've decided to use optionparser instead of GetOpt

and unfortunately i've got an error which i cant handle

my pice of code:

from optparse import OptionParser

def main():
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()

if __name__=="__main__":
main()

if i run test3.py without any arguments, i wait for generated help,
but i see the following

Usage: test3.py [options]

None

why None? Where are all of my options

=) my bad
unnecessary print in
print parser.print_usage()
and print_help() instead print_usage()
 
S

Stargaming

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top