Python OptionParser允许选择类型选项的多个参数

时间:2015-02-05 20:51:23

标签: python optionparser

我在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只能在多个args中进行操作,所以我认为这可以用于类型选择。我无法弄清楚如何对可变数量的瑕疵做这件事。

语法如:

./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

我尝试使用回调来分割逗号,但如果回调返回一个数组,则会出错,因为该选项是无效的选择&f; foo,bar'。错误是有道理的,但我不确定从哪里开始。

def foo_callback(option, opt, value, parser):
  setattr(parser.values, option.dest, value.split(','))

尝试采用nargs方法,我尝试实施Callback Example 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()

这个片段介绍了一种实现可变数量的nargs的方法。但是,我让这个工作的唯一方法是从我的选项中删除类型和选择(击败我尝试做的目的)。如果我没有,我会得到的错误是:

[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

2 个答案:

答案 0 :(得分:1)

只需使用action='append', default=[]代替您当前的设置;无需自定义回调 然后,您可以将倍数指定为:-t foo -t bar

如果你需要默认值,你可以在事后做到;即类似的东西 test = options.test or ['foo']

答案 1 :(得分:1)

我认为您可以尝试从标准派生自己的选项类,以覆盖您的案例的标准检查:

from optparse import Option, OptionParser, OptionValueError

class MyOption(Option):
    def check_value(option, opt, value):
        for val in value.split(','):
            if val not in option.choices:
                choices = ", ".join(map(repr, option.choices))
                raise OptionValueError("option %s: invalid choice: %r (choose from %s)" % (opt, val, choices))
        return value

 parser = OptionParser(option_class=MyOption)
 foo_choices = ['foo', 'bar', 'mac', 'win'] 
 parser.add_option('-t', '--test',
          type='choice',
          dest='test',
          choices=foo_choices)


(options, args) = parser.parse_args()

print options

输出:

./test1.py -t foo,bar,mac
{'test': 'foo,bar,mac'}