[New-bugs-announce] [issue23487] argparse: add_subparsers 'action' broken

BJ Dierkes report at bugs.python.org
Fri Feb 20 08:24:03 CET 2015


New submission from BJ Dierkes:

Related: http://bugs.python.org/issue9253

I came across issue9253 in trying to implement a default action for a subparser namespace.  In the absence of a 'default' option, I thought that it may be possible by adding an 'action' to 'add_subparsers'.  Per the documentation this should be possible:

https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_subparsers

[QUOTE]
action - the basic type of action to be taken when this argument is 
encountered at the command line
[/QUOTE]


That said, custom actions on 'add_subparsers' doesn't appear to work at all:

    import argparse

    class CustomAction(argparse.Action):
        def __call__(self, parser, namespace, values, option_string=None):
            print('Inside CustomAction')
            setattr(namespace, self.dest, values)

    root_parser = argparse.ArgumentParser(prog='myapp')
    sub_parser = root_parser.add_subparsers(dest='commands', action=CustomAction)
    args = root_parser.parse_args()


Produces:

$ python argtest.py --help
Traceback (most recent call last):
  File "argtest.py", line 46, in <module>
    sub_parser = root_parser.add_subparsers(dest='commands', action=CustomAction)
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1661, in add_subparsers
    action = parsers_class(option_strings=[], **kwargs)
TypeError: __init__() got an unexpected keyword argument 'prog'



Erroneous documentation maybe?  Tested the same on Python 2.7 and 3.3.

----------
components: Library (Lib)
messages: 236254
nosy: derks
priority: normal
severity: normal
status: open
title: argparse: add_subparsers 'action' broken
type: behavior
versions: Python 2.7, Python 3.3

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


More information about the New-bugs-announce mailing list