fix: use prefixFor for BoolWithInverseFlag names and aliases

BoolWithInverseFlag.String picked - or -- with len(name) == 1, which counts bytes, while the rest of the library uses prefixFor, which counts runes. a one-character non-ascii name or alias was shown as --ü in its help and as -ü for any other flag.
This commit is contained in:
dualfroz committed 2026-10-07 19:37:46 +02:00
1 parent ba1266f7c4
commit 544f3ffc57
2 files changed
+15 -10

No files matched your search

+2 -10
View File
@@ -203,12 +203,8 @@ func (bif *BoolWithInverseFlag) String() string {
i := strings.Index(out, "\t") i := strings.Index(out, "\t")
prefix := "--"
// single character flags are prefixed with `-` instead of `--` // single character flags are prefixed with `-` instead of `--`
if len(bif.Name) == 1 { prefix := prefixFor(bif.Name)
prefix = "-"
}
// Guard against a FlagStringer that returns a string without a tab (e.g. // Guard against a FlagStringer that returns a string without a tab (e.g.
// a custom stringer or the default stringer when the flag does not // a custom stringer or the default stringer when the flag does not
@@ -220,11 +216,7 @@ func (bif *BoolWithInverseFlag) String() string {
var aliasParts []string var aliasParts []string
for _, alias := range bif.Aliases { for _, alias := range bif.Aliases {
aPrefix := "--" aliasParts = append(aliasParts, prefixFor(alias)+alias)
if len(alias) == 1 {
aPrefix = "-"
}
aliasParts = append(aliasParts, aPrefix+alias)
} }
names := fmt.Sprintf("%s[%s]%s", prefix, bif.inversePrefix(), bif.Name) names := fmt.Sprintf("%s[%s]%s", prefix, bif.inversePrefix(), bif.Name)
+13
View File
@@ -367,6 +367,12 @@ func TestBoolWithInverseString(t *testing.T) {
required: true, required: true,
expected: "-[no-]e\t", expected: "-[no-]e\t",
}, },
{
testName: "single-char multibyte flag name",
flagName: "ü",
required: true,
expected: "-[no-]ü\t",
},
{ {
testName: "multi-char flag name", testName: "multi-char flag name",
flagName: "env", flagName: "env",
@@ -412,6 +418,13 @@ func TestBoolWithInverseString(t *testing.T) {
aliases: []string{"c"}, aliases: []string{"c"},
expected: "--[no-]color, -c\t(default: false)", expected: "--[no-]color, -c\t(default: false)",
}, },
{
testName: "short multibyte alias",
flagName: "color",
required: false,
aliases: []string{"ü"},
expected: "--[no-]color, -ü\t(default: false)",
},
{ {
testName: "long alias", testName: "long alias",
flagName: "color", flagName: "color",