"""example.py Module docstring. example.py DoesSomething to infilename Producing outfilename Usage: example.py infileName outfileName Example: example.py in.txt out.txt """ def process(args): print 'we did example.py to ' + str(args[0]) + ' yielding ' + str(args[1]) # Guidos fancy main() see: http://www.artima.com/weblogs/viewpost.jsp?thread=4829 class Usage(Exception): def __init__(self, msg): self.msg = msg def main(argv=None): import sys import getopt N_ARGS = 2 # see line 37 for check if argv is None: argv = sys.argv # try: try: opts, args = getopt.getopt(argv[1:], "h", ["help"]) except getopt.error, msg: raise Usage(msg) # process options for o, a in opts: if o in ("-h", "--help"): print __doc__ sys.exit(0) # process arguments if len(args) == N_ARGS: process(args) #NOTE: args[0] != argv[0] == else: msg = __doc__ raise Usage(msg) except Usage, err: print >>sys.stderr, err.msg print >>sys.stderr, "for help use --help" return 2 if __name__ == "__main__": sys.exit(main())