Python 如何在pyinvoke中使用可变数量的参数
我希望在pyinvoke的任务中使用数量可变的参数。 像这样: 以上只是我尝试过的众多变体之一,但pyinvoke似乎无法处理数量可变的参数。这是真的吗 上面的代码导致Python 如何在pyinvoke中使用可变数量的参数,python,pyinvoke,Python,Pyinvoke,我希望在pyinvoke的任务中使用数量可变的参数。 像这样: 以上只是我尝试过的众多变体之一,但pyinvoke似乎无法处理数量可变的参数。这是真的吗 上面的代码导致 $ invoke pdf_combine -o binder.pdf -i test.pdf test1.pdf No idea what '-i' is! 类似的,如果我定义pdf\u组合(out\u文件,in\u文件),在in\u文件之前没有星号 $ invoke pdf_combine -o binder.pdf -i
$ invoke pdf_combine -o binder.pdf -i test.pdf test1.pdf
No idea what '-i' is!
类似的,如果我定义pdf\u组合(out\u文件,in\u文件),在in\u文件之前没有星号
$ invoke pdf_combine -o binder.pdf -i test.pdf test1.pdf
No idea what 'test1.pdf' is!
如果我调用任务时只有一个in_文件,如下面所示,则运行OK
$ 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的文档中找不到类似的内容,尽管我无法想象该库的其他用户不需要调用具有可变参数数的任务…您可以这样做:
from invoke import task
@task
def pdf_combine(out_file, in_files):
print( "out = %s" % out_file)
print( "in = %s" % in_files)
in_file_list = in_files.split(',') # insert as many args as you want separated by comma
>> out = binder.pdf
>> in = test.pdf,test1.pdf,test2.pdf
其中,invoke
命令为:
invoke pdf_combine -o binder.pdf -i test.pdf,test1.pdf,test2.pdf
阅读文档时,我找不到其他方法来执行此操作。从您可以使用的版本开始:
@任务(
帮助={
“输出文件”:“输出文件的名称”。#“输出文件”不是“输出文件”`
“在文件中”:“输入文件列表”。},
iterable=['in_files'],
)
def pdf_组合(输出文件、输入文件):
对于\u文件中的项目:
打印(f“文件:{item}”)
注意help
使用破折号转换键和iterable
使用非转换下划线键
注释我知道上面的注释有点奇怪,所以我提交了一份报告,因为作者非常棒,可能会考虑这个建议
以这种方式使用它允许使用这种类型的cli:
$invoke pdf combine-o spam-i eggs-i ham
档案:鸡蛋
档案:火腿
$invoke—帮助pdf合并
用法:inv[oke][--core opts]pdf组合[--options][此处的其他任务…]
文档字符串:
没有一个
选项:
-i、 --在输入文件的文件列表中
-o STRING,--out file=输出文件的字符串名称。
注意
pdf\u combine
使用CLI中的inv-pdf combine
调用任务是否收到错误?如果是,请在问题中包含回溯。谢谢,没有回溯,问题更多的是关于使用pyinvoke库。我用几个例子修正了我的问题,以便澄清。我不确定您使用的是什么版本的invoke,但您需要使用invoke pdf combine
(注意,它是破折号,而不是下划线,如果参数中有下划线的话)谢谢。这看起来是一个实用且干净的解决方案。我很高兴文档中没有遗漏任何内容。同时,我使用了argparse模块。还需要20分钟才能理解,但这是值得的。我刚刚瞥见了它的力量。当然,pyinvoke的目标与argparse的目标不同。很公平。
invoke pdf_combine -o binder.pdf -i test.pdf,test1.pdf,test2.pdf