Skip to content

Commit 6fcfbc9

Browse files
authored
Merge pull request #453 from alimpfard/bool-flag-help
Show 'correct' explicit bool value syntax in help
2 parents be274af + 666b603 commit 6fcfbc9

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

flag.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,12 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string {
723723
}
724724

725725
varname, usage := UnquoteUsage(flag)
726-
if varname != "" {
726+
if flag.Value.Type() == "bool" {
727+
line += "[=true|false]"
728+
} else if varname != "" {
727729
line += " " + varname
728730
}
731+
729732
if flag.NoOptDefVal != "" {
730733
switch flag.Value.Type() {
731734
case "string":

flag_test.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,28 +1271,28 @@ func TestHiddenFlagUsage(t *testing.T) {
12711271
}
12721272
}
12731273

1274-
const defaultOutput = ` --A for bootstrapping, allow 'any' type
1275-
--Alongflagname disable bounds checking
1276-
-C, --CCC a boolean defaulting to true (default true)
1277-
--D path set relative path for local imports
1278-
-E, --EEE num[=1234] a num with NoOptDefVal (default 4321)
1279-
--F number a non-zero number (default 2.7)
1280-
--G float a float that defaults to zero
1281-
--IP ip IP address with no default
1282-
--IPMask ipMask Netmask address with no default
1283-
--IPNet ipNet IP network with no default
1284-
--Ints ints int slice with zero default
1285-
--N int a non-zero int (default 27)
1286-
--ND1 string[="bar"] a string with NoOptDefVal (default "foo")
1287-
--ND2 num[=4321] a num with NoOptDefVal (default 1234)
1288-
--StringArray stringArray string array with zero default
1289-
--StringSlice strings string slice with zero default
1290-
--Z int an int that defaults to zero
1291-
--custom custom custom Value implementation
1292-
--custom-with-val custom custom value which has been set from command line while help is shown
1293-
--customP custom a VarP with default (default 10)
1294-
--maxT timeout set timeout for dial
1295-
-v, --verbose count verbosity
1274+
const defaultOutput = ` --A[=true|false] for bootstrapping, allow 'any' type
1275+
--Alongflagname[=true|false] disable bounds checking
1276+
-C, --CCC[=true|false] a boolean defaulting to true (default true)
1277+
--D path set relative path for local imports
1278+
-E, --EEE num[=1234] a num with NoOptDefVal (default 4321)
1279+
--F number a non-zero number (default 2.7)
1280+
--G float a float that defaults to zero
1281+
--IP ip IP address with no default
1282+
--IPMask ipMask Netmask address with no default
1283+
--IPNet ipNet IP network with no default
1284+
--Ints ints int slice with zero default
1285+
--N int a non-zero int (default 27)
1286+
--ND1 string[="bar"] a string with NoOptDefVal (default "foo")
1287+
--ND2 num[=4321] a num with NoOptDefVal (default 1234)
1288+
--StringArray stringArray string array with zero default
1289+
--StringSlice strings string slice with zero default
1290+
--Z int an int that defaults to zero
1291+
--custom custom custom Value implementation
1292+
--custom-with-val custom custom value which has been set from command line while help is shown
1293+
--customP custom a VarP with default (default 10)
1294+
--maxT timeout set timeout for dial
1295+
-v, --verbose count verbosity
12961296
`
12971297

12981298
// Custom value that satisfies the Value interface.

printusage_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"testing"
77
)
88

9-
const expectedOutput = ` --long-form Some description
10-
--long-form2 Some description
11-
with multiline
12-
-s, --long-name Some description
13-
-t, --long-name2 Some description with
14-
multiline
9+
const expectedOutput = ` --long-form[=true|false] Some description
10+
--long-form2[=true|false] Some description
11+
with multiline
12+
-s, --long-name[=true|false] Some description
13+
-t, --long-name2[=true|false] Some description with
14+
multiline
1515
`
1616

1717
func setUpPFlagSet(buf io.Writer) *FlagSet {
@@ -47,11 +47,11 @@ func setUpPFlagSet2(buf io.Writer) *FlagSet {
4747
return f
4848
}
4949

50-
const expectedOutput2 = ` --long-form Some description
51-
--long-form2 Some description
50+
const expectedOutput2 = ` --long-form[=true|false] Some description
51+
--long-form2[=true|false] Some description
5252
with multiline
53-
-s, --long-name Some description
54-
-t, --long-name2 Some description with
53+
-s, --long-name[=true|false] Some description
54+
-t, --long-name2[=true|false] Some description with
5555
multiline
5656
-o, --other-very-long-arg string Some very long description having
5757
break the limit (default
@@ -69,6 +69,6 @@ func TestPrintUsage_2(t *testing.T) {
6969
f := setUpPFlagSet2(&buf)
7070
res := f.FlagUsagesWrapped(80)
7171
if res != expectedOutput2 {
72-
t.Errorf("Expected \n%q \nActual \n%q", expectedOutput2, res)
72+
t.Errorf("Expected \n%s \nActual \n%s", expectedOutput2, res)
7373
}
7474
}

0 commit comments

Comments
 (0)