Skip to content

Conversation

@davesteele
Copy link
Contributor

@davesteele davesteele commented Sep 7, 2020

This adds the FlexiHelpFormatter class to argparse.

It supports wrapping text, while preserving paragraphs. Bullet lists are supported.

There are a number of differences, relative to the latest patch in the issue report:

  • single line feeds in a paragraph are allowed
  • the code is refactored to avoid duplication
  • test failure fixes (mostly whitespace)

Tests and documentation are included.

https://pypi.org/project/argparse-formatter/

https://bugs.python.org/issue12806

@davesteele davesteele changed the title bpo12806: Add argparse FlexiHelpFormatter bpo-12806: Add argparse FlexiHelpFormatter Sep 7, 2020
@davesteele davesteele force-pushed the bpo12806-flexihelpformatter branch 2 times, most recently from d82ce68 to da19cdd Compare April 9, 2021 17:04
benjaminhwilliams added a commit to dials/dials_scratch that referenced this pull request Jun 23, 2021
Unfortunately, argparse's proposed FlexiHelpFormatter does not yet
exist, see python/cpython#22129.
@davesteele davesteele force-pushed the bpo12806-flexihelpformatter branch from 96c0799 to 008764b Compare July 25, 2021 15:29
@ghost
Copy link

ghost commented Feb 27, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 18, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

davesteele and others added 6 commits April 27, 2025 13:47
@davesteele davesteele force-pushed the bpo12806-flexihelpformatter branch from a3ac677 to a21117a Compare April 27, 2025 17:49
Copy link
Member

@savannahostrowski savannahostrowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I just saw this has been opened for awhile but I think this is a great formatter addition. I have a couple of comment but I've played around with this quite a bit and it looks pretty solid!

-h, --help show this help message and exit


.. versionadded:: 3.13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. versionadded:: 3.13
.. versionadded:: 3.15

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit 8e20754.


.. class:: RawDescriptionHelpFormatter
RawTextHelpFormatter
FlexiHelpFormatter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that FlexiHelpFormatter is descriptive enough. I'd suggest something like:

Suggested change
FlexiHelpFormatter
ParagraphHelpFormatter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to ParagraphHelpFormatter in 2b30019.

Lib/argparse.py Outdated
Comment on lines 677 to 678
lines = self._para_reformat(text, width)
return "\n".join(lines)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lines = self._para_reformat(text, width)
return "\n".join(lines)
lines = self._para_reformat(text, width - len(indent))
return "\n".join(indent + line for line in lines)

As is, this eats any form of indentation that the user might have specified, which I think we want to preserve.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit 2a53ad4.

one. If you wish to preserve multiple blank lines, add spaces between the
newlines.

:class:`FlexiHelpFormatter` wraps description and help text like the default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also make it more clear that consecutive non-blank lines at the same indentation without supported bullet markers are merged into one paragraph. Users should add blank lines if they want to preserve formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in ec6d2d7.

Comment on lines 360 to 362
formatter, while preserving paragraphs and supporting bulleted lists. Bullet
list items are marked by the use of the "*", "-", "+", or ">" characters, or a
single non-whitespace character followed by a "."::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
formatter, while preserving paragraphs and supporting bulleted lists. Bullet
list items are marked by the use of the "*", "-", "+", or ">" characters, or a
single non-whitespace character followed by a "."::
formatter, while preserving paragraphs and supporting bulleted lists. Bullet
list items are recognized by markers such as "*", "-", "+", or ">" characters, or
by alphanumeric sequences followed by "." or ")" (e.g. 1., a), iv.)::

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I nuked the last part of your change. I thought it was mine, and has trouble reading it.

... * See?
... """)
>>> parser.print_help()
usage: PROG [-h] option
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
usage: PROG [-h] option
usage: PROG [-h] argument

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit ae7c70f.

wrap:
* See?

optional arguments:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
optional arguments:
options:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit f64e966.

@bedevere-app
Copy link

bedevere-app bot commented Dec 14, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@savannahostrowski savannahostrowski changed the title bpo-12806: Add argparse FlexiHelpFormatter GH-57015: Add argparse FlexiHelpFormatter Dec 14, 2025
davesteele and others added 10 commits December 14, 2025 21:01
Co-authored-by: Savannah Ostrowski <savannah@python.org>
Co-authored-by: Savannah Ostrowski <savannah@python.org>
Co-authored-by: Savannah Ostrowski <savannah@python.org>
Co-authored-by: Savannah Ostrowski <savannah@python.org>
Co-authored-by: Savannah Ostrowski <savannah@python.org>
It led to headaches in unittest.
@davesteele
Copy link
Contributor Author

I have made the requested changes; please review again.

@bedevere-app
Copy link

bedevere-app bot commented Dec 19, 2025

Thanks for making the requested changes!

@savannahostrowski: please review the changes made to this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants