summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven.bethard <devnull@localhost>2009-04-30 15:32:42 +0000
committersteven.bethard <devnull@localhost>2009-04-30 15:32:42 +0000
commit5465e04e5572379b0ee95abebe4567f976035caa (patch)
tree8012605cfe92c6627f57391e91330d5d06f0b3bc
parentf1cfd4836ece12289c4bec4c5d36caf452268d3d (diff)
downloadargparse-5465e04e5572379b0ee95abebe4567f976035caa.tar.gz
Fix bug with wrapping mutually exclusive groups in the usage message.
-rw-r--r--argparse.py7
-rw-r--r--test/test_argparse.py46
2 files changed, 51 insertions, 2 deletions
diff --git a/argparse.py b/argparse.py
index a57c923..5ffcd42 100644
--- a/argparse.py
+++ b/argparse.py
@@ -338,8 +338,11 @@ class HelpFormatter(object):
group_actions = set()
inserts = {}
for group in groups:
- start = actions.index(group._group_actions[0])
- if start != -1:
+ try:
+ start = actions.index(group._group_actions[0])
+ except ValueError:
+ continue
+ else:
end = start + len(group._group_actions)
if actions[start:end] == group._group_actions:
for action in group._group_actions:
diff --git a/test/test_argparse.py b/test/test_argparse.py
index dd66239..dc3ff4b 100644
--- a/test/test_argparse.py
+++ b/test/test_argparse.py
@@ -33,6 +33,9 @@ class TestCase(unittest.TestCase):
def assertEqual(self, obj1, obj2):
if obj1 != obj2:
+ print('')
+ print(repr(obj1))
+ print(repr(obj2))
print(obj1)
print(obj2)
super(TestCase, self).assertEqual(obj1, obj2)
@@ -1834,6 +1837,49 @@ class TestMutuallyExclusiveSimple(MEMixin, TestCase):
'''
+class TestMutuallyExclusiveLong(MEMixin, TestCase):
+
+ def get_parser(self, required=None):
+ parser = ErrorRaisingArgumentParser(prog='PROG')
+ parser.add_argument('--abcde', help='abcde help')
+ parser.add_argument('--fghij', help='fghij help')
+ group = parser.add_mutually_exclusive_group(required=required)
+ group.add_argument('--klmno', help='klmno help')
+ group.add_argument('--pqrst', help='pqrst help')
+ return parser
+
+ failures = ['--klmno X --pqrst Y']
+ successes = [
+ ('--klmno X', NS(abcde=None, fghij=None, klmno='X', pqrst=None)),
+ ('--abcde Y --klmno X',
+ NS(abcde='Y', fghij=None, klmno='X', pqrst=None)),
+ ('--pqrst X', NS(abcde=None, fghij=None, klmno=None, pqrst='X')),
+ ('--pqrst X --fghij Y',
+ NS(abcde=None, fghij='Y', klmno=None, pqrst='X')),
+ ]
+ successes_when_not_required = [
+ ('', NS(abcde=None, fghij=None, klmno=None, pqrst=None)),
+ ]
+
+ usage_when_not_required = '''\
+ usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ] [--klmno KLMNO | --pqrst
+ PQRST]
+ '''
+ usage_when_required = '''\
+ usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ] (--klmno KLMNO | --pqrst
+ PQRST)
+ '''
+ help = '''\
+
+ optional arguments:
+ -h, --help show this help message and exit
+ --abcde ABCDE abcde help
+ --fghij FGHIJ fghij help
+ --klmno KLMNO klmno help
+ --pqrst PQRST pqrst help
+ '''
+
+
class TestMutuallyExclusiveFirstSuppressed(MEMixin, TestCase):
def get_parser(self, required):