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.

Author Ying Zhang
Recipients Ying Zhang
Date 2020-03-13.13:22:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1584105778.75.0.565516989827.issue39955@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2020-03-13 13:22:58Ying Zhangsetrecipients: + Ying Zhang
2020-03-13 13:22:58Ying Zhangsetmessageid: <1584105778.75.0.565516989827.issue39955@roundup.psfhosted.org>
2020-03-13 13:22:58Ying Zhanglinkissue39955 messages
2020-03-13 13:22:58Ying Zhangcreate