as we have seen in bash, getopts, provides a useful
method for handling arguments
in fact many languages have adopted getopts
C, C++, bash and python
autoftp has three which had three options:
-v verbose on
-p probeonly
-h help
Autoftp arguments in python
#!/usr/bin/python
import sys, getopt
def Usage ():
print "autoftp [-v][-p][-h]"
sys.exit(0)
optlist, list = getopt.getopt(sys.argv[1:], ’:vphf:’)
print "optlist =", optlist
print "list =", list
for opt in optlist:
print opt
if opt[0] == ’-h’:
Usage()
if opt[0] == ’-f’:
print "file found"
if opt[0] == ’-v’:
print "verbose found"
if opt[0] == ’-p’:
print "probeonly found"
没有评论:
发表评论