Skip to content
Myr

Fix handling of options which set both is_flag=False and flag_value=... (#3152)

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

authored by Rowlando13 · committed by GitHub · parents 7f7bbe4, 955ca49 · Browse files · 3 files+55 added−3 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+55 added−3 removed

  • tests:+44 added−0 removed
  • other:+11 added−3 removed

3 files+55 added−3 removed

modified:src/click/core.py+6 added-3 removed
2775 unmodified lines
2776 is_flag = False2777 # Implicitly a flag because secondary options names were given.2778 elif self.secondary_opts:2779 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.27802781 # 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:2783 self._flag_needs_value = self.default is UNSET2786 self._flag_needs_value = flag_value is not UNSET or self.default is UNSET27872788 if is_flag:2789 # Set missing default for flags if not explicitly required or prompted.2790 if self.default is UNSET and not self.required and not self.prompt:
628 unmodified lines
modified:tests/test_options.py+44 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 == 0227922802281@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.22952296 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()2314 @flagopt2315 def cmd(theflag):2316 click.echo(theflag)23172318 runner = CliRunner()2319 result = runner.invoke(cmd, args)2320 assert result.exit_code == 02321 assert result.output == expect_output
modified:CHANGES.rst+5 added
1.. currentmodule:: click23Unreleased45- 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--------------1011Released 2025-11-15
1304 unmodified lines