Variable number of arguments

dieter dieter at handshake.de
Tue Dec 18 03:32:52 EST 2018


F Massion <massion.francois at gmail.com> writes:
> My script is used by a web interface which gets some parameters from a form.
> The parameters are passed on as arguments like this:
>
> (...)
> def start(self, _Inputfile ,_Outputfile ,_Stopwordsfile)
>
> As long as the number of arguments is set (i.e. 3), there are no problems running the script.
>
> Currently I have e.g. 
> ARGV[0] = _Inputfile
> ARGV[1] = _Outputfile
> ARGV[2] = _Stopwordsfile
>
> Now I want to allow a variable number of arguments, i.e. 1..n input files or stopwords lists.
>
> In this case ARGV[0] would become [filename1.txt, filename2.txt,...], but I wonder how ARGV[1] would still remain _Outputfile.
>
> I thought of using the argparse module with nargs='+' for a variable number arguments but it seems to work only with Python 2.7 and I have 3.6 running.

"argparse" works with both Python 2.7 and Python 3 (above 3.2, I think).

*BUT* "argparse" is used for parsing command lines and has nothing
to do with function signatures. Thus, "argparse" would
only help, if the "parameters from a form" contain an arguments string
which must be parsed into individual arguments.

For a web interface, it would be much more natural
that the arguments ("input", "output", "stop words") come
from individual form fields. In this case, no parsing would
be necessary.

Depending on what form library your web interface uses,
it may support complex form values (such as lists) directly.
One example is "z3c.form".




More information about the Python-list mailing list