Skip to content

argparse.RawTextHelpFormatter does not maintain lines separated by more than one newline #75511

Closed

Description

@anntzer
mannequin
BPO 31330
Nosy @bitdancer, @anntzer, @elenaoat
PRs
  • bpo-31330: Clarify that multiple new lines are reduced to one. #3272
  • [2.7] bpo-31330: Clarify that RawTextHelpFormatter collapses repeated newlines. (GH-3272) #3428
  • [3.6] bpo-31330: Clarify that RawTextHelpFormatter collapses repeated newlines. (GH-3272) #3429
  • Files
  • Screen Shot 2017-09-02 at 10.45.48.png
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-09-07.23:18:33.574>
    created_at = <Date 2017-09-02.03:19:37.683>
    labels = ['3.7', 'library']
    title = 'argparse.RawTextHelpFormatter does not maintain lines separated by more than one newline'
    updated_at = <Date 2017-09-07.23:18:33.573>
    user = 'https://github.com/anntzer'

    bugs.python.org fields:

    activity = <Date 2017-09-07.23:18:33.573>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-09-07.23:18:33.574>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2017-09-02.03:19:37.683>
    creator = 'Antony.Lee'
    dependencies = []
    files = ['47117']
    hgrepos = []
    issue_num = 31330
    keywords = ['patch']
    message_count = 10.0
    messages = ['301158', '301161', '301162', '301163', '301164', '301465', '301623', '301624', '301638', '301640']
    nosy_count = 4.0
    nosy_names = ['r.david.murray', 'paul.j3', 'Antony.Lee', 'Elena.Oat']
    pr_nums = ['3272', '3428', '3429']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue31330'
    versions = ['Python 3.6', 'Python 3.7']

    Activity

    1. anntzer commented on Sep 2, 2017

      anntzermannequin
      MannequinAuthor

      The doc for argparse.RawTextHelpFormatter states that it "maintains whitespace for all sorts of help text, including argument descriptions." But the following example (which outputs "Foo", "Bar", and "Baz", each separated by exactly one empty line) shows that paragraphs separated by more than one empty line will only be separated in the output text by a single empty line.

      import argparse
      parser = argparse.ArgumentParser(
          prog='PROG',
          formatter_class=argparse.RawTextHelpFormatter,
          description='''\
      Foo

      Bar

      Baz''')
      parser.print_help()

    2. added
      stdlib
      on Sep 2, 2017
    3. elenaoat commented on Sep 2, 2017

      elenaoatmannequin
      Mannequin

      It maintains *whitespace*, not the new lines.
      I've tested this using whitespaces and I think it keeps them according to the original description. See the image attached.

    4. bitdancer commented on Sep 2, 2017

      @bitdancer
      Member

      If that is the intended definition of "whitespace" in this context (I have no opinion on that so far), then the docs need amplification, because in general "whitespace" includes newlines. On the other hand, this might be considered a bug in the code and not the docs. However, changing it has backward compatibility implications, so we might go for a doc fix regardless.

    5. paulj3 commented on Sep 2, 2017

      paulj3mannequin
      Mannequin

      I've seen this before, either in another bug/issue or a Stackoverflow question.

      The basic change in RawTextHelpFormatter is to turn off the line wrapping for texts like the description. Multiple newlines are preserved at this step.

      But after assembling all the pieces (usage, description, help, epilog) the formatter strips out duplicate newlines. This is needed because the process of assembly freely inserts newlines between the pieces, regardless of whether they are blank or not. It can't, or at least doesn't try to, distinguish between newlines within a block like the description, and newlines between blocks.

      If someone could come up with a simple patch, great. Or a simple change to the doc wording. But I don't think this issue merits a larger fix.

    6. paulj3 commented on Sep 2, 2017

      paulj3mannequin
      Mannequin

      http://bugs.python.org/issue17113
      argparse.RawDescriptionHelpFormatter should not delete blank lines

      This provides more details on why this is happening.

      I suggest using adding a space between newlines, thus: '\n \n'. HelpFormatter.format_help is just looking for '\n\n'.

    7. elenaoat commented on Sep 6, 2017

      elenaoatmannequin
      Mannequin

      I've created a pull request. Could someone review it please?

    8. bitdancer commented on Sep 7, 2017

      @bitdancer
      Member

      New changeset 397c467 by R. David Murray (Elena Oat) in branch 'master':
      bpo-31330: Clarify that RawTextHelpFormatter collapses repeated newlines. (bpo-3272)
      397c467

    9. bitdancer commented on Sep 7, 2017

      @bitdancer
      Member

      Thanks, Elena.

    10. bitdancer commented on Sep 7, 2017

      @bitdancer
      Member

      New changeset 82cae7c by R. David Murray (Miss Islington (bot)) in branch '2.7':
      [2.7] bpo-31330: Clarify that RawTextHelpFormatter collapses repeated newlines. (GH-3272) (GH-3428)
      82cae7c

    11. bitdancer commented on Sep 7, 2017

      @bitdancer
      Member

      New changeset e89b35d by R. David Murray (Miss Islington (bot)) in branch '3.6':
      [3.6] bpo-31330: Clarify that RawTextHelpFormatter collapses repeated newlines. (GH-3272) (GH-3429)
      e89b35d

    12. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)stdlib

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions