Skip to content
Myr

Added functionality to set flag_value correctly when using envvar (#2788)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by chips-wq · committed by GitHub · parent 3800083 · Browse files · 3 files+25 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Andreas Backx <andreas@backx.org>

Diff stats

3 files+25 added−3 removed

  • tests:+13 added−0 removed
  • other:+12 added−3 removed

3 files+25 added−3 removed

modified:src/click/core.py+9 added-3 removed
2428 unmodified lines
2429 :param help: the help string.2430 :param hidden: hide this option from help outputs.2431 :param attrs: Other command arguments described in :class:`Parameter`.24322433 .. versionchanged:: 8.1.02433 .. versionchanged:: 8.22434 ``envvar`` used with ``flag_value`` will always use the ``flag_value``,2435 previously it would use the value of the environment variable.24362437 .. versionchanged:: 8.12438 Help text indentation is cleaned here instead of only in the2439 ``@option`` decorator.24402437 .. versionchanged:: 8.1.02441 .. versionchanged:: 8.12442 The ``show_default`` parameter overrides2443 ``Context.show_default``.24442441 .. versionchanged:: 8.1.02445 .. versionchanged:: 8.12446 The default of a single option boolean flag is not shown if the2447 default value is ``False``.24482449 .. versionchanged:: 8.0.1
417 unmodified lines
2867 def resolve_envvar_value(self, ctx: Context) -> str | None:2868 rv = super().resolve_envvar_value(ctx)28692870 if rv is not None:2871 if self.is_flag and self.flag_value:2872 return str(self.flag_value)2873 return rv28742875 if (2876 self.allow_from_autoenv
163 unmodified lines
modified:tests/test_arguments.py+13 added
196 unmodified lines
197 else:198 assert result.return_value == expect199200201def test_envvar_flag_value(runner):202 @click.command()203 # is_flag is implicitly true204 @click.option("--upper", flag_value="upper", envvar="UPPER")205 def cmd(upper):206 click.echo(upper)207 return upper208209 # For whatever value of the `env` variable, if it exists, the flag should be `upper`210 result = runner.invoke(cmd, env={"UPPER": "whatever"})211 assert result.output.strip() == "upper"212213214def test_nargs_envvar_only_if_values_empty(runner):215 @click.command()216 @click.argument("arg", envvar="X", nargs=-1)217 def cli(arg):
219 unmodified lines
modified:CHANGES.rst+3 added
49 unmodified lines
50 ``Context.with_resource`` to be closed on exit as well. :pr:`2680`51- Add ``ProgressBar(hidden: bool)`` to allow hiding the progressbar. :issue:`2609`52- A ``UserWarning`` will be shown when multiple parameters attempt to use the53 same name. :issue:`2396``54- When using ``Option.envvar`` with ``Option.flag_value``, the ``flag_value``55 will always be used instead of the value of the environment variable.56 :issue:`2746` :pr:`2788`575859Version 8.1.860-------------
1116 unmodified lines