I’m trying to write a piece of code in python to get command-line options and arguments using getopt module.
Here is my code:
import getopt
import sys
def usage ():
print('Usage')
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'xy:')
except getopt.GetoptError as err:
print(err)
usage()
sys.exit()
for o,a in opts:
if o in ("-x", "--xxx"):
print(a)
elif o in ("-y", "--yyy"):
print(a)
else:
usage()
sys.exit()
if __name__ == "__main__":
main()
The problem is that I can’t read the argument of option x, but I can read the argument of y. What should I do to fix this?
Try
getopt.getopt(sys.argv[1:], 'x:y:')http://docs.python.org/library/getopt.html