]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 13 Oct 2021 17:15:43 +0000 (10:15 -0700)
committerGitHub <noreply@github.com>
Wed, 13 Oct 2021 17:15:43 +0000 (19:15 +0200)
(cherry picked from commit 6fafc25aea8689048314b5bf7a9bb986bb1ce238)

Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2021-08-30-00-19-23.bpo-24444.Ki4bgz.rst [new file with mode: 0644]

index ce4635adcbfad387de1464b7cf33a77a468450ec..f1f6f3227bd17fb27d6de3df4ae3659aa0dfb229 100644 (file)
@@ -526,12 +526,13 @@ class HelpFormatter(object):
         parts = [action_header]
 
         # if there was help for the action, add lines of help text
-        if action.help:
+        if action.help and action.help.strip():
             help_text = self._expand_help(action)
-            help_lines = self._split_lines(help_text, help_width)
-            parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
-            for line in help_lines[1:]:
-                parts.append('%*s%s\n' % (help_position, '', line))
+            if help_text:
+                help_lines = self._split_lines(help_text, help_width)
+                parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
+                for line in help_lines[1:]:
+                    parts.append('%*s%s\n' % (help_position, '', line))
 
         # or add a newline if the description doesn't end with one
         elif not action_header.endswith('\n'):
index 0927281f69c925292f08b3be4ce9845013fd30ea..6680feb016a4d86609af49cc42737bb489a8ef46 100644 (file)
@@ -2145,6 +2145,42 @@ class TestAddSubparsers(TestCase):
                               wrap\N{NO-BREAK SPACE}at non-breaking spaces
         '''))
 
+    def test_help_blank(self):
+        # Issue 24444
+        parser = ErrorRaisingArgumentParser(
+            prog='PROG', description='main description')
+        parser.add_argument(
+            'foo',
+            help='    ')
+        self.assertEqual(parser.format_help(), textwrap.dedent('''\
+            usage: PROG [-h] foo
+
+            main description
+
+            positional arguments:
+              foo         
+
+            options:
+              -h, --help  show this help message and exit
+        '''))
+
+        parser = ErrorRaisingArgumentParser(
+            prog='PROG', description='main description')
+        parser.add_argument(
+            'foo', choices=[],
+            help='%(choices)s')
+        self.assertEqual(parser.format_help(), textwrap.dedent('''\
+            usage: PROG [-h] {}
+
+            main description
+
+            positional arguments:
+              {}          
+
+            options:
+              -h, --help  show this help message and exit
+        '''))
+
     def test_help_alternate_prefix_chars(self):
         parser = self._get_parser(prefix_chars='+:/')
         self.assertEqual(parser.format_usage(),
diff --git a/Misc/NEWS.d/next/Library/2021-08-30-00-19-23.bpo-24444.Ki4bgz.rst b/Misc/NEWS.d/next/Library/2021-08-30-00-19-23.bpo-24444.Ki4bgz.rst
new file mode 100644 (file)
index 0000000..efcacb8
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed an error raised in :mod:`argparse` help display when help for an
+option is set to 1+ blank spaces or when *choices* arg is an empty container.