Author paul.j3
Recipients DasIch, G2P, Julian, bethard, bewest, bkabrda, chris.jerdonek, csernazs, dsully, elsdoerfer, eric.araujo, labrat, nvie, paul.j3, r.david.murray, seblu, zzzeek
Date 2013-04-09.07:08:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1365491323.44.0.501518162998.issue9253@psf.upfronthosting.co.za>
In-reply-to
Content
I think this problem arises from a change made in http://bugs.python.org/issue10424

Changeset to default (i.e. development) is
http://hg.python.org/cpython/rev/cab204a79e09

Near the end of _parse_known_args it removes a:

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

with a scan for required options that have not been seen. 

Ordinary positionals are required.  But a SubParsersAction is not required.  So we no longer get a warning.

http://bugs.python.org/issue12776 changed this block of code as well.  Notice the 2.7 and 3.2 branches have this 'too few arguments' error, but the default does not.

The default value for Action.required is False.  In _get_positional_kwargs(), a positional's required is set based on nargs (e.g. '+' is required, '*' not).  But add_subparsers() does not use this, so its 'required' ends up False.

This fudge seems to do the trick:

   parser = ArgumentParser(prog='test')
   subparsers = parser.add_subparsers()
   subparsers.required = True
   subparsers.dest = 'command'
   subparser = subparsers.add_parser("foo", help="run foo")
   parser.parse_args()

producing an error message:

   usage: test [-h] {foo} ...
   test: error: the following arguments are required: command

subparsers.dest is set so the error message can give this positional a name.

I'll try to write a patch to do something similar in argparse itself.
History
Date User Action Args
2013-04-09 07:08:43paul.j3setrecipients: + paul.j3, csernazs, bethard, eric.araujo, r.david.murray, zzzeek, labrat, chris.jerdonek, nvie, DasIch, elsdoerfer, G2P, Julian, dsully, seblu, bewest, bkabrda
2013-04-09 07:08:43paul.j3setmessageid: <1365491323.44.0.501518162998.issue9253@psf.upfronthosting.co.za>
2013-04-09 07:08:43paul.j3linkissue9253 messages
2013-04-09 07:08:42paul.j3create