goviper-goflag-gopflag-go

golang: How can I use pflag with other packages that use flag?


How does one use pflag while also using other packages that use flag?

Some of these packages define flags for the flag package (e.g. in their init functions) - and require flag.Parse() to be called.

Defining flags using the pflag package, require pflag.Parse() to be called.

One of the calls to flag.Parse() and pflag.Parse() will fail when arguments are mixed.

How does one use pflag with other packages that use flag?


Solution

  • I got a response from a maintainer for pflag.

    The solution is to "import" all flags defined for flag into pflag, and then make a call to pflag.Parse() alone.

    pflag provides a convenience function called AddGoFlagSet() to accomplish this.