aaron
aaron

Reputation: 597

Python argparse nargs='+' trying to pass in multiple strings but script is not picking up strings after first one

I have a Python file (python 3.6) that I would like to be able to take a series of file names in a single argument. I am trying to do so like this

parser.add_argument('--ftr', type=str, required=True, nargs='+',
                    help='a list of files to read, in order passed')
dargs, leftover = parser.parse_known_args()
import pdb
pdb.set_trace()

but when I run my command

python reader.py --ftr=file1.py file2.py

I get back

(Pdb) dargs
Namespace(ftr=['file1.py'])

How can it be made so that I can get back

Namespace(ftr=['file1.py', 'file2.py'])

?

Upvotes: 1

Views: 880

Answers (2)

Glenn Mackintosh
Glenn Mackintosh

Reputation: 2780

I believe your command line syntaxt is incorrect.

python reader.py --ftr=file1.py file2.py

Try leaving out the = sign afte the long argument name like this:

python reader.py --ftr file1.py file2.py

Upvotes: 2

Alex
Alex

Reputation: 7045

If you print the help text for your parser you can see how you are expected to supply your arguments:

usage: reader.py [-h] --ftr FTR [FTR ...]

optional arguments:
  -h, --help           show this help message and exit
  --ftr FTR [FTR ...]

Your command should be:

~ python reader.py --ftr file1.py file2.py

Upvotes: 3

Related Questions