[New-bugs-announce] [issue9348] Calling argparse's add_argument with the wrong number of metavars causes delayed error message

Steven Bethard report at bugs.python.org
Fri Jul 23 15:34:16 CEST 2010


New submission from Steven Bethard <steven.bethard at gmail.com>:

What steps will reproduce the problem?

parser = argparse.ArgumentParser()	
parser.add_argument('--foo', nargs=2, metavar=('X','Y','Z'))
parser.parse_args(['-h'])

The error dosn't show up until help is formatted.

Giving any incorrect length of metavar will produce the problem, which
includes a tuple whos length doesn't match a numerical value, more than two metavars to '*' or '+', and more than one metavar to '?'. Furthermore, a tuple of length one causes the error when nargs is greater than 1, '*', or '+'.

What is the expected output? What do you see instead?

When the help is displayed, you get:

TypeError: not all arguments converted during string formatting

Or a similar error message for other cases.

It would be expected that the error message would be more specific. The
error should definitely be raised when add_argument is called, rather than later.

There should be a test that does something like:

    for meta in ('X', ('X',), ('X','Y'), ('X','Y','Z')):
        for n in (1, 2, 3, '?', '+', '*'):
            parser = argparse.ArgumentParser()
            parser.add_argument('--foo', nargs=n, metavar=meta)
            parser.format_help()

and makes sure that the error shows up in add_argument, not format_help.

----------
components: Library (Lib)
messages: 111317
nosy: bethard
priority: normal
severity: normal
stage: needs patch
status: open
title: Calling argparse's add_argument with the wrong number of metavars causes delayed error message
type: behavior
versions: Python 2.7, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9348>
_______________________________________


More information about the New-bugs-announce mailing list