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

Using pflag precludes the use of any other package written to use flag #63

Closed
ziffusion opened this issue Dec 3, 2015 · 6 comments
Closed

Comments

@ziffusion
Copy link

This is because if you mix packages that use pflag with those that use flag, it requires you to call both flag.Parse() anf pflag.Parse() - one or both of which will fail.

This seems to be a crippling requirement because if I choose to use pflag, I am precluded from using any other third party package that uses flag.

@eparis
Copy link
Collaborator

eparis commented Dec 3, 2015

You can use AddGoFlagSet to add all flag defined flags as pflag flags.

@ziffusion
Copy link
Author

There is another use case that is still problematic.

There are cases where the program is passed flag style arguments automatically, which then fail when parsed by pflag.

A glaring example of this is in fact "go test" - which passes flags such as -test.v -test.trace etc. After importing flags from flag into pflag, "go test" fails.

@anthonyfok anthonyfok reopened this Dec 5, 2015
@anthonyfok
Copy link
Collaborator

Reopening this to bring attention to fellow developers.

@epelc
Copy link

epelc commented Feb 10, 2016

I also ran into this while using pflags with flags from go test ie -v.

Here is a workaround. It works by seperating the test flags and all other flags. Then it hands them off to the correct flag parser. Note you need go 1.6rc2(might work with rc1) as there is a bug in 1.5 which makes m.Run() call flags.parse even if they have already been parsed.
https://gist.github.com/epelc/02011f31bd7e227de1c8

@doublerebel
Copy link

I extended @epelc's gist to also filter goflags with values. It now works with -test.* flags or any other single-dash flags with or without values:

https://gist.github.com/doublerebel/8b95c5c118e958e495d2#file-your_test-go-L13

Wouldn't hurt to have this as a feature such as KeepGoFlagSet, so that existing software moving to pflag package can maintain backward compatibility.

@spf13
Copy link
Owner

spf13 commented Apr 27, 2016

I believe this commit cb88ea7 adds built in support for passing through test flags.

It's convenient that the go flag package uses a single dash followed by a string of characters containing a dot as that's invalid for posix shorthand flags and is easily identifiable and ignorable.

Closing this for now as it feels the core issue is resolved. If someone wants to create another issue requesting a new KeepGoFlagSet feature or similar please do.

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

6 participants