python - command-line options and arguments using getopt -


i'm trying write piece of code in python command-line options , arguments using getopt module. here code:

import getopt import sys  def usage ():     print('usage')  def main():     try:         opts, args = getopt.getopt(sys.argv[1:], 'xy:')     except getopt.getopterror err:         print(err)         usage()         sys.exit()      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 can't read argument of option x, can read argument of y. should fix this?

try getopt.getopt(sys.argv[1:], 'x:y:')

http://docs.python.org/library/getopt.html

parses command line options , parameter list. args argument list parsed, without leading reference running program. typically, means sys.argv[1:]. options string of option letters script wants recognize, options require argument followed colon (':'; i.e., same format unix getopt() uses).


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -