Skip to content

fix: keep write_usage output for commands without args#3365

Closed
d3v07 wants to merge 1 commit intopallets:mainfrom
d3v07:fix/3360-empty-write-usage
Closed

fix: keep write_usage output for commands without args#3365
d3v07 wants to merge 1 commit intopallets:mainfrom
d3v07:fix/3360-empty-write-usage

Conversation

@d3v07
Copy link
Copy Markdown

@d3v07 d3v07 commented Apr 21, 2026

Fixes #3360

HelpFormatter.write_usage() routed empty args through the text wrapper, which produced an empty line and dropped the usage prefix entirely. Handle the no-args case directly so Usage: <prog> still renders for internal commands that take no arguments.

This adds a regression test for HelpFormatter.write_usage("program") and updates CHANGES.rst.

Verification

  • PYTHONPATH=src .venv/bin/pytest tests/test_formatting.py -q

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Click’s HelpFormatter.write_usage() behavior when called with no args, ensuring the “Usage:” prefix and program name are still emitted (regression for internal/interactive commands with no arguments).

Changes:

  • Update HelpFormatter.write_usage() to handle the no-args case without routing through the text wrapper.
  • Add a regression test for write_usage("program") with default args.
  • Document the fix in CHANGES.rst under 8.3.3.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/click/formatting.py Adds an early-return path to reliably render Usage: <prog> when args is empty.
tests/test_formatting.py Adds a regression test asserting Usage: program\n for no-args usage rendering.
CHANGES.rst Notes the bugfix and links to issue #3360.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_formatting.py


def test_help_formatter_write_usage_without_args():
formatter = click.formatting.HelpFormatter()
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

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

For consistency within this test module, consider instantiating the formatter via the public alias click.HelpFormatter() (used in test_help_formatter_write_text) instead of reaching into click.formatting.HelpFormatter(). This keeps tests aligned with the intended public API surface.

Suggested change
formatter = click.formatting.HelpFormatter()
formatter = click.HelpFormatter()

Copilot uses AI. Check for mistakes.
@davidism davidism closed this Apr 21, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty output from HelpFormatter.write_usage for a program without arguments

3 participants