Message111285
| Author |
bethard |
| Recipients |
bethard |
| Date |
2010年07月23日.12:00:02 |
| SpamBayes Score |
0.009947528 |
| 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:06 | bethard | set | recipients:
+ bethard |
| 2010年07月23日 12:00:05 | bethard | set | messageid: <1279886405.92.0.918578525345.issue9340@psf.upfronthosting.co.za> |
| 2010年07月23日 12:00:03 | bethard | link | issue9340 messages |
| 2010年07月23日 12:00:02 | bethard | create |
|