Skip to content

Flag value is not working (new breaking change!) #3130

@uriii3

Description

@uriii3

We found a breaking change in the latest version of click! ❗

The example for optional value for an option, using flag_value is not correctly working anymore.

Example

import click


@click.command()
@click.option("--number", is_flag=False, flag_value=1, default=0)
def hello(number):
    click.echo(f"Hello, number is: {number}!")


hello()

With this python script, then you can try 3 different options:

Without option: python example.py outputs Hello, number is: 0!

With option (no value): python example.py --number outputs Error: Option '--number' requires an argument.

With option an value: python example.py --number 3 outputs Hello, number is: 3!

Expected behaviour

With option (no value): python example.py --number outputs Hello, number is: 1!

The expected behaviour is, as described in the click documentation (see Optional Value) that the option has a flag_value which is set to 1.

Or that is how I understand that it should also work.

PD: it works the same way with string and not numbers (so the exact example set in the documentation, I put numbers because it is also my use case.

Environment:

  • Python version: 3.12.3
  • Click version: 8.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions