#!/usr/bin/python
import sys, getopt
#--------------------------------------
# default values
#--------------------------------------
verbose = "false"filename= ""
size = "small"
#--------------------------------------
# display usage
#--------------------------------------
def usage():
print "Usage: getopt_ex.py [OPTION] FILENAME "
print " -h, --help : Help "
print " -v, --verbose : Verbose "
print " -s, --size=SIZE : Option with value "
sys.exit()
# setup arguements passed to getopt
try:
opts, args =getopt.getopt(sys.argv[1:], "hvs:", ["help","verbose", "size="])
except getopt.error:sys.exit(1)
#--------------------------------------
# set arguments
#--------------------------------------
for opt, arg in opts:
if opt in ("-h", "--help"):usage()
elif opt in ("-v", "--verbose"):verbose= "true"
elif opt in ("-s", "--size"):size= arg
if (len(args)!= 1):
usage()
else:
filename = args[0]
print "Options:"
print " Verbose : ", verbose
print " Size : ", size
print " Filename: ", filename
Syntax highlighted by code2html, ver. $Rev: 17 $