alecthomas / kingpin

CONTRIBUTIONS ONLY: A Go (golang) command line and flag parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regression in parsing Duration flag

linki opened this issue · comments

#329 introduced a difference in parsing a DurationVar compared to the old version.

Unfortunately, this breaks the old behaviour when the duration was a negative value. The most common use case is probably to have a Default("-1s") to denote that the value isn't set at all. Similarly a --some-duration="" would previously fail but is now treated as 0s. Here are the differences that I can observe:

// parse negative duration
time.ParseDuration("-1s") => `-1s` // no error
str2duration.Str2Duration("-1s") => `(*errors.errorString)(0x140001c2b70)(invalid input duration string)`
// parse empty string
time.ParseDuration("") => `(*errors.errorString)(0x140001c2b80)(time: invalid duration "")`
str2duration.Str2Duration("") => 0s // no error

In practice this leads to the following (some-duration is a DurationVar).

// old version (v2.2.6)
$ go run main.go --some-duration="-1s"
... // runs fine

// new version (v2.3.1 (2e61e1e))
$ go run main.go --some-duration="-1s"
main: error: invalid input duration string, try --help
exit status 1
// old version (v2.2.6)
$ go run main.go --some-duration=""
main: error: time: invalid duration "", try --help
exit status 1

// new version (v2.3.1 (2e61e1e))
$ go run main.go --some-duration=""
... // runs fine

/cc @adowair

This also seems to be breaking our 32-bit testing. See: prometheus/node_exporter#2622

It looks like the v2 of this library may fix the issues.

@alecthomas, can you cut a new tag with #336?

👍

Tagged 2.3.2

2.3.2 fixed it for me: linki/chaoskube@c4b9b0d

Thank you @alecthomas and @SuperQ 🙏