My Python script template


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #/bin/env python
  2.  
  3. import getopt
  4. import sys
  5. import os
  6.  
  7. def main(argv):
  8. # check arguments and options
  9. try:
  10. opts, args = getopt.getopt(argv, "hd:", ["help", "database"])
  11. # show usage printout if something is bad
  12. for opt, arg in opts:
  13. if opt in ("-h, --help"):
  14. printUsage()
  15. sys.exit(2)
  16. if opt in ("-d, --db"):
  17. print "Supposed to create db '" + arg +"'"
  18. MAIN_PROGRAM_METHOD(args[0])
  19. except getopt.GetoptError:
  20. printUsage(True)
  21. sys.exit(2)
  22. except IndexError:
  23. printUsage(True)
  24. sys.exit(2)
  25.  
  26. def printUsage(error = False):
  27. if error:
  28. print "Unknown option or missing argument."
  29. print """
  30. Usage: scriptname.py [options] <xml file>
  31.  
  32. -h show this help
  33. -d use specific database
  34. """
  35.  
  36. if __name__ == "__main__":
  37. main(sys.argv[1:])

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.