Fix HelpFormatter.write_usage with no args#3382
Closed
ibondarenko1 wants to merge 1 commit intopallets:mainfrom
Closed
Fix HelpFormatter.write_usage with no args#3382ibondarenko1 wants to merge 1 commit intopallets:mainfrom
ibondarenko1 wants to merge 1 commit intopallets:mainfrom
Conversation
When write_usage was called without args, wrap_text was passed an empty string, which causes textwrap.TextWrapper.fill to drop the initial_indent and produce a single blank line. Short-circuit when args is empty, writing the usage prefix directly so the output is 'Usage: prog' instead of an empty line. Closes pallets#3360
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3360.
When
HelpFormatter.write_usageis called without anargsvalue, the resulting output is a single blank line instead of the expectedUsage: <prog>line. This happens becausewrap_text("", initial_indent=usage_prefix, ...)ends up callingtextwrap.TextWrapper.fill(""), which returns""and discardsinitial_indent.This short-circuits the empty-args path and writes the usage prefix directly. The trailing space added between
progandargsisrstrip-ped before the newline so the output is exactlyUsage: <prog>.Reproducer (from #3360)
Usage: programTests
Added three tests in
tests/test_formatting.py:test_help_formatter_write_usage_no_args— default prefix.test_help_formatter_write_usage_no_args_custom_prefix— custom prefix preserved.test_help_formatter_write_usage_no_args_indented— indent applied via theprefix:>{indent}format spec is preserved (rstriponly strips trailing whitespace).pytest -x -qpasses (1389 passed, 73 skipped, 1 xfailed).