Skip to content

StringToString usage output is unstable #362

@SOF3

Description

@SOF3

Since Go maps have randomized iteration order, FlagUsages() returns inconsistent output:

playground

// 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions