Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting NoOptDefVal forces using = syntax #134

Closed
loderunner opened this issue Aug 10, 2017 · 5 comments
Closed

Setting NoOptDefVal forces using = syntax #134

loderunner opened this issue Aug 10, 2017 · 5 comments

Comments

@loderunner
Copy link

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 ?

@loderunner
Copy link
Author

I created a Pull request #135

Here's the output I get (and expected).

$ ./test
verbosity: 1
$ ./test --verbose=0
verbosity: 0
$ ./test --verbose 3
verbosity: 3
$ ./test --verbose
verbosity: 2
$ ./test -v=0
verbosity: 0
$ ./test -v
verbosity: 2
$ ./test -v 1
verbosity: 1

@n10v
Copy link
Collaborator

n10v commented Aug 10, 2017

It's not possible to solve this issue unequivocally. Just consider if user will call three bool flags in the row like this: -abc. How it should be parsed? That's why your tests are failed.
And actually it will considerably change the behaviour, that we do not want.

@ghost
Copy link

ghost commented Mar 8, 2019

I do not fully understand the issue.

It's not possible to solve this issue unequivocally. Just consider if user will call three bool flags in the row like this: -abc

If 3 bool flags are passed, shouldn't pflag know that -a is a bool. if -abc is shorthand for a string flag, then that would override -abc as a multiple bool flags possibility.

Am I missing something?

@eparis
Copy link
Collaborator

eparis commented Mar 8, 2019

Imagine a is a string flag shorthand with NoOptDefVal="a_default"
Imagine there are bool shorthand flags b and c

What does -abc mean? Does it mean:
a = "bc" and b=false c=false
or does it mean:
a=a_default and b=true c=true

@ghost
Copy link

ghost commented Mar 8, 2019

Ah I understand what is meant by the above now.

What does -abc mean? Does it mean:
a = "bc" and b=false c=false
or does it mean:
a=a_default and b=true c=true

I don't understand why this is an issue. Yes it is a case against the idea, but if pflag follows a set of rules it seems obvious to me that a="bc" would be the correct answer. Since -a is being passed the string value "bc" it would overwrite it default and use "bc". If I wanted the seperate functionality the correct solution would be to use:

-a -bc

Are there any other examples where this would actually be a problem?

EDIT:

Actually it appears that this is handled in NodeJS as parsed as @eparis second option:

a=a_default and b=true c=true

I digress, Go isn't Node, but it seems every library handles it differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants