passing bash seq sequence to python option parser: TypeError: '<' not supported between instances of 'int' and 'str'

passing bash seq sequence to python option parser: TypeError: '<' not supported between instances of 'int' and 'str'

我只是想完成这项工作:

def main(argv):
    parser = OptionParser()
    parser.add_option("-v", "--variables", nargs="*", default=['dem'], type="str", dest="variables")
    parser.add_option("-t", "--timesID", nargs="*", default=range(20,-200,-1), type="float", dest="timesID")
    (options, args) = parser.parse_args(argv)
    try:
        return get_chelsa(
            inputFile = options.input,
            variables = options.variables,
            timesID = options.timesID)
    except Exception as e:
        print(e)

if __name__ == '__main__':
    import sys
    main(sys.argv[1:])

然后我想调用它:

python3 script.py -t seq -100 -1 20

但是它returns一个错误:

Traceback (most recent call last):
  File "script.py", line 203, in <module>
    main(sys.argv[1:])
  File ".script.py", line 186, in main
    (options, args) = parser.parse_args(argv)
  File "/usr/lib/python3.8/optparse.py", line 1387, in parse_args
    stop = self._process_args(largs, rargs, values)
  File "/usr/lib/python3.8/optparse.py", line 1431, in _process_args
    self._process_short_opts(rargs, values)
  File "/usr/lib/python3.8/optparse.py", line 1522, in _process_short_opts
    if len(rargs) < nargs:
TypeError: '<' not supported between instances of 'int' and 'str'

我做错了什么?

我最终使用了回调。对我来说仍然很奇怪,没有什么比这更直接了。我对这个解决方案不是很满意,因为我不能将默认序列定义为选项。

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

def get_timesID_args(option, opt, value, parser, type='float'):
    setattr(parser.values, option.dest, [float(s) for s in value.split(',')])

def main(argv):

    parser = OptionParser()

    parser.add_option("-v", "--variables",
                        dest="variables",
                        type='str',
                        action='callback',
                        callback=get_variables_args)

    parser.add_option("-t", "--timesID",
                        dest="timesID",
                        type='str',
                        action='callback',
                        callback=get_timesID_args)
    (options, args) = parser.parse_args(argv)
    try:
        return my_function(
            variables = options.variables,
            timesID = options.timesID)
    except Exception as e:
        print(e)

if __name__ == '__main__':
    import sys
    main(sys.argv[1:])

可在 bash 中调用:

myscript.py -v dem -t $(seq -s ',' -100 1 20)