show env var in error hint (#2696)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by KotlinIsland · committed by GitHub · parent 0e0c003 · Browse files · 3 files·+26 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+26 added−2 removed
- tests:+16 added−0 removed
- other:+10 added−2 removed
Provenance
- Commit
1b524bb- Tree
451b050- Author
KotlinIsland <65446343+KotlinIsland@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0e0c003
3 files changed+26 added−2 removed
modified:src/click/core.py+8 added-2 removed
2370 unmodified lines2371 actual value. This is particularly useful for dynamic options.2372 For single option boolean flags, the default remains hidden if2373 its value is ``False``.2374 :param show_envvar: Controls if an environment variable should be2375- shown on the help page. Normally, environment variables are not2376- shown.2375+ shown on the help page and error messages.2376+ Normally, environment variables are not shown.2377 :param prompt: If set to ``True`` or a non empty string then the2378 user will be prompted for input. If set to ``True`` the prompt2379 will be the option name capitalized.2380 :param confirmation_prompt: Prompt a second time to confirm the169 unmodified lines2550 hidden=self.hidden,2551 )2552 return info_dict2553 2554+ def get_error_hint(self, ctx: Context) -> str:2555+ result = super().get_error_hint(ctx)2556+ if self.show_envvar:2557+ result += f" (env var: '{self.envvar}')"2558+ return result2559+2560 def _parse_decls(2561 self, decls: cabc.Sequence[str], expose_value: bool2562 ) -> tuple[str | None, list[str], list[str]]:2563 opts = []443 unmodified linesmodified:tests/test_options.py+16 added
529 unmodified lines530 assert "foo" in choices531 assert "bar" in choices532 533 534+def test_missing_envvar(runner):535+ cli = click.Command(536+ "cli", params=[click.Option(["--foo"], envvar="bar", required=True)]537+ )538+ result = runner.invoke(cli)539+ assert result.exit_code == 2540+ assert "Error: Missing option '--foo'." in result.output541+ cli = click.Command(542+ "cli",543+ params=[click.Option(["--foo"], envvar="bar", show_envvar=True, required=True)],544+ )545+ result = runner.invoke(cli)546+ assert result.exit_code == 2547+ assert "Error: Missing option '--foo' (env var: 'bar')." in result.output548+549+550 def test_case_insensitive_choice(runner):551 @click.command()552 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))553 def cmd(foo):438 unmodified linesmodified:CHANGES.rst+2 added
41 unmodified lines42 - Keep stdout and stderr streams independent in ``CliRunner``. Always43 collect stderr output and never raise an exception. Add a new44 output` stream to simulate what the user sees in its terminal. Removes45 the ``mix_stderr`` parameter in ``CliRunner``. :issue:`2522` :pr:`2523`46+- If ``show_envvar`` then show env var in error messages.47+ :issue:`2695`48 49 50 Version 8.1.851 -------------1116 unmodified lines