Handle command line arguments with getopt in Python

Posted in python

Here’s a way to use getopt to handle arguments when creating command line applications. It works quite well with the standard help (–help or -h) by showing usage when bad arguments are passed.

#!/bin env python
import sys

def main(argv):
   try:
      for opt, arg in opts:
         if opt in ("-h", "--help"):
            usage()
            sys.exit()
         elif opt == "all":
            pass
         elif opt == "1":
            pass
         elif opt == "2":
            pass
         elif opt == "3":
            pass
   except getopt.GetoptError:
         usage()
         sys.exit(2)
Posted by admica   @   29 December 2009

Related Posts

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Hidden Title Challenges in Call of Duty Modern Warfare 2
Next Post
Build script to utilize Makefiles to build multiple applications »
Powered by Wordpress   |   Lunated designed by ZenVerse