Posted By

kangell on 03/22/08


Tagged

template Shell python new application app


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

pulczynski


New Python App Shell


 / Published in: Python
 

Use this to start of new python applications.

  1. #!/usr/bin/env python
  2. """
  3. appname v1.0.1 Copyright(c) 2008; Me, Inc.
  4.  
  5. Description of the program
  6.  
  7. usage: appname [-g config ]
  8. -g config file (default is /usr/local/etc/mcp.conf)
  9. """
  10.  
  11. import os, sys, time, string, traceback
  12.  
  13. def _banner():
  14. """
  15. Displays a banner announcing what this program is and what it does.
  16. """
  17.  
  18. import string
  19.  
  20. doc = string.split( __doc__, '\n' )
  21. s = doc[1] + '\n' + doc[3]
  22. n = 4
  23. while doc[n][:6] != 'usage:':
  24. s = s + '\n' + doc[n]
  25. n = n + 1
  26.  
  27. return s + '\n'
  28.  
  29.  
  30. def _usage():
  31. """
  32. Displays the command line arguments for this application.
  33. """
  34.  
  35. import string
  36.  
  37. print _banner()
  38.  
  39. doc = string.split( __doc__, '\n' )
  40. n = 0
  41. while ( doc[n][:6] != 'usage:' ):
  42. n = n + 1
  43.  
  44. s = doc[n]
  45. n = n + 1
  46. while ( len(doc[n]) ):
  47. s = s + '\n' + doc[n]
  48. n = n + 1
  49.  
  50. return s + '\n'
  51.  
  52. def _processOpts():
  53.  
  54. # Process command line options
  55. import getopt, sys
  56. try:
  57. # If the option takes a parameter, don't forget to add
  58. # a colon immediately after the parameter.
  59. opts, args = getopt.getopt( sys.argv[1:], "g:", "" )
  60. except:
  61. print _usage()
  62. sys.exit( 1 )
  63.  
  64. config = 'config.file'
  65.  
  66. for o, a in opts:
  67.  
  68. if o == "-g":
  69. config = a
  70. else:
  71. # we don't know this command line option
  72. print _usage()
  73. sys.exit( 1 )
  74.  
  75. return config
  76.  
  77.  
  78. # If this module is being run as the main module
  79. if __name__ == "__main__":
  80.  
  81. # Get our command line options
  82. config = _processOpts()

Report this snippet  

You need to login to post a comment.