Skip to content

Fix counter panic on a uint or float field with an explicit --flag=N - #626

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
chuenchen309:fix/counter-uint-float-explicit
Jul 19, 2026
Merged

Fix counter panic on a uint or float field with an explicit --flag=N#626
alecthomas merged 1 commit into
alecthomas:masterfrom
chuenchen309:fix/counter-uint-float-explicit

Conversation

@chuenchen309

Copy link
Copy Markdown
Contributor

Problem

A counter flag with an explicit value (--flag=N, the documented long form) panics when the field is a uint or float:

var cli struct {
    Count uint `type:"counter"`
}
kong.Parse(&cli, "--count=3")
// panic: reflect: call of reflect.Value.SetInt on uint Value

The README says counter works on a "numeric field" and "Can accept -s, --long or --long=N", and the plain increment form (-ccc) already works on uint/float fields — only the explicit-value form crashes.

Cause

In counterMapper, the increment path switches on Int/Uint/Float kinds, but the explicit-value path assigned unconditionally:

case string:
    n, err := strconv.ParseInt(v, 10, 64)
    ...
    target.SetInt(n)            // panics on a uint/float target
case int, ...:
    target.Set(reflect.ValueOf(v))   // panics on a kind mismatch (e.g. int value, uint target)

SetInt is only valid on int kinds, so a uint/float64 counter panics. The resolver-supplied numeric branch had the same kind-mismatch issue.

Fix

Parse the explicit value to an int64, then assign by the target's kind (SetInt/SetUint/SetFloat), mirroring the increment path just below it. Extended TestCounter with the previously-missing --uint=N / --float=N cases (it only covered --int=5 explicitly).

go test ./... passes; golangci-lint/gofmt clean.


Disclosure: authored by an AI coding agent (Claude Code) running on this account — it found the bug, wrote the repro/test, the fix and this description. I review every change and am accountable for it; the verification above is real and re-runnable from the diff. Happy to adjust anything.

A `counter` flag documents `--long=N` on a numeric field, and the
increment path (`-vvv`) already handles int/uint/float kinds. But the
explicit-value path assigned unconditionally with `SetInt`, so
`--count=N` on a `uint` or `float64` counter panicked with
`reflect: call of reflect.Value.SetInt on uint Value` instead of setting
the value. The resolver-supplied `int` branch (`Set(reflect.ValueOf(v))`)
had the same kind-mismatch problem.

Parse the explicit value to an int64 and assign by the target's kind,
mirroring the increment path below.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Andrew Chen <48723787+chuenchen309@users.noreply.github.com>
@alecthomas
alecthomas merged commit a5c9626 into alecthomas:master Jul 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants