Navigation Menu

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

flag: should not output errors directly to stderr #10169

Closed
jvehent opened this issue Mar 15, 2015 · 1 comment
Closed

flag: should not output errors directly to stderr #10169

jvehent opened this issue Mar 15, 2015 · 1 comment

Comments

@jvehent
Copy link

jvehent commented Mar 15, 2015

When using a flagset, errors are printed to stderr in addition to being returned by flag.Parse(). It is redundant, since the caller can print the error if desired. It also creates a small issue in a situation where multiple flagsets are used to parse a single command line.

I have a command line interface that provides a wrapper to multiple modules. The command line has its own flagset [1], and forward the flags it cannot parse to one of many packages where specific flagsets are defined [2, 3]. The issue is that the first call to Parse() detects the flags it doesn't know and prints an error to stderr directly. I would like to avoid printing this error. I ended up setting the output to os.DevNull around the call to Parse() to prevent displaying it to the user, but it feels like a hack [4].

The culpric is here: https://github.com/golang/go/blob/master/src/flag/flag.go#L793
I suggest removing that line and only returning the error to the caller through the Error value.

I'm not aware of a different way to split a flagset across packages, but if there's one, I'm very interested :)

[1] https://github.com/mozilla/mig/blob/master/src/mig/client/cmd/main.go#L71-L77
[2] https://github.com/mozilla/mig/blob/master/src/mig/modules/file/paramscreator.go#L285-L305
[3] https://github.com/mozilla/mig/blob/master/src/mig/modules/netstat/paramscreator.go#L171-L178
[4] https://github.com/mozilla/mig/blob/master/src/mig/client/cmd/main.go#L124

@mikioh mikioh changed the title Flag package should not output errors directly to stderr flag: should not output errors directly to stderr Mar 15, 2015
@minux
Copy link
Member

minux commented Mar 15, 2015

Just use SetOutput on the flag set to set the output io.Writer to
ioutil.Discard.

Of course we can't remove that line, otherwise it will also change the
behavior of toplevel flag.CommandLine flagset.

@minux minux closed this as completed Mar 15, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants