So the return value from getopt.getopt() is a list of tuples, e.g.
[('-a', '1'), ('-b', '2'), ('-a', '3')]
what's the idiomatic way of using this result? I can think of several
possibilities.
For options not allowed to occur more than once, you can turn it into
a dictionary:
'2'
Otherwise, you can use a list comprehension:
['1', '3']
Maybe the usual idiom is to iterate through the list rather than using
it in situ...
.... if opt == '-a':
.... a.append(arg)
.... elif opt == '-b':
.... b = arg
.... else:
.... raise ValueError("Unr ecognized option %s" % opt)
Any of the foregoing constitute The One Way To Do It? Any simpler,
legible shortcuts?
Thanks.
>>import getopt
>>opts = getopt.getopt('-a 1 -b 2 -a 3'.split(), 'a:b:')[0]; opts
>>opts = getopt.getopt('-a 1 -b 2 -a 3'.split(), 'a:b:')[0]; opts
what's the idiomatic way of using this result? I can think of several
possibilities.
For options not allowed to occur more than once, you can turn it into
a dictionary:
>>b = dict(opts)['-b']; b
Otherwise, you can use a list comprehension:
>>a = [arg for opt, arg in opts if opt == '-a']; a
Maybe the usual idiom is to iterate through the list rather than using
it in situ...
>>a = []
>>b = None
>>for opt, arg in opts:
>>b = None
>>for opt, arg in opts:
.... a.append(arg)
.... elif opt == '-b':
.... b = arg
.... else:
.... raise ValueError("Unr ecognized option %s" % opt)
Any of the foregoing constitute The One Way To Do It? Any simpler,
legible shortcuts?
Thanks.
Comment