From 544f3ffc576c65f4c5e023d0ae5984b1f6a4632d Mon Sep 17 00:00:00 2001 From: DualFroz Date: Wed, 7 Oct 2026 19:37:46 +0200 Subject: [PATCH] fix: use prefixFor for BoolWithInverseFlag names and aliases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- flag_bool_with_inverse.go | 12 ++---------- flag_bool_with_inverse_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/flag_bool_with_inverse.go b/flag_bool_with_inverse.go index d885bbd..adc6c47 100644 --- a/flag_bool_with_inverse.go +++ b/flag_bool_with_inverse.go @@ -203,12 +203,8 @@ func (bif *BoolWithInverseFlag) String() string { i := strings.Index(out, "\t") - prefix := "--" - // single character flags are prefixed with `-` instead of `--` - if len(bif.Name) == 1 { - prefix = "-" - } + prefix := prefixFor(bif.Name) // 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 @@ -220,11 +216,7 @@ func (bif *BoolWithInverseFlag) String() string { var aliasParts []string for _, alias := range bif.Aliases { - aPrefix := "--" - if len(alias) == 1 { - aPrefix = "-" - } - aliasParts = append(aliasParts, aPrefix+alias) + aliasParts = append(aliasParts, prefixFor(alias)+alias) } names := fmt.Sprintf("%s[%s]%s", prefix, bif.inversePrefix(), bif.Name) diff --git a/flag_bool_with_inverse_test.go b/flag_bool_with_inverse_test.go index 895c33d..423157e 100644 --- a/flag_bool_with_inverse_test.go +++ b/flag_bool_with_inverse_test.go @@ -367,6 +367,12 @@ func TestBoolWithInverseString(t *testing.T) { required: true, expected: "-[no-]e\t", }, + { + testName: "single-char multibyte flag name", + flagName: "ü", + required: true, + expected: "-[no-]ü\t", + }, { testName: "multi-char flag name", flagName: "env", @@ -412,6 +418,13 @@ func TestBoolWithInverseString(t *testing.T) { aliases: []string{"c"}, 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", flagName: "color",