Author paul.j3
Recipients John.Didion, bethard, manveru, paul.j3, xuanji
Date 2014-02-25.23:33:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
python, argparse: enable input parameter when another one has been specified

    $ python --parameter1 value1
    $ python --parameter1 value1 --parameter2 value2
    $ python --parameter2 value2  # error

This is an example where a 'mutually inclusive group' wouldn't quite do the job.  

The proposed answers mostly use a custom Action.  I'd lean toward an after-the-parse test of the namespace.

With the patch I proposed this could be implemented with:

    a1 = parser.add_argument("--parameter1")
    a2 = parser.add_argument("--parameter2")
    def test(parser, seen_actions, *args):
        if a2 in seen_actions and a1 not in seen_actions:
            parser.error('parameter2 requires parameter1')
    parser.register('cross_tests', 'test', test)

One poster on that thread claimed that the use of 'a1 = parser.add_argument...' is using an undocumented feature.  The fact that `add_argument` returns an `action` object, should be illustrated in the documentation, and may be explicitly noted.  I'll have to review the documentation to see if this is the case.
Date User Action Args
2014-02-25 23:33:01paul.j3setrecipients: + paul.j3, bethard, xuanji, John.Didion, manveru
2014-02-25 23:33:01paul.j3setmessageid: <>
2014-02-25 23:33:01paul.j3linkissue11588 messages
2014-02-25 23:33:00paul.j3create