fix: write_usage outputs usage line when args is empty#3386
Closed
blackwell-systems wants to merge 1 commit intopallets:mainfrom
Closed
fix: write_usage outputs usage line when args is empty#3386blackwell-systems wants to merge 1 commit intopallets:mainfrom
blackwell-systems wants to merge 1 commit intopallets:mainfrom
Conversation
When write_usage is called with no args (the default), wrap_text receives an empty string and returns an empty string, dropping the initial_indent that contains the usage prefix. The usage line vanishes entirely. Handle the empty-args case before calling wrap_text: write the usage prefix directly (stripped of trailing space) followed by a newline. Fixes #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.
Motivation
HelpFormatter.write_usage("program")with noargsoutputs an empty line instead ofUsage: program. This affects CLI tools with commands that take no arguments (e.g.exit,help).The bug
write_usagepassesargs=""towrap_text(), which returns""for empty input, dropping theinitial_indent(which contains the usage prefix). The entire usage line vanishes.Fix
Handle the empty-args case before calling
wrap_text: writeusage_prefix.rstrip()directly. The.rstrip()removes the trailing space that is only needed to separate the prefix from arguments. All other behavior is unchanged.Tests
Added three tests in
tests/test_formatting.py:test_write_usage_no_args: the bug casetest_write_usage_with_args: regression checktest_write_usage_no_args_custom_prefix: edge case with custom prefixAll 20 formatting tests pass.
Fixes #3360