Skip to content

Commit 341d453

Browse files
added required test in test_options, removed seperate test files. Maintained consistency throughout the docs.
1 parent d428b1d commit 341d453

File tree

5 files changed

+45
-155
lines changed

5 files changed

+45
-155
lines changed

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Version 8.3.x
44
--------------
55

66
Unreleased
7-
7+
- If ``flag_value`` is set or no default is provided the flag can be accepted without an
8+
argument. :issue:`3084` :pr:`#3104`
89
- Don't discard pager arguments by correctly using ``subprocess.Popen``. :issue:`3039`
910
:pr:`3055`
1011
- Replace ``Sentinel.UNSET`` default values by ``None`` as they're passed through

src/click/core.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,10 @@ class Option(Parameter):
26452645
:param hidden: hide this option from help outputs.
26462646
:param attrs: Other command arguments described in :class:`Parameter`.
26472647
2648+
.. versionchanged:: 8.3.dev
2649+
If ``flag_value`` is set or no default is provided, the flag can be
2650+
accepted without an argument.
2651+
26482652
.. versionchanged:: 8.2
26492653
``envvar`` used with ``flag_value`` will always use the ``flag_value``,
26502654
previously it would use the value of the environment variable.
@@ -2738,8 +2742,11 @@ def __init__(
27382742
# Implicitly a flag because secondary options names were given.
27392743
elif self.secondary_opts:
27402744
is_flag = True
2741-
# The option is explicitly not a flag. But we do not know yet if it needs a
2742-
# value or not. So we look at the default value to determine it.
2745+
2746+
# Handle options that are not flags but provide a flag_value.
2747+
# If flag_value is set or no default is provided the flag can be accepted
2748+
# without an argument.
2749+
# https://github.com/pallets/click/issues/3084
27432750
elif is_flag is False and not self._flag_needs_value:
27442751
self._flag_needs_value = flag_value is not UNSET or self.default is UNSET
27452752

test.sh

Lines changed: 0 additions & 56 deletions
This file was deleted.

tests/test_optional_value_bug.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

tests/test_options.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from click import Option
1616
from click import UNPROCESSED
1717
from click._utils import UNSET
18+
from click.testing import CliRunner
1819

1920

2021
def test_prefixes(runner):
@@ -2239,3 +2240,36 @@ def rcli(scm_ignore_files):
22392240
result = runner.invoke(rcli, ["--without-scm-ignore-files"])
22402241
assert result.stdout == "frozenset()"
22412242
assert result.exit_code == 0
2243+
2244+
2245+
def test_flag_value_optional_behavior():
2246+
"""Test that options with flag_value and is_flag=False use flag_value
2247+
when only flag is provided
2248+
2249+
Reproduces https://github.com/pallets/click/issues/3084
2250+
"""
2251+
2252+
@click.command()
2253+
@click.option("--name", is_flag=False, flag_value="Flag", default="Default")
2254+
def hello(name):
2255+
click.echo(f"Hello, {name}!")
2256+
2257+
runner = CliRunner()
2258+
result = runner.invoke(hello, ["--name"])
2259+
assert result.exit_code == 0
2260+
assert result.output == "Hello, Flag!\n"
2261+
2262+
2263+
def test_flag_value_with_type_conversion():
2264+
"""Test that flag_value is correctly type-converted when used as an option value."""
2265+
2266+
@click.command()
2267+
@click.option("--count", is_flag=False, flag_value="1", type=int, default=0)
2268+
def repeat(count):
2269+
for i in range(count):
2270+
click.echo(f"Line {i + 1}")
2271+
2272+
runner = CliRunner()
2273+
result = runner.invoke(repeat, ["--count"])
2274+
assert result.exit_code == 0
2275+
assert result.output == "Line 1\n"

0 commit comments

Comments
 (0)