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 derks
Recipients derks
Date 2015-02-20.07:24:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1424417043.22.0.71313795028.issue23487@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2015-02-20 07:24:03derkssetrecipients: + derks
2015-02-20 07:24:03derkssetmessageid: <1424417043.22.0.71313795028.issue23487@psf.upfronthosting.co.za>
2015-02-20 07:24:03derkslinkissue23487 messages
2015-02-20 07:24:02derkscreate