paul j3 added the comment:

In the attached patch I modified 'add_parser' to take a 'parser' keyword 
parameter.  If given, it is used as the parser, rather than create a new one.  
Thus an existing parser, or one created with a custom ArgumentParser class, 
could be used as a subparser.  

In this sample script, a parent parser is used as subparser in 2 different ways 
- via the parent mechanism, and with this new add_parser parameter.

    parent = argparse.ArgumentParser()
    parent.add_argument('-f')
    parent.add_argument('bar')

    parser = argparse.ArgumentParser()
    sp = parser.add_subparsers(dest='cmd')
    p1 = sp.add_parser('cmd1', add_help=False, parents=[parent])
    p2 = sp.add_parser('cmd2', parser=parent)
    parent.add_argument('test')
    assert p2 is parent
    assert p1 is not parent
    print(parser.parse_args())

This change passes existing unittests.  I don't think there are any backward 
compatibility issues.

----------
keywords: +patch
Added file: http://bugs.python.org/file36858/patch0.diff

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue17204>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to