[issue28609] argparse claims '*' positional argument is required in error output

paul j3 report at bugs.python.org
Sun Nov 6 13:15:13 EST 2016


paul j3 added the comment:

The current error message is the result of http://bugs.python.org/issue10424 and http://bugs.python.org/issue12776

Before the test was just:

   if positionals:
       self.error(_('too few arguments'))

The 2nd patch reworked the test to include the revised handling of defaults.

So the current error message just lists all the positionals which haven't been consumed.  ARGUMENT wasn't consumed because COMMAND wasn't consumed.     And technically that would be true even if ARGUMENT required arguments. Well, to be pickier, it as a 're' pattern like 'AA*' that failed. 

The proposed patch looks like it would work, but I haven't tested or looked at the unittests.  But I wonder if such a patch is really needed. Are users really misled by the the current message?

===============

As for the usage, the current version allows you to give a tuple METAVAR, producing lines like:

    In [359]: a.metavar=('A','B')
    In [360]: parser.print_usage()
    usage: ipython3 [-h] [A [B ...]]
    In [361]: a.nargs='+'
    In [362]: parser.print_usage()
    usage: ipython3 [-h] A [B ...]

This display pattern is generated in HelpFormater._format_args, with these lines

        elif action.nargs == ZERO_OR_MORE:
            result = '[%s [%s ...]]' % get_metavar(2)
        elif action.nargs == ONE_OR_MORE:
            result = '%s [%s ...]' % get_metavar(2)
        elif action.nargs == REMAINDER:
            result = '...'

You could subclass HelpFormatter, and replace this method with one that performs as you want, (I just tried this)

    result = '[%s ...]' % get_metavar(1)

I wouldn't recommend this as default change, but if there was a enough demand it could added as another HelpFormatter subclass.

METAVAR lets me approximate your shorter version:

    In [4]: p.print_usage()
    usage: ipython3 [-h] [pos [pos ...]]
    In [5]: a.metavar=('pos','')
    In [6]: p.print_usage()
    usage: ipython3 [-h] [pos [...]]
    In [7]: a.nargs='+'
    In [8]: p.print_usage()
    usage: ipython3 [-h] pos [...]

It still has the [], but the name is gone.

----------
nosy: +paul.j3

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


More information about the Python-bugs-list mailing list