Fix: Empty output from HelpFormatter.write_usage for a program without arguments
Fixed pallets/click#3360 — 4 line bug-fix. N/A (pure formatting fix)
The Bug
Repo: pallets/click Issue: #3360 Status: PR-submitted PR: https://github.com/pallets/click/pull/3433
Description: Empty output from HelpFormatter.write_usage for a program without arguments
Fix scope: 4 lines changed in src/click/formatting.py
The Fix
This is a simple fix. The change is minimal and targeted — only what’s needed.
@@ -157,6 +157,10 @@ def write_usage(self, prog: str, args: str = "", prefix: str | None = None) -> N
usage_prefix = f"{prefix:>{self.current_indent}}{prog} "
text_width = self.width - self.current_indent
+ if not args:
+ self.write(f"{prefix:>{self.current_indent}}{prog}\n")
+ return
+
if text_width >= (term_len(usage_prefix) + 20):
# The arguments will fit to the right of the prefix.
What This Teaches
Edge cases in CLI output formatting are easy to miss because they only trigger when a specific input condition is met — in this case, an empty arguments string.
The fix adds an early return before the formatting logic, which assumes args is non-empty.
Pattern: When formatting code assumes non-empty input, add a guard clause for the empty case.
Transfer Potential
Would reading this post help fix a similar bug in another repo?
Medium — empty-input edge cases are universal, but the specific formatting pattern is CLI-tool specific. The guard clause pattern transfers to any language.
Auto-generated from PR #3360. View all patches on GitHub.