Return to Snippet

Revision: 2303
at January 31, 2007 18:50 by V


Initial Code
import sys
import getopt

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.error, msg:
             raise Usage(msg)
        # more code, unchanged
    except Usage, err:
        print >>sys.stderr, err.msg
        print >>sys.stderr, "for help use --help"
        return 2

if __name__ == "__main__":
    sys.exit(main())

Initial URL
http://www.artima.com/weblogs/viewpost.jsp?thread=4829

Initial Description
Guido Van Rossum talks about his recipe for a useful main() function.

Initial Title
Python main() functions

Initial Tags


Initial Language
Python