/ Published in: Python
Use this to start of new python applications.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/env python """ appname v1.0.1 Copyright(c) 2008; Me, Inc. Description of the program usage: appname [-g config ] -g config file (default is /usr/local/etc/mcp.conf) """ import os, sys, time, string, traceback def _banner(): """ Displays a banner announcing what this program is and what it does. """ import string doc = string.split( __doc__, '\n' ) s = doc[1] + '\n' + doc[3] n = 4 while doc[n][:6] != 'usage:': s = s + '\n' + doc[n] n = n + 1 return s + '\n' def _usage(): """ Displays the command line arguments for this application. """ import string print _banner() doc = string.split( __doc__, '\n' ) n = 0 while ( doc[n][:6] != 'usage:' ): n = n + 1 s = doc[n] n = n + 1 while ( len(doc[n]) ): s = s + '\n' + doc[n] n = n + 1 return s + '\n' def _processOpts(): # Process command line options import getopt, sys try: # If the option takes a parameter, don't forget to add # a colon immediately after the parameter. opts, args = getopt.getopt( sys.argv[1:], "g:", "" ) except: print _usage() sys.exit( 1 ) config = 'config.file' for o, a in opts: if o == "-g": config = a else: # we don't know this command line option print _usage() sys.exit( 1 ) return config # If this module is being run as the main module if __name__ == "__main__": # Get our command line options config = _processOpts()