Skip to content

Commit

Permalink
show envvar in error hint
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinIsland committed Mar 27, 2024
1 parent f8857cb commit 19e076f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -31,6 +31,8 @@ Unreleased
- When generating a command's name from a decorated function's name, the
suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed.
:issue:`2322`
- If ``show_envvar`` then show env var in error messages.
:issue:`2695`


Version 8.1.7
Expand Down
9 changes: 6 additions & 3 deletions src/click/core.py
Expand Up @@ -2332,7 +2332,10 @@ def get_error_hint(self, ctx: Context) -> str:
indicate which param caused the error.
"""
hint_list = self.opts or [self.human_readable_name]
return " / ".join(f"'{x}'" for x in hint_list)
result = " / ".join(f"'{x}'" for x in hint_list)
if self.show_envvar:
result += f" (env var: '{self.envvar}')"
return result

def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]:
"""Return a list of completions for the incomplete value. If a
Expand Down Expand Up @@ -2372,8 +2375,8 @@ class Option(Parameter):
For single option boolean flags, the default remains hidden if
its value is ``False``.
:param show_envvar: Controls if an environment variable should be
shown on the help page. Normally, environment variables are not
shown.
shown on the help page and error messages.
Normally, environment variables are not shown.
:param prompt: If set to ``True`` or a non empty string then the
user will be prompted for input. If set to ``True`` the prompt
will be the option name capitalized.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_options.py
Expand Up @@ -527,6 +527,22 @@ def cmd(foo):
assert "bar" in choices


def test_missing_envvar(runner):
cli = click.Command(
"cli", params=[click.Option(["--foo"], envvar="bar", required=True)]
)
result = runner.invoke(cli)
assert result.exit_code == 2
assert "Error: Missing option '--foo'." in result.output
cli = click.Command(
"cli",
params=[click.Option(["--foo"], envvar="bar", show_envvar=True, required=True)],
)
result = runner.invoke(cli)
assert result.exit_code == 2
assert "Error: Missing option '--foo' (env var: 'bar')." in result.output


def test_case_insensitive_choice(runner):
@click.command()
@click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))
Expand Down

0 comments on commit 19e076f

Please sign in to comment.