Author bethard
Recipients bethard
Date 2010-07-23.12:00:02
SpamBayes Score 0.00994753
Marked as misclassified No
Message-id <1279886405.92.0.918578525345.issue9340@psf.upfronthosting.co.za>
In-reply-to
Content
[Moved from http://code.google.com/p/argparse/issues/detail?id=45]

If you try to use parse_known_args and you have a subparser, the subparser will still complain if it sees extra arguments:

>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers()
>>> subparsers.add_parser('A')
>>> parser.parse_known_args(['A', '--foo', 'b'])
usage:  A [-h]
 A: error: unrecognized arguments: --foo b

What should be returned is probably:

>>> parser.parse_known_args(['A', '--foo', 'b'])
(Namespace(), ['--foo', 'b'])

The problem is that subparsers don't know whether they're being called by parse_args() or parse_known_args(). I see a few possible fixes:

* Add another argument to Action.__call__ that indicates what method is being called. But that would break any existing subclasses.

* Do some stack inspection using sys._getframe(). But that's not guaranteed to work on other implementations of Python.

* When parse_args is called, set some flag on the subparsers object that causes it to call parse_known_args instead, and restore that flag before parse_known_args returns. This probably introduces potential threading issues, though practically perhaps they'll never turn up.
History
Date User Action Args
2010-07-23 12:00:06bethardsetrecipients: + bethard
2010-07-23 12:00:05bethardsetmessageid: <1279886405.92.0.918578525345.issue9340@psf.upfronthosting.co.za>
2010-07-23 12:00:03bethardlinkissue9340 messages
2010-07-23 12:00:02bethardcreate