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
Provenance
- Commit
ebcd548- Tree
00e4ad8- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
7f7bbe4- Parent 2
955ca49
3 files changed+55 added−3 removed
modified:src/click/core.py+6 added-3 removed
2775 unmodified lines2776 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.2780+2781+ # 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 UNSET2787 2788 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 linesmodified:tests/test_options.py+44 added
13 unmodified lines14 import click15 from click import Option16 from click import UNPROCESSED17 from click._utils import UNSET18+from click.testing import CliRunner19 20 21 def test_prefixes(runner):22 @click.command()2252 unmodified lines2275 2276 result = runner.invoke(rcli, ["--without-scm-ignore-files"])2277 assert result.stdout == "frozenset()"2278 assert result.exit_code == 02279+2280+2281+@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+)2292+def 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.2295+2296+ 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)2312+2313+ @click.command()2314+ @flagopt2315+ def cmd(theflag):2316+ click.echo(theflag)2317+2318+ runner = CliRunner()2319+ result = runner.invoke(cmd, args)2320+ assert result.exit_code == 02321+ assert result.output == expect_outputmodified:CHANGES.rst+5 added
1 .. currentmodule:: click2 3+Unreleased4+5+- Fix handling of ``flag_value`` when ``is_flag=False`` to allow such options to be6+ used without an explicit value. :issue:`3084`7+8 Version 8.3.19 --------------10 11 Released 2025-11-151304 unmodified lines