Skip to content
Myr

Cleanup changes to fix flag_value bug

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

authored by Stephen Rosen · committed by Stephen Rosen · parent 91de59c · Browse files · 3 files+40 added−34 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+40 added−34 removed

  • tests:+34 added−24 removed
  • other:+6 added−10 removed

3 files+40 added−34 removed

modified:src/click/core.py+4 added-8 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.26902687 .. versionchanged:: 8.22688 ``envvar`` used with ``flag_value`` will always use the ``flag_value``,2689 previously it would use the value of the environment variable.2690
86 unmodified lines
2777 # Implicitly a flag because secondary options names were given.2778 elif self.secondary_opts:2779 is_flag = True27802785 # 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/30842781 # The option is explicitly not a flag, but to determine whether or not it needs2782 # value, we need to check if `flag_value` or `default` was set. Either one is2783 # sufficient.2784 # Ref: https://github.com/pallets/click/issues/30842785 elif is_flag is False and not self._flag_needs_value:2786 self._flag_needs_value = flag_value is not UNSET or self.default is UNSET27872788 if is_flag:
630 unmodified lines
modified:tests/test_options.py+34 added-24 removed
2276 unmodified lines
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 provided2281@pytest.mark.parametrize(2282 ("flag_type", "args", "expect_output"),2283 [2284 (str, [], "Default\n"),2285 (str, ["--theflag"], "FlagValue\n"),2286 (str, ["--theflag", "value"], "value\n"),2287 (int, [], "0\n"),2288 (int, ["--theflag"], "1\n"),2289 (int, ["--theflag", "2"], "2\n"),2290 ],2291)2292def test_flag_value_on_option_with_zero_or_one_args(flag_type, args, expect_output):2293 """An option with flag_value and is_flag=False can be2294 omitted or used with 0 or 1 args.22952285 Reproduces https://github.com/pallets/click/issues/30842296 Regression test for https://github.com/pallets/click/issues/30842297 """2298 if flag_type is str:2299 flagopt = click.option(2300 "--theflag",2301 type=str,2302 is_flag=False,2303 flag_value="FlagValue",2304 default="Default",2305 )2306 elif flag_type is int:2307 flagopt = click.option(2308 "--theflag", type=int, is_flag=False, flag_value=1, default=02309 )2310 else:2311 raise NotImplementedError(flag_type)23122313 @click.command()2289 @click.option("--name", is_flag=False, flag_value="Flag", default="Default")2290 def hello(name):2291 click.echo(f"Hello, {name}!")2314 @flagopt2315 def cmd(theflag):2316 click.echo(theflag)23172318 runner = CliRunner()2294 result = runner.invoke(hello, ["--name"])2319 result = runner.invoke(cmd, args)2320 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"2321 assert result.output == expect_output
modified:CHANGES.rst+2 added-2 removed
1.. currentmodule:: click23Unreleased45- If ``flag_value`` is set or no default is provided the flag can be accepted6 without an argument.:issue:`3084`5- Fix handling of ``flag_value`` when ``is_flag=False`` to allow such options to be6 used without an explicit value. :issue:`3084`78Version 8.3.19--------------10
1305 unmodified lines