paul j3 added the comment:

http://stackoverflow.com/questions/11455218
python, argparse: enable input parameter when another one has been specified

    $ python myScript.py --parameter1 value1
    $ python myScript.py --parameter1 value1 --parameter2 value2
    $ python myScript.py --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.

----------

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

Reply via email to