Variable number of arguments

David Raymond David.Raymond at tomtom.com
Mon Dec 17 13:57:53 EST 2018


argparse works fine on 3.x
https://docs.python.org/3.6/library/argparse.html

You can't really have back to back _positional_ arguments with nargs = "+" as it won't be able to tell where you meant one group to end and the next to begin. You'd have to call it with switches like

script.py -if inFile1 inFile2 inFile3 -of outFile -swf stopwordsFile1 stopwordsFile2


-----Original Message-----
From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of F Massion
Sent: Monday, December 17, 2018 1:38 PM
To: python-list at python.org
Subject: Variable number of arguments

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.

Any suggestions?
-- 
https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list