This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author atpage
Recipients atpage
Date 2016-01-22.16:35:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1453480532.52.0.987369969347.issue26181@psf.upfronthosting.co.za>
In-reply-to
Content
This code is meant to take a filename and a list of integers as arguments.  The filename is required, the integers are optional:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('filename')
parser.add_argument('-L', metavar='integer', type=int, nargs='+')
args = parser.parse_args()
print(args)  # see what we got

It produces the following help message:
usage: demo.py [-h] [-L integer [integer ...]] filename

However, the filename argument does not work if it's given in that position (after the list of ints).  Instead, it tries to use filename as another list element:

$ python demo.py -L 1 2 3 test.txt
usage: demo.py [-h] [-L integer [integer ...]] filename
demo.py: error: argument -L: invalid int value: 'test.txt'

Changing the order of the arguments works as intended:

$ python demo.py test.txt -L 1 2 3 
Namespace(L=[1, 2, 3], filename='test.txt')

Probably the simplest fix would be to amend the help message to show the positional argument before the list:

usage: demo.py [-h] filename [-L integer [integer ...]]
History
Date User Action Args
2016-01-22 16:35:32atpagesetrecipients: + atpage
2016-01-22 16:35:32atpagesetmessageid: <1453480532.52.0.987369969347.issue26181@psf.upfronthosting.co.za>
2016-01-22 16:35:32atpagelinkissue26181 messages
2016-01-22 16:35:32atpagecreate