[issue39955] argparse print_help breaks when help is blank space

Ying Zhang report at bugs.python.org
Fri Mar 13 09:22:58 EDT 2020


New submission from Ying Zhang <zhangyingmath at gmail.com>:

Code is attached. Comments in line. 

from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

parser1 = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser1.add_argument('--foo',
                        default='default_value_for_foo', required=False)
# this will not print the default value for foo. I think this is not the  most natural choice, given that the user has asked for ArgumentDefaultsHelpFormatter, but acceptable since the user didn't define help here. 

parser1.add_argument('--bar', help='',
                        default='default_value_for_bar', required=False)
# this will not print the default value for bar. Again, acceptable but I feel not the most natural. 

parser1.add_argument('--baz', help=' ',
                        default='default_value_for_baz', required=False)
# this will print the default value for baz. 


parser1.print_help()


parser2 = ArgumentParser()
parser2.add_argument('--baz', help=' ',
                        default='default_value_for_baz', required=False)

# this will break, which surprises me.
parser2.print_help()

----------------

Result:
python argparse_help_demo.py 
usage: argparse_help_demo.py [-h] [--foo FOO] [--bar BAR] [--baz BAZ]

optional arguments:
  -h, --help  show this help message and exit
  --foo FOO
  --bar BAR
  --baz BAZ   (default: default_value_for_baz)
Traceback (most recent call last):
  File "argparse_help_demo.py", line 21, in <module>
    parser2.print_help()
  File "/nfs/statbuild/zhangyi/conda_envs/net37_env0/lib/python3.7/argparse.py", line 2474, in print_help
    self._print_message(self.format_help(), file)
  File "/nfs/statbuild/zhangyi/conda_envs/net37_env0/lib/python3.7/argparse.py", line 2458, in format_help
    return formatter.format_help()
  File "/nfs/statbuild/zhangyi/conda_envs/net37_env0/lib/python3.7/argparse.py", line 284, in format_help
    help = self._root_section.format_help()
  File "/nfs/statbuild/zhangyi/conda_envs/net37_env0/lib/python3.7/argparse.py", line 215, in format_help
    item_help = join([func(*args) for func, args in self.items])
  File "/nfs/statbuild/zhangyi/conda_envs/net37_env0/lib/python3.7/argparse.py", line 215, in <listcomp>
    item_help = join([func(*args) for func, args in self.items])
  File "/nfs/statbuild/zhangyi/conda_envs/net37_env0/lib/python3.7/argparse.py", line 215, in format_help
    item_help = join([func(*args) for func, args in self.items])
  File "/nfs/statbuild/zhangyi/conda_envs/net37_env0/lib/python3.7/argparse.py", line 215, in <listcomp>
    item_help = join([func(*args) for func, args in self.items])
  File "/nfs/statbuild/zhangyi/conda_envs/net37_env0/lib/python3.7/argparse.py", line 527, in _format_action
    parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
IndexError: list index out of range

----------
components: Library (Lib)
messages: 364091
nosy: Ying Zhang
priority: normal
severity: normal
status: open
title: argparse print_help breaks when help is blank space
type: crash
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39955>
_______________________________________


More information about the Python-bugs-list mailing list