bpo-38584: fix a bug in argparse with whitespace-only help messages

This commit is contained in:
Lucas Cimon 2020-11-01 12:27:29 +01:00
parent d2810054c7
commit 617616e1c5
3 changed files with 12 additions and 1 deletions

View File

@ -529,6 +529,7 @@ class HelpFormatter(object):
if action.help: if action.help:
help_text = self._expand_help(action) help_text = self._expand_help(action)
help_lines = self._split_lines(help_text, help_width) help_lines = self._split_lines(help_text, help_width)
if help_lines:
parts.append('%*s%s\n' % (indent_first, '', help_lines[0])) parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
for line in help_lines[1:]: for line in help_lines[1:]:
parts.append('%*s%s\n' % (help_position, '', line)) parts.append('%*s%s\n' % (help_position, '', line))

View File

@ -4688,6 +4688,15 @@ class TestOptionalsHelpVersionActions(TestCase):
self.assertPrintHelpExit(parser, format % '--help') self.assertPrintHelpExit(parser, format % '--help')
self.assertRaises(AttributeError, getattr, parser, 'format_version') self.assertRaises(AttributeError, getattr, parser, 'format_version')
def test_whitespace_help(self):
for formatter_class in (argparse.ArgumentDefaultsHelpFormatter, argparse.HelpFormatter,
argparse.MetavarTypeHelpFormatter,
argparse.RawDescriptionHelpFormatter, argparse.RawTextHelpFormatter):
parser = argparse.ArgumentParser(formatter_class=formatter_class)
parser.add_argument('--foo', action='store_true', help=' ')
parser.add_argument('bar', type=float, help=' ')
parser.format_help() # does not raise any error
# ====================== # ======================
# str() and repr() tests # str() and repr() tests

View File

@ -0,0 +1 @@
Fix a minor bug in argparse with whitespace-only argument help messages