glog and testing flag "pollution"

245 views
Skip to first unread message

Tamás Gulácsi

unread,
Nov 4, 2014, 4:51:53 PM11/4/14
to golan...@googlegroups.com
Hi,

I've wanted to write some adapter methods for another logging package (gopkg.in/inconshreveable/log15.v2): github.com/tgulacsi/go/loghlp
If I import the "testing" package in a non-testing package (this library), all testing flags appear in the program using this loghlp package.
This is only annoying.

But when I import glog (github.com/golang/glog), it register "v", and I like to use the same in my apps, so as I import anything which importes loghlp,
my main app panics on start (duplicate flag registration).

I have a very hackish hack against this: create a new flag.CommandLine FlagSet after importing glog and testing.
But this seems erroneous, as if any other package does register flags rightfully, this may mask that...

Any better idea?

Thanks,
Tamás Gulácsi

Ian Davis

unread,
Nov 5, 2014, 4:30:10 AM11/5/14
to golan...@googlegroups.com
You can just create a new FlagSet, add all your flags to it, then call its Parse method. Don't call the package level Parse function. This way you control exactly which flags are parsed by your program.
 

Ian
 

Tamás Gulácsi

unread,
Nov 5, 2014, 5:19:01 AM11/5/14
to golan...@googlegroups.com

Yes, I do have to create a new FlagSet in the application now.
Or I have to split the loghlp package, and import them only when the application actually uses the functionality.

Tamás

chris...@gmail.com

unread,
Dec 18, 2015, 10:16:43 PM12/18/15
to golang-nuts
On Tuesday, November 4, 2014 at 1:51:53 PM UTC-8, Tamás Gulácsi wrote:

I've wanted to write some adapter methods for another logging package (gopkg.in/inconshreveable/log15.v2): github.com/tgulacsi/go/loghlp
If I import the "testing" package in a non-testing package (this library), all testing flags appear in the program using this loghlp package.
This is only annoying.

Reviving an old topic.  The glog library seems to be included in many different apps and libraries these days.  For a less-grizzled Go programmer like myself,
this creates a time-consuming and annoying goose chase to track down the offender.  Yes, I could (and did) create a FlagSet but this seems like an
unfair burden on people who may be unaware that a library several imports down the chain might be causing this.   

Thanks,

Chris
Reply all
Reply to author
Forward
0 new messages