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.RawDescriptionHelpFormatter should not delete blank lines
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: bethard, chris.jerdonek, iritkatriel, paul.j3, roysmith
Priority: normal Keywords:

Created on 2013-02-03 13:45 by roysmith, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg181267 - (view) Author: Roy Smith (roysmith) Date: 2013-02-03 13:45
The following code, when run with "--help", omits the trailing newlines from the epilog.  It should just emit the string verbatim.  If the developer didn't want the extra newlines, he/she wouldn't have put them there.


import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
                                 epilog="foo\n\n")
parser.parse_args()
msg223358 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-17 19:23
Can we have this followed up please.
msg223371 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2014-07-17 20:55
I suspect fixing this isn't going to be easy.

Extra lines are removed by the top most `formatter` function: 

    def format_help(self):
        help = self._root_section.format_help()
        if help:
            help = self._long_break_matcher.sub('\n\n', help)
            help = help.strip('\n') + '\n'
        return help

RawDescriptionHelpFormatter on the other hand just changes one deeply embedded function:

    def _fill_text(self, text, width, indent):
        return ''.join(indent + line for line in text.splitlines(keepends=True))

That `format_help` function has no way of distinguishing between '\n' that were part of the raw text, and which were added while assembling the various parts.

It could be instructive to try this version of format_help and see just how many \n are being removed:

    def format_help(self):
        help = self._root_section.format_help()
        return help
msg223388 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2014-07-17 22:53
A user could preserve blank lines by including a space (or more) in each.  That is, instead of ending with '\n', end with '\n \n'.

epilog = 'Epilog: No wrap text %(prog)s\n\tNext line\n \n'

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

I just checked a simple help.  The description and epilog get an extra blank line during assembly.  If 'epilog=None', there are 2 blank lines, which normally get reduced to one \n. 

So the philosophy of the formatter is to freely add \n between sections, regardless of whether they are empty (None) or not, and then deal with excess \n at the end.  Preserving blank lines during Raw requires rethinking that underlying philosophy.
msg408223 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-10 17:00
Adding a space as Paul suggests works. I'll close this soon if nobody objects.
msg408230 - (view) Author: Roy Smith (roysmith) Date: 2021-12-10 17:29
It's nice to see this is still being worked on after all these years :-)

I'm not actually convinced the proposed fix makes sense.  It swaps out one incorrect behavior for a different incorrect behavior.  If it really is more effort than it's worth to fix this (and given msg223371, I agree it probably is), then at least the original behavior makes more sense, as it's got years of history behind it and dropping an extra blank line is less arbitrary than adding an extra space.  

I've long since forgotten what real-world issue led me to open this, but it seems like it be easier to just document that extra trailing whitespace may not be honored.
History
Date User Action Args
2022-04-11 14:57:41adminsetgithub: 61315
2021-12-10 17:55:37iritkatrielsetstatus: open -> closed
stage: resolved
2021-12-10 17:29:34roysmithsetstatus: pending -> open

messages: + msg408230
2021-12-10 17:00:45iritkatrielsetstatus: open -> pending

nosy: + iritkatriel
messages: + msg408223

resolution: wont fix
2019-04-26 19:58:16BreamoreBoysetnosy: - BreamoreBoy
2014-07-17 22:53:23paul.j3setmessages: + msg223388
2014-07-17 20:55:43paul.j3setmessages: + msg223371
2014-07-17 19:23:21BreamoreBoysetnosy: + paul.j3, BreamoreBoy

messages: + msg223358
versions: + Python 3.4, Python 3.5
2013-02-03 19:50:28chris.jerdoneksetnosy: + bethard, chris.jerdonek
2013-02-03 13:45:40roysmithcreate