简体   繁体   中英

python argparse, input output pairs

Looking for a pattern for argparse that would support a series of input/output files

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

Google is failing me - cause what I am finding is argparse tutorials and not what I am looking for.

You can just use append and match the resulting lists:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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