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 paul.j3
Recipients Mickaël.Falck, bethard, chris.jerdonek, paul.j3
Date 2014-10-10.04:35:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1412915704.69.0.77869363943.issue17204@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2014-10-10 04:35:04paul.j3setrecipients: + paul.j3, bethard, chris.jerdonek, Mickaël.Falck
2014-10-10 04:35:04paul.j3setmessageid: <1412915704.69.0.77869363943.issue17204@psf.upfronthosting.co.za>
2014-10-10 04:35:04paul.j3linkissue17204 messages
2014-10-10 04:35:04paul.j3create