【发布时间】:2016-06-16 18:16:15
【问题描述】:
我想在 pyinvoke 的任务中使用可变数量的参数。 像这样:
from invoke import task
@task(help={'out_file:': 'Name of the output file.',
'in_files': 'List of the input files.'})
def pdf_combine(out_file, *in_files):
print( "out = %s" % out_file)
print( "in = %s" % list(in_files))
以上只是我尝试过的众多变体之一,但似乎 pyinvoke 无法处理可变数量的参数。这是真的吗?
以上代码结果
$ invoke pdf_combine -o binder.pdf -i test.pdf test1.pdf
No idea what '-i' is!
类似,如果我定义 pdf_combine(out_file, in_file),in_file 之前没有星号
$ invoke pdf_combine -o binder.pdf -i test.pdf test1.pdf
No idea what 'test1.pdf' is!
如果我只使用下面的一个 in_file 调用任务,它运行正常。
$ invoke pdf_combine -o binder.pdf -i test.pdf
out = binder.pdf
in = ['t', 'e', 's', 't', '.', 'p', 'd', 'f']
我想看到的是
$ invoke pdf_combine -o binder.pdf test.pdf test1.pdf test2.pdf
out = binder.pdf
in = [test.pdf test1.pdf test2.pdf]
我在 pyinvoke 的文档中找不到类似的东西,尽管我无法想象这个库的其他用户不需要调用具有可变数量参数的任务...
【问题讨论】:
-
您遇到错误了吗?如果是这样,请在您的问题中包含回溯。
-
谢谢,没有回溯,问题更多是关于使用 pyinvoke 库。我用几个例子修正了我的问题以进行澄清。
-
我不确定您使用的是哪个版本的调用 - 但您需要使用
invoke pdf-combine(注意它是一个破折号而不是下划线 - 如果他们有一个参数,则在参数中这样做docs.pyinvoke.org/en/stable/concepts/…)