Skip to content

Commit c6f8d76

Browse files
Fix defaultIsZeroValue check for generic Value type
1 parent 5ca8134 commit c6f8d76

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

flag.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,32 @@ unaffected.
2727
Define flags using flag.String(), Bool(), Int(), etc.
2828
2929
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
30+
3031
var ip = flag.Int("flagname", 1234, "help message for flagname")
32+
3133
If you like, you can bind the flag to a variable using the Var() functions.
34+
3235
var flagvar int
3336
func init() {
3437
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
3538
}
39+
3640
Or you can create custom flags that satisfy the Value interface (with
3741
pointer receivers) and couple them to flag parsing by
42+
3843
flag.Var(&flagVal, "name", "help message for flagname")
44+
3945
For such flags, the default value is just the initial value of the variable.
4046
4147
After all flags are defined, call
48+
4249
flag.Parse()
50+
4351
to parse the command line into the defined flags.
4452
4553
Flags may then be used directly. If you're using the flags themselves,
4654
they are all pointers; if you bind to variables, they're values.
55+
4756
fmt.Println("ip has value ", *ip)
4857
fmt.Println("flagvar has value ", flagvar)
4958
@@ -54,22 +63,26 @@ The arguments are indexed from 0 through flag.NArg()-1.
5463
The pflag package also defines some new functions that are not in flag,
5564
that give one-letter shorthands for flags. You can use these by appending
5665
'P' to the name of any function that defines a flag.
66+
5767
var ip = flag.IntP("flagname", "f", 1234, "help message")
5868
var flagvar bool
5969
func init() {
6070
flag.BoolVarP(&flagvar, "boolname", "b", true, "help message")
6171
}
6272
flag.VarP(&flagval, "varname", "v", "help message")
73+
6374
Shorthand letters can be used with single dashes on the command line.
6475
Boolean shorthand flags can be combined with other shorthand flags.
6576
6677
Command line flag syntax:
78+
6779
--flag // boolean flags only
6880
--flag=x
6981
7082
Unlike the flag package, a single dash before an option means something
7183
different than a double dash. Single dashes signify a series of shorthand
7284
letters for flags. All but the last shorthand letter must be boolean flags.
85+
7386
// boolean flags
7487
-f
7588
-abc
@@ -551,7 +564,7 @@ func (f *Flag) defaultIsZeroValue() bool {
551564
case *intSliceValue, *stringSliceValue, *stringArrayValue:
552565
return f.DefValue == "[]"
553566
default:
554-
switch f.Value.String() {
567+
switch f.DefValue {
555568
case "false":
556569
return true
557570
case "<nil>":
@@ -934,9 +947,9 @@ func (f *FlagSet) usage() {
934947
}
935948
}
936949

937-
//--unknown (args will be empty)
938-
//--unknown --next-flag ... (args will be --next-flag ...)
939-
//--unknown arg ... (args will be arg ...)
950+
// --unknown (args will be empty)
951+
// --unknown --next-flag ... (args will be --next-flag ...)
952+
// --unknown arg ... (args will be arg ...)
940953
func stripUnknownFlagValue(args []string) []string {
941954
if len(args) == 0 {
942955
//--unknown

0 commit comments

Comments
 (0)