简体   繁体   English

python argparse,输入 output 对

[英]python argparse, input output pairs

Looking for a pattern for argparse that would support a series of input/output files寻找支持一系列输入/输出文件的 argparse 模式

For example:例如:

    app.py  --input FOO.txt_in   --output FOO.txt_out   --input BAR.txt_in  --output BAR.txt.Out

In effect, each "--input" file must be paired with an "--output" file and there must be 1 in/out pair, but can have multiples实际上,每个“--input”文件必须与一个“--output”文件配对,并且必须有1个输入/输出对,但可以有多个

Google is failing me - cause what I am finding is argparse tutorials and not what I am looking for.谷歌让我失望了——因为我发现的是 argparse 教程,而不是我想要的。

You can just use append and match the resulting lists:您可以只使用 append 并匹配结果列表:

parser = argprse.ArgumentParser()
parser.add_argument('--input', action='append')
parser.add_argument('--output', action='append')
args = parser.parse_args()

args.input and args.output are now lists (but you can provide them how you've written in the question), and you can zip them together. args.inputargs.output现在是列表(但您可以提供他们在问题中的编写方式),并且您可以将它们一起 zip 。 You can assert that they have the same length as a check, and you can safely assume that users will read your help message before trying to use the commandline, so add a help message to explain how this works You can say something like:您可以断言它们与检查具有相同的长度,并且您可以放心地假设用户会在尝试使用命令行之前阅读您的帮助消息,因此添加帮助消息以解释其工作原理您可以这样说:

The ith input corresponds to the ith output第 i 个输入对应第 i 个 output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM