classification
Title: Add ArgumentParser.add_mutually_dependence_group
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bethard, dongwm, paul.j3, r.david.murray
Priority: normal Keywords: patch

Created on 2015-01-22 05:41 by dongwm, last changed 2017-02-01 02:22 by paul.j3. This issue is now closed.

Files
File name Uploaded Description Edit
argparse_lib.patch dongwm, 2015-01-22 05:41 patch for Lib/argparse.py review
argparse_doc.patch dongwm, 2015-01-22 05:43 patch for Doc/library/argparse.rst review
argparse_test.patch dongwm, 2015-01-22 05:48 patch for Lib/test/test_argparse.py review
Messages (4)
msg234475 - (view) Author: dongwm (dongwm) * Date: 2015-01-22 05:41
Sometimes I need to use argparse like this:


>>> parser = argparse.ArgumentParser(prog='PROG')
>>> group = parser.add_mutually_dependence_group()
>>> group.add_argument('--foo')
>>> group.add_argument('--bar')
>>> parser.parse_args(['--foo', 'f', '--bar', 'b'])
Namespace(bar='b', foo='f')
>>> parser.parse_args(['--foo', 'f'])
PROG: error: --foo dependence on --bar
>>> parser.parse_args(['--bar', 'b'])
PROG: error: --bar dependence on --foo

I have some optional argument. but if any argument in a group was present on the command line. i need the others must also was present on. so i think ``add_mutually_dependence_group`` does make sense.
msg234495 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-01-22 15:38
Well, it doesn't make much sense in the English language sense.  If I got that error message I'd have no idea what was wrong.

It sounds like what you want to do is dynamically make some arguments be required, depending on whether or not other arguments are present or not.
msg238940 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-03-22 20:43
http://bugs.python.org/issue11588 is an earlier request for 'necessarily inclusive' groups.

The patches that I proposed there are more general, allowing for other logical combinations of arguments, as well as nesting groups.  As such it is more complex than your patch, but the basic testing idea is the same - 

    At the end of _parse_known_args check the group's actions
    against the ones that have been parsed.  I use the existing
    'seen_actions' or 'seen_nondefault_actions' rather than 
    check the namespace directly.

I also tried to construct this testing as a 'hook' that the user could customize.

Your test, since it uses the Namespace and Action default, rather than the 'seen_actions' set, could just as well be run AFTER parse_args.  That's the kind of testing that Stackoverflow answers often suggest for similar questions.

In some cases it's also possible to write custom Action classes that handle this kind of interdependency.
msg286581 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2017-02-01 02:22
I propose closing this with reference to http://bugs.python.org/issue11588
History
Date User Action Args
2017-02-01 02:22:42paul.j3setstatus: open -> closed

messages: + msg286581
2015-03-22 20:43:29paul.j3setnosy: + paul.j3
messages: + msg238940
2015-01-22 15:39:19r.david.murraysetversions: - Python 2.7, Python 3.2, Python 3.3, Python 3.4
2015-01-22 15:38:13r.david.murraysetnosy: + r.david.murray
messages: + msg234495
2015-01-22 05:48:29dongwmsetfiles: + argparse_test.patch
2015-01-22 05:43:46dongwmsetfiles: + argparse_doc.patch
2015-01-22 05:41:52dongwmcreate