Pflags accepts the following syntaxes for an integer argument:
--verbose=0
--verbose 0
-v 0
-v=0
Using this program:
v := pflag.IntP("verbose", "v", 1, "make output verbose")
pflag.Parse()
fmt.Printf("verbosity: %d\n", *v)
I get the following results:
$ ./test
verbosity: 1
$ ./test --verbose=0
verbosity: 0
$ ./test --verbose 2
verbosity: 2
$ ./test -v 0
verbosity: 0
$ ./test -v=2
verbosity: 2
If I set NoOptDefVal, I get inconsistent behavior.
v := pflag.IntP("verbose", "v", 1, "make output verbose")
verboseFlag := pflag.Lookup("verbose")
// In case of --verbose or -v with no arg, set to 2
verboseFlag.NoOptDefVal = "2"
pflag.Parse()
fmt.Printf("verbosity: %d\n", *v)
Here are the results:
$ ./test
verbosity: 1
$ ./test --verbose=0
verbosity: 0
$ ./test --verbose 2
verbosity: 2
$ ./test --verbose 3
verbosity: 2
$ ./test -v=0
verbosity: 0
$ ./test -v=1
verbosity: 1
$ ./test -v=2
verbosity: 2
$ ./test -v 1
verbosity: 2
I expected -v 3 and -v=3 to work the same way.
Is this a bug, or am I missing the point of NoOptDefVal ?
Pflags accepts the following syntaxes for an integer argument:
Using this program:
I get the following results:
If I set
NoOptDefVal, I get inconsistent behavior.Here are the results:
I expected
-v 3and-v=3to work the same way.Is this a bug, or am I missing the point of
NoOptDefVal?