6.20.2.7 Putting it all together

Here's what optparse-based scripts typically look like:

from optparse import OptionParser
[...]
def main():
    usage = "usage: \%prog [-f] [-v] [-q] firstarg secondarg"
    parser = OptionParser(usage)
    parser.add_option("-f", "--file", type="string", dest="filename",
                      help="read data from FILENAME")
    parser.add_option("-v", "--verbose",
                      action="store_true", dest="verbose")
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose")

    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("incorrect number of arguments")

    if options.verbose:
        print "reading \%s..." \% options.filename
    [... go to work ...]

if __name__ == "__main__":
    main()

See About this document... for information on suggesting changes.