How to use variable number of arguments in pyinvoke

I would like to use a variable number of arguments in a task for pyinvoke. For instance:

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

The above is just one of many options I tried, but it seems pyinvoke cannot handle a variable number of arguments. It's true?

The above code leads to

 $ invoke pdf_combine -o binder.pdf -i test.pdf test1.pdf No idea what '-i' is! 

Similarly, if I define pdf_combine (out_file, in_file), without an asterisk in front of in_file

 $ invoke pdf_combine -o binder.pdf -i test.pdf test1.pdf No idea what 'test1.pdf' is! 

If I call the task with only one in_file, as shown below, run OK.

 $ invoke pdf_combine -o binder.pdf -i test.pdf out = binder.pdf in = ['t', 'e', 's', 't', '.', 'p', 'd', 'f'] 

What I would like to see

 $ invoke pdf_combine -o binder.pdf test.pdf test1.pdf test2.pdf out = binder.pdf in = [test.pdf test1.pdf test2.pdf] 

I could not find anything like this in the pyinvoke documentation, although I cannot imagine that other users of this library do not need to call a task with a variable number of arguments ...

+5
source share
1 answer

You can do something like this:

 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 

If the invoke command:

 invoke pdf_combine -o binder.pdf -i test.pdf,test1.pdf,test2.pdf 

I could not find another way to do this by reading the pyinvoke documentation.

+3
source

Source: https://habr.com/ru/post/1244353/


All Articles