Title: argparse.RawDescriptionHelpFormatter should not delete blank lines
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bethard, chris.jerdonek, paul.j3, roysmith
Priority: normal Keywords:

Created on 2013-02-03 13:45 by roysmith, last changed 2019-04-26 19:58 by BreamoreBoy.

Messages (4)
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,
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.
Date User Action Args
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