How to move optparse from main to function?

Bob bob.python at yahoo.com
Thu Feb 23 13:59:39 EST 2006


I'm playing around with optparse and created the code below. How do I
move that to a function and what variable do I pass?

>From the documentation it seems like "options.optparse_test" would have
the value zero if its option, either "-t" or "--test", is not detected
at the command line. When I issue "print options.optparse_test" when no
command options are used, "None" is sent to the screen (IIRC)... Is a
proper check if a command line argument is not used to use the
following:

# being check
if options.optparse_test <> 1:
 print "test parameter NOT detected"
# end check

Thanks.

# begin program
#!/usr/bin/python

from optparse import OptionParser
import string

parser = OptionParser()
parser.add_option("-t", "--test", action="count", dest="optparse_test",
 help="testing optparse")

(options, args) = parser.parse_args()
print options.optparse_test
if options.optparse_test == 1:
 print "test parameter detected"
if options.optparse_test <> 1:
 print "test parameter NOT detected"
#parser.print_help()

# end program




More information about the Python-list mailing list