This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse print_help breaks when help is blank space
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: In argparse empty choices cannot be printed in the help
View: 24444
Assigned To: Nosy List: Ido Michael, Ying Zhang, paul.j3, rhettinger, xtreak
Priority: normal Keywords:

Created on 2020-03-13 13:22 by Ying Zhang, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg364091 - (view) Author: Ying Zhang (Ying Zhang) Date: 2020-03-13 13:22
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
msg364099 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-03-13 15:40
Seems to be a duplicate of issue24444
msg364239 - (view) Author: Ido Michael (Ido Michael) * Date: 2020-03-15 14:56
Looks like both the original and this are still issues.
Can I take this?
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84136
2021-09-09 17:48:46iritkatrielsetstatus: open -> closed
type: crash -> behavior
superseder: In argparse empty choices cannot be printed in the help
resolution: duplicate
stage: resolved
2020-03-15 14:56:25Ido Michaelsetnosy: + Ido Michael
messages: + msg364239
2020-03-13 15:40:35xtreaksetnosy: + xtreak
messages: + msg364099
2020-03-13 15:38:16xtreaksetnosy: + rhettinger
2020-03-13 15:35:35paul.j3setnosy: + paul.j3
2020-03-13 13:22:58Ying Zhangcreate