New submission from Alex:
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 ...]]
----------
components: Library (Lib)
messages: 258823
nosy: atpage
priority: normal
severity: normal
status: open
title: argparse can't handle positional argument after list (help message is
wrong)
type: behavior
versions: Python 2.7, Python 3.4
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue26181>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com