command line arguments - What is the data type returned by getopt in Python -
i have following use of getopt
in code:
opts, args = getopt.getopt(sys.argv[1:], "", ["admit=", "wardname="])
then run code command line in following matter:
test.py --args --admit=1 --wardname="ccu"
when print contents of opts
, following output:
[('--admit', '1'), ('--wardname', 'ccu')]
the first question data type of result? seems me list of tuples. correct?
the second question - there convenient way work such tuple pairs (if these tuples)? example, how can say: if admit == 1 x? thought of converting tuples dictionary, practice?
p.s. shouldn't make difference jython , not pure python.
the front page of python docs describes python library docs "keep under pillow". page on getopt at:
http://docs.python.org/2/library/getopt.html
you 2 lists getopt: list of tuples mentioned, followed list of remaining arguments after options have been parsed out. try this:
import getopt args = ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] opts, args = getopt.getopt(args, "abc:d") print ("opts=" + str(opts)) print ("args=" + str(args)) optdic = dict(opts) # convert options dictionary print ("optdic['-c'] = " + str(optdic['-c']))
output:
opts=[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', '')] args=['bar', 'a1', 'a2'] optdic['-c'] = foo
Comments
Post a Comment