notice that the script fails if an unsupported option is
issued
./autoftp2.py -x
...
getopt.GetoptError: option -x not recognised
Better argument handling
so we need a way to trap these errors
python uses an exception handler for this
#!/usr/bin/python
import sys, getopt
def Usage ():
print "autoftp [-v][-p][-h]"
sys.exit(0)
try:
optlist, list = getopt.getopt(sys.argv[1:],
’:vphf:’)
except getopt.GetoptError:
Usage()
print "called exception"
sys.exit(1)
for opt in optlist:
print opt
if opt[0] == ’-h’:
Usage()
if opt[0] == ’-v’:
print "verbose found"
if opt[0] == ’-p’:
print "probeonly found"
if opt[0] == ’-f’:
print "file option found"
没有评论:
发表评论