This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bethard
Recipients bethard
Date 2010-07-23.12:00:02
SpamBayes Score 0.00994753
Marked as misclassified No
Message-id <>
[Moved from]

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.
Date User Action Args
2010-07-23 12:00:06bethardsetrecipients: + bethard
2010-07-23 12:00:05bethardsetmessageid: <>
2010-07-23 12:00:03bethardlinkissue9340 messages
2010-07-23 12:00:02bethardcreate