[New-bugs-announce] [issue10424] better error message from argparse when positionals missing

Steven Bethard report at bugs.python.org
Mon Nov 15 11:38:44 CET 2010

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

>From a private email in respect to the following class of error messages:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--foo')
>>> parser.add_argument('--bar')
>>> parser.add_argument('ham')
>>> parser.add_argument('spam', nargs='+')
>>> parser.parse_args(['HAM'])
usage: PROG [-h] [--foo FOO] [--bar BAR] ham spam [spam ...]
PROG: error: too few arguments

One suggestion would be that when it displays the error "too few arguments", it would nice if it said something about the argument(s) that are missing.

I modified argparse's error message when there are too few arguments.  I didn't examine the code a lot, so there might be cases where this doesn't work, but here's what I did:

    if positionals:
        self.error(_('too few arguments: %s is required' % positionals[0].dest))

This would be a nice feature - I haven't checked if the suggested approach works in general though.

components: Library (Lib)
messages: 121220
nosy: bethard
priority: normal
severity: normal
stage: needs patch
status: open
title: better error message from argparse when positionals missing
versions: Python 3.2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list