classification
Title: Argparse needs better error handling for nargs
Type: Stage: needs patch
Components: Library (Lib) Versions: Python 3.2, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: bethard Nosy List: Jason.Baker, bethard, eric.araujo, ned.deily
Priority: normal Keywords:

Created on 2010-09-13 23:25 by Jason.Baker, last changed 2010-11-02 21:41 by eric.araujo.

Messages (2)
msg116355 - (view) Author: Jason Baker (Jason.Baker) Date: 2010-09-13 23:25
This is referring to argparse 1.1 installed under Python 2.6.  When I was passing in an nargs flag, I figured that since '+' and '*' are valid options, I should pass in strings.  So when I tried passing in the string '1' instead of the integer 1, I got the following error:


>>> import argparse

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', nargs='1')
_StoreAction(option_strings=[], dest='foo', nargs='1', const=None, default=None, type=None, choices=None, help=None, metavar=None)

>>> parser.parse_args()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.6-universal/egg/argparse.py", line 1698, in parse_args
  File "build/bdist.macosx-10.6-universal/egg/argparse.py", line 1730, in parse_known_args
  File "build/bdist.macosx-10.6-universal/egg/argparse.py", line 1935, in _parse_known_args
  File "build/bdist.macosx-10.6-universal/egg/argparse.py", line 1884, in consume_positionals
  File "build/bdist.macosx-10.6-universal/egg/argparse.py", line 2028, in _match_arguments_partial
  File "build/bdist.macosx-10.6-universal/egg/argparse.py", line 2169, in _get_nargs_pattern
TypeError: can't multiply sequence by non-int of type 'str'

Fortunately, I had just added the nargs and knew to correct that.  However, if I were to do something like read that value in from a config file and forget to coerce the value from a string to an int, I could see how this could be a giant pain to track down.
msg116357 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2010-09-14 00:29
Note, argparse is not part of the Python standard library in 2.6 but the 2.7 and 3.2 versions exhibit the same behavior.
History
Date User Action Args
2010-11-02 21:41:32eric.araujosetnosy: + eric.araujo

stage: needs patch
2010-09-14 00:29:09ned.deilysetversions: + Python 2.7, Python 3.2, - Python 2.6
nosy: + ned.deily, bethard

messages: + msg116357

assignee: bethard
2010-09-13 23:25:11Jason.Bakercreate