Skip to content

show_envvar=True Drops Environment Variable from Error Messages When Using auto_envvar_prefix #3914

Open

Description

@chinmayp995

Bug Description

When an option is configured with show_envvar=True and the command or context defines an auto_envvar_prefix, the help output correctly lists the derived environment variable (e.g. [env var: MYAPP_PORT]). However, if that option is omitted or has an invalid value at runtime, the error hint produced by Option.get_error_hint() omits the environment variable entirely (Error: Missing option '--port'.), whereas declaring envvar="MYAPP_PORT" explicitly outputs Error: Missing option '--port' (env var: 'MYAPP_PORT')..
show_envvar is documented to show the environment variable on both the help page and in error messages. Option.get_error_hint(ctx) receives ctx, but currently only checks self.envvar and does not check ctx.auto_envvar_prefix.

Minimal reproducible example

import click
from click.testing import CliRunner
@click.command(context_settings={"auto_envvar_prefix": "MYAPP"})
@click.option("--port", type=int, required=True, show_envvar=True)
def cli(port):
    click.echo(f"port={port}")
runner = CliRunner()
# Help correctly displays derived env var:
# --port INTEGER  [env var: MYAPP_PORT; required]
print(runner.invoke(cli, ["--help"]).output)
# Error output omits the env var:
# Error: Missing option '--port'.
print(runner.invoke(cli, []).output)

Expected behavior

When show_envvar=True is enabled and auto_envvar_prefix is set on the context, the missing parameter and bad parameter error messages should include the resolved environment variable hint:

Error: Missing option '--port' (env var: 'MYAPP_PORT').

Matching the output that occurs when envvar="MYAPP_PORT" is passed directly.

Environment:

  • Python version: 3.12 / 3.13 / 3.14
  • Click version: 8.5.1 / main

Activity

  1. deleted a comment from chinmayp995 on Nov 10, 2026
  2. deleted a comment from amitcoder79 on Nov 10, 2026
  3. deleted a comment from Hayabusa-YL on Nov 11, 2026
  4. deleted a comment from Nani-2007-code on Nov 11, 2026
  5. deleted a comment from Eliot5566 on Nov 12, 2026
  6. deleted a comment from Eliot5566 on Nov 12, 2026
  7. deleted a comment from Seemanshi2104 on Nov 13, 2026
  8. deleted a comment from shehryaur on Nov 14, 2026
  9. deleted a comment from Duo-ee on Nov 14, 2026
  10. deleted a comment from klnmurthi12345 on Nov 16, 2026
  11. locked as spam and limited conversation to collaborators on Nov 16, 2026
  12. added 3 commits that reference this issue on Nov 19, 2026
    8b42756
    e06e56f
    8833d2b
  13. added 2 commits that reference this issue on Nov 21, 2026
    274726f
    aca4348
  14. added a commit that references this issue on Nov 23, 2026
    c336247
  15. added a commit that references this issue on Nov 24, 2026
    464a69f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions