Skip to content

Commit 373429e

Browse files
committed
1 parent b7b6368 commit 373429e

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

docs/options.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,15 @@ If you want to define an alias for the second option only, then you will need to
314314

315315
## Flag Value
316316

317-
To have an flag pass a value to the underlying function set `flag_value`. This automatically sets `is_flag=True`. To set a default flag, set `default=True`. Setting flag values can be used to create patterns like this:
317+
To have an flag pass a value to the underlying function set `flag_value`. This automatically sets `is_flag=True`. To influence the value-less behavior, force its value with `default='upper'`. Setting flag values can be used to create patterns like this:
318318

319319
```{eval-rst}
320320
.. click:example::
321321
322322
import sys
323323
324324
@click.command()
325-
@click.option('--upper', 'transformation', flag_value='upper', default=True)
325+
@click.option('--upper', 'transformation', flag_value='upper', default='upper')
326326
@click.option('--lower', 'transformation', flag_value='lower')
327327
def info(transformation):
328328
click.echo(getattr(sys.platform, transformation)())

src/click/core.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,9 +2250,12 @@ def consume_value(
22502250
22512251
If no value is found, the value is returned as :attr:`UNSET`.
22522252
"""
2253+
# If the value is set, it means the parser produced a value from user input.
22532254
value = opts.get(self.name, UNSET) # type: ignore
22542255
source = (
2255-
ParameterSource.DEFAULT if value is UNSET else ParameterSource.COMMANDLINE
2256+
ParameterSource.COMMANDLINE
2257+
if value is not UNSET
2258+
else ParameterSource.DEFAULT
22562259
)
22572260

22582261
if value is UNSET:
@@ -3103,7 +3106,7 @@ def consume_value(
31033106
elif (
31043107
self.multiple
31053108
and value is not UNSET
3106-
and source is ParameterSource.COMMANDLINE
3109+
and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)
31073110
and any(v is _flag_needs_value for v in value)
31083111
):
31093112
value = [self.flag_value if v is _flag_needs_value else v for v in value]

tests/test_basic.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,27 @@ def cli(flag):
276276
assert result.output == expect
277277

278278

279+
@pytest.mark.parametrize(
280+
("default", "args", "expect"),
281+
(
282+
# See: https://github.com/pallets/click/issues/3024#issuecomment-3146199461
283+
(True, ["--upper"], "upper"),
284+
(True, ["--lower"], "lower"),
285+
(True, [], "True"),
286+
("upper", [], "upper"),
287+
),
288+
)
289+
def test_flag_value(runner, default, args, expect):
290+
@click.command()
291+
@click.option("--upper", "transformation", flag_value="upper", default=default)
292+
@click.option("--lower", "transformation", flag_value="lower")
293+
def cli(transformation):
294+
click.echo(repr(transformation), nl=False)
295+
296+
result = runner.invoke(cli, args)
297+
assert result.output == repr(expect)
298+
299+
279300
def test_file_option(runner):
280301
@click.command()
281302
@click.option("--file", type=click.File("w"))

0 commit comments

Comments
 (0)