Skip to content

Setting NoOptDefVal forces using = syntax #134

@loderunner

Description

@loderunner

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions