Closed
Description
Environment
$ python --version
Python 3.8.10
$ uname -a
Linux 4.4.0-19041-Microsoft #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 x86_64 x86_64 GNU/Linux
Example
# example.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-e', help=' ')
args = parser.parse_args()
$ python example.py -h
Traceback (most recent call last):
File "example.py", line 6, in <module>
args = parser.parse_args()
File "/usr/lib/python3.8/argparse.py", line 1780, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python3.8/argparse.py", line 1812, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib/python3.8/argparse.py", line 2018, in _parse_known_args
start_index = consume_optional(start_index)
File "/usr/lib/python3.8/argparse.py", line 1958, in consume_optional
take_action(action, args, option_string)
File "/usr/lib/python3.8/argparse.py", line 1886, in take_action
action(self, namespace, argument_values, option_string)
File "/usr/lib/python3.8/argparse.py", line 1056, in __call__
parser.print_help()
File "/usr/lib/python3.8/argparse.py", line 2506, in print_help
self._print_message(self.format_help(), file)
File "/usr/lib/python3.8/argparse.py", line 2490, in format_help
return formatter.format_help()
File "/usr/lib/python3.8/argparse.py", line 294, in format_help
help = self._root_section.format_help()
File "/usr/lib/python3.8/argparse.py", line 225, in format_help
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.8/argparse.py", line 225, in <listcomp>
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.8/argparse.py", line 225, in format_help
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.8/argparse.py", line 225, in <listcomp>
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.8/argparse.py", line 543, in _format_action
parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
IndexError: list index out of range
Other examples that cause errors:
parser.add_argument('-e', help='\n')
parser.add_argument('-e', help='\r')
parser.add_argument('-e', help='\n\n\n\n')
parser.add_argument('-e', help=' ')
parser.add_argument('-e', help=' \n\r ')
Expected Behavior
I would expect a whitespace-only help message to behave similarly to the empty string:
# example.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-e', help='')
args = parser.parse_args()
$ python example.py -h
usage: example.py [-h] [-e E]
optional arguments:
-h, --help show this help message and exit
-e E