Skip to content

Commit b5aa710

Browse files
authored
Merge pull request urfave#2321 from wucm667/fix/bool-inverse-alias-help
fix: show BoolWithInverseFlag aliases in help text
2 parents f2cd020 + 2f662c8 commit b5aa710

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

flag_bool_with_inverse.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ func (bif *BoolWithInverseFlag) IsVisible() bool {
167167
//
168168
// Example for BoolFlag{Name: "env"}
169169
// --[no-]env (default: false)
170+
//
171+
// Example for BoolFlag{Name: "env", Aliases: []string{"e"}}
172+
// --[no-]env, -e (default: false)
170173
func (bif *BoolWithInverseFlag) String() string {
171174
out := FlagStringer(bif)
172175

@@ -187,7 +190,21 @@ func (bif *BoolWithInverseFlag) String() string {
187190
i = 0
188191
}
189192

190-
return fmt.Sprintf("%s[%s]%s%s", prefix, bif.inversePrefix(), bif.Name, out[i:])
193+
var aliasParts []string
194+
for _, alias := range bif.Aliases {
195+
aPrefix := "--"
196+
if len(alias) == 1 {
197+
aPrefix = "-"
198+
}
199+
aliasParts = append(aliasParts, aPrefix+alias)
200+
}
201+
202+
names := fmt.Sprintf("%s[%s]%s", prefix, bif.inversePrefix(), bif.Name)
203+
if len(aliasParts) > 0 {
204+
names = names + ", " + strings.Join(aliasParts, ", ")
205+
}
206+
207+
return fmt.Sprintf("%s%s", names, out[i:])
191208
}
192209

193210
// IsBoolFlag returns whether the flag doesnt need to accept args

flag_bool_with_inverse_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func TestBoolWithInverseString(t *testing.T) {
345345
required bool
346346
usage string
347347
inversePrefix string
348+
aliases []string
348349
expected string
349350
}{
350351
{
@@ -397,6 +398,27 @@ func TestBoolWithInverseString(t *testing.T) {
397398
required: true,
398399
expected: "--[no-]env\t",
399400
},
401+
{
402+
testName: "short alias",
403+
flagName: "color",
404+
required: false,
405+
aliases: []string{"c"},
406+
expected: "--[no-]color, -c\t(default: false)",
407+
},
408+
{
409+
testName: "long alias",
410+
flagName: "color",
411+
required: false,
412+
aliases: []string{"colour"},
413+
expected: "--[no-]color, --colour\t(default: false)",
414+
},
415+
{
416+
testName: "multiple aliases",
417+
flagName: "color",
418+
required: false,
419+
aliases: []string{"c", "colour"},
420+
expected: "--[no-]color, -c, --colour\t(default: false)",
421+
},
400422
}
401423

402424
for _, tc := range tcs {
@@ -406,6 +428,7 @@ func TestBoolWithInverseString(t *testing.T) {
406428
Usage: tc.usage,
407429
Required: tc.required,
408430
InversePrefix: tc.inversePrefix,
431+
Aliases: tc.aliases,
409432
}
410433

411434
require.Equal(t, tc.expected, flag.String())

godoc-current.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ func (bif *BoolWithInverseFlag) String() string
414414

415415
Example for BoolFlag{Name: "env"} --[no-]env (default: false)
416416

417+
Example for BoolFlag{Name: "env", Aliases: []string{"e"}} --[no-]env,
418+
-e (default: false)
419+
417420
func (bif *BoolWithInverseFlag) TakesValue() bool
418421

419422
func (bif *BoolWithInverseFlag) TypeName() string

testdata/godoc-v3.x.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ func (bif *BoolWithInverseFlag) String() string
414414

415415
Example for BoolFlag{Name: "env"} --[no-]env (default: false)
416416

417+
Example for BoolFlag{Name: "env", Aliases: []string{"e"}} --[no-]env,
418+
-e (default: false)
419+
417420
func (bif *BoolWithInverseFlag) TakesValue() bool
418421

419422
func (bif *BoolWithInverseFlag) TypeName() string

0 commit comments

Comments
 (0)