Skip to content

Instantly share code, notes, and snippets.

@nikoheikkila
Created May 25, 2013 09:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikoheikkila/5648502 to your computer and use it in GitHub Desktop.
Save nikoheikkila/5648502 to your computer and use it in GitHub Desktop.
Python: general main() function
"""Module docstring
This is a placeholder for a long usage message.
"""
import sys
import getopt
def process(arg):
""" Write a method for argument processing here """
pass
def main(argv=None):
""" Main function """
if argv is None:
argv = sys.argv
# Parse commandline options
try:
opts, args = getopt.getopt(argv[1:], "h", ["help"])
except getopt.error, msg:
print msg
print "for help use --help"
sys.exit(2)
# Process options
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
# Process arguments
for arg in args:
process(arg)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment