[rejected AI] count=True ignores non-zero default when counting flags
When an option uses count=True with a non-zero default, each occurrence of the flag counts from 0 instead of from the configured default. Passing fewer flags than the default can therefore produce a value lower than the default (or leave default=1 unchanged when the flag is passed once).
This differs from argparse.ArgumentParser with action="count", which increments relative to the default (for example default=3 plus one flag occurrence yields 4).
import argparse
import click
from click.testing import CliRunner
# argparse (expected-aligned behavior)
p = argparse.ArgumentParser()
p.add_argument("-v", action="count", default=3)
print("argparse no flags", p.parse_args([]).v) # 3
print("argparse one flag", p.parse_args(["-v"]).v) # 4
print("argparse two flags", p.parse_args(["-vv"]).v) # 5
# click
@click.command()
@click.option("-v", count=True, default=3)
def cli(v):
click.echo(v)
runner = CliRunner()
print("click no flags", runner.invoke(cli, []).output.strip()) # 3
print("click one flag", runner.invoke(cli, ["-v"]).output.strip()) # 1 ← bug
print("click two flags", runner.invoke(cli, ["-vv"]).output.strip()) # 2 ← bug
Root cause appears to be in click/parser.py, where the count action always starts from 0:
state.opts[self.dest] = state.opts.get(self.dest, 0) + 1
Once a command-line count is present, consume_value() takes that value and never applies the option default as a baseline.
Expected (argparse-compatible): command-line counts are additive on top of the option default.
| Invocation | Expected | Actual (Click) |
|---|---|---|
(none) |
3 |
3 |
one -v |
4 |
1 |
-vv |
5 |
2 |
-vvv |
6 |
3 |
With default=1, a single flag occurrence should yield 2, but currently yields 1 (same as omitting the flag).
Environment:
- Python version: 3.12.10
- Click version: 8.5.1.dev0 (main)
https://palletsprojects.com/contributing/llm-ai
Current behavior makes sense too, this is complete AI nonsense.