我在 OptionParser 中有一个选项,如下所示:
foo_choices = ['foo', 'bar', 'mac', 'win']
parser.add_option('-t', '--test',
type='choice',
action='store',
dest='test',
choices=foo_choices,
default='foo')
但是,我希望该选项能够接受多个选项(可以用逗号分隔,或允许 > 1 个参数)。只需设置 nargs=2 就可以传递多个参数,所以我认为这可以用于类型选择。我无法弄清楚如何处理可变数量的 nargs。
语法如:
./demo.py -t foo -o out.out
./demo.py -t foo,bar -o out.out
或者
./demo.py -t foo -o out.out
./demo.py -t foo bar -o out.out
我尝试使用回调以逗号分隔,但如果回调返回一个数组,则会出错,因为该选项是无效的选择“foo,bar”。这个错误是有道理的,但我不确定从这里去哪里。
def foo_callback(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
尝试采用 nargs 方法,我尝试实现回调示例 6,
#!/usr/bin/python
from optparse import OptionParser
def main():
parser = OptionParser()
foo_choices = ['foo', 'bar', 'mac', 'win']
parser.add_option('-t', '--test',
type='choice',
action='callback',
dest='test',
choices=foo_choices,
callback=vararg_callback)
(options, args) = parser.parse_args()
print options
print args
def vararg_callback(option, opt_str, value, parser):
assert value is None
value = []
def floatable(str):
try:
float(str)
return True
except ValueError:
return False
for arg in parser.rargs:
# stop on --foo like options
if arg[:2] == "--" and len(arg) > 2:
break
# stop on -a, but not on -3 or -3.0
if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
break
value.append(arg)
del parser.rargs[:len(value)]
setattr(parser.values, option.dest, value)
if __name__ == '__main__':
main()
这个片段介绍了一种实现可变数量的参数的方法。但是,我让它工作的唯一方法是从我的选项中删除类型和选择(违背了我想要做的事情的目的)。如果我没有,我会得到的错误是:
[vagrant@machine a]$ ./demo.py -t foo
Traceback (most recent call last):
File "./demo.py", line 43, in <module>
main()
File "./demo.py", line 14, in main
(options, args) = parser.parse_args()
File "/usr/lib64/python2.6/optparse.py", line 1394, in parse_args
stop = self._process_args(largs, rargs, values)
File "/usr/lib64/python2.6/optparse.py", line 1438, in _process_args
self._process_short_opts(rargs, values)
File "/usr/lib64/python2.6/optparse.py", line 1545, in _process_short_opts
option.process(opt, value, values, self)
File "/usr/lib64/python2.6/optparse.py", line 788, in process
self.action, self.dest, opt, value, values, parser)
File "/usr/lib64/python2.6/optparse.py", line 808, in take_action
self.callback(self, opt, value, parser, *args, **kwargs)
File "./demo.py", line 20, in vararg_callback
assert value is None
AssertionError