Skip to content
Myr

Fix #3084: Correct flag optional value behavior and add comprehensive tests

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

- [pre-commit.ci lite] apply automatic fixes - added required test in test_options, removed seperate test files. Maintained consistency throughout the docs. - Fixed Missing indentaion in CHANGES.rst

authored by Animesh Tripathi · committed by Stephen Rosen · parent 7f7bbe4 · Browse files · 3 files+49 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: Stephen Rosen <1300022+sirosen@users.noreply.github.com>

Diff stats

3 files+49 added−3 removed

  • tests:+34 added−0 removed
  • other:+15 added−3 removed

3 files+49 added−3 removed

modified:src/click/core.py+10 added-3 removed
2682 unmodified lines
2683 :param help: the help string.2684 :param hidden: hide this option from help outputs.2685 :param attrs: Other command arguments described in :class:`Parameter`.26862687 .. versionchanged:: 8.3.dev2688 If ``flag_value`` is set or no default is provided, the flag can be2689 accepted without an argument.26902691 .. versionchanged:: 8.22692 ``envvar`` used with ``flag_value`` will always use the ``flag_value``,2693 previously it would use the value of the environment variable.2694
85 unmodified lines
2780 is_flag = False2781 # Implicitly a flag because secondary options names were given.2782 elif self.secondary_opts:2783 is_flag = True2780 # The option is explicitly not a flag. But we do not know yet if it needs a2781 # value or not. So we look at the default value to determine it.27842785 # Handle options that are not flags but provide a flag_value.2786 # If flag_value is set or no default is provided the flag can be accepted2787 # without an argument.2788 # https://github.com/pallets/click/issues/30842789 elif is_flag is False and not self._flag_needs_value:2783 self._flag_needs_value = self.default is UNSET2790 self._flag_needs_value = flag_value is not UNSET or self.default is UNSET27912792 if is_flag:2793 # Set missing default for flags if not explicitly required or prompted.2794 if self.default is UNSET and not self.required and not self.prompt:
628 unmodified lines
modified:tests/test_options.py+34 added
13 unmodified lines
14import click15from click import Option16from click import UNPROCESSED17from click._utils import UNSET18from click.testing import CliRunner192021def test_prefixes(runner):22 @click.command()
2252 unmodified lines
22752276 result = runner.invoke(rcli, ["--without-scm-ignore-files"])2277 assert result.stdout == "frozenset()"2278 assert result.exit_code == 0227922802281def test_flag_value_optional_behavior():2282 """Test that options with flag_value and is_flag=False use flag_value2283 when only flag is provided22842285 Reproduces https://github.com/pallets/click/issues/30842286 """22872288 @click.command()2289 @click.option("--name", is_flag=False, flag_value="Flag", default="Default")2290 def hello(name):2291 click.echo(f"Hello, {name}!")22922293 runner = CliRunner()2294 result = runner.invoke(hello, ["--name"])2295 assert result.exit_code == 02296 assert result.output == "Hello, Flag!\n"229722982299def test_flag_value_with_type_conversion():2300 """Test that flag_value is correctly type-converted when used as an option value."""23012302 @click.command()2303 @click.option("--count", is_flag=False, flag_value="1", type=int, default=0)2304 def repeat(count):2305 for i in range(count):2306 click.echo(f"Line {i + 1}")23072308 runner = CliRunner()2309 result = runner.invoke(repeat, ["--count"])2310 assert result.exit_code == 02311 assert result.output == "Line 1\n"
modified:CHANGES.rst+5 added
1.. currentmodule:: click23Unreleased45- If ``flag_value`` is set or no default is provided the flag can be accepted6 without an argument.:issue:`3084`78Version 8.3.19--------------1011Released 2025-11-15
1304 unmodified lines