[issue10984] argparse add_mutually_exclusive_group should accept existing arguments to register conflicts
paul j3
report at bugs.python.org
Tue Jun 3 07:44:01 CEST 2014
paul j3 added the comment:
Another way to add an existing Action to a group is to modify the 'add_argument' method for the Group subclass. For example we could add this to the _MutuallyExclusiveGroup class:
def add_argument(self, *args, **kwargs):
# allow adding a prexisting Action
if len(args) and isinstance(args[0], Action):
action = args[0]
return self._group_actions.append(action)
else:
return super(_MutuallyExclusiveGroup, self).add_argument(*args, **kwargs)
With this the 1st example might be written as:
group1 = parser.add_mutually_exclusive_group()
a_action = parser.add_argument('-a')
c_action = parser.add_argument('-c')
group2 = parser.add_mutually_exclusive_group()
group2.add_argument(a_action)
d_action = parser.add_argument('-d')
This might be more intuitive to users.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10984>
_______________________________________
More information about the Python-bugs-list
mailing list