pflag
pflag copied to clipboard
StringToString usage output is unstable
Since Go maps have randomized iteration order, FlagUsages()
returns inconsistent output:
// You can edit this code!
// Click here and start typing.
package main
import (
"fmt"
"github.com/spf13/pflag"
)
func main() {
counter := map[string]int{}
for i := 0; i < 100; i++ {
fs := pflag.NewFlagSet("t", pflag.ExitOnError)
fs.StringToString("foo", map[string]string{"a": "b", "c": "d"}, "usage")
output := fs.FlagUsages()
counter[output]++
}
fmt.Print(counter)
}
map[ --foo stringToString usage (default [a=b,c=d])
:88 --foo stringToString usage (default [c=d,a=b])
:12]
In projects with a workflow that writes the command usage to a file (e.g. README), this causes unnecessary diff all the time.
It's a general issue concerning reproducibility. If you want to create an OS package including docs, say, man pages, it won't be reproducible since the man pages will differ on each build.