[docs] [issue23487] argparse: add_subparsers 'action' broken

paul j3 report at bugs.python.org
Wed Mar 25 17:53:44 CET 2015


paul j3 added the comment:

As to the nature of the error when 'add_subparsers' is given an 'action' parameter:

'add_subparsers' does several things to 'kwargs' before it passes them to the relevant Action class.

     def add_subparsers(self, **kwargs):
        # adds 'parser_class'
        # removes 'title', 'description' (used in an argument group)
        # add 'prog'
        parsers_class = self._pop_action_class(kwargs, 'parsers')
        action = parsers_class(option_strings=[], **kwargs)

What I wrote earlier about using the registry is partly wrong.  The Action class is determined by either the 'action' parameter or the registry entry.

     In [17]: p._pop_action_class({}, 'parsers')
     Out[17]: argparse._SubParsersAction

     In [18]: p._pop_action_class({'action':'test'}, 'parsers')
     Out[18]: 'test'

So the 'action' parameter works - if you specify a compatible Action class.

    sp=p.add_subparsers(dest='cmd',action=argparse._SubParsersAction)

Such a class must have the same __init__ signature, otherwise you'll get errors such the OP's.

It might be worth rewriting the documentation line so this is clearer.  Otherwise I recommend closing this issue.






     action = parsers_class(option_strings=[], **kwargs)

----------

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


More information about the docs mailing list