Skip to content

flag: antipattern - failf error handler prints usage unconditionally #73307

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

Closed
abitrolly opened this issue Apr 10, 2025 · 4 comments
Closed

flag: antipattern - failf error handler prints usage unconditionally #73307

abitrolly opened this issue Apr 10, 2025 · 4 comments
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@abitrolly
Copy link

abitrolly commented Apr 10, 2025

Go version

1.24

Output of go env in your module/workspace:

No.

What did you do?

Minimal flags example (https://go.dev/play/p/r6dDUoerjTI):

package main

import "flag"

func main() {
        flag.Parse()
}

Running this example with invalid flag always prints usage that is impossible to turn off regardless of error handling strategy https://pkg.go.dev/flag#ErrorHandling

What did you see happen?

$ go run minimal.go -ggg
flag provided but not defined: -ggg
Usage of /home/anatoli/.cache/go-build/8d/8dae0dadd55b6ae9e6d4bca4d6c85e20970b537337a98a5006eaeef926f92b5f-d/minimal:
exit status 2

The problem is in failf function https://cs.opensource.google/go/go/+/refs/tags/go1.24.2:src/flag/flag.go;l=1060 that outputs the usage two levels below Parse where it should be handled.

Parse --> parseOne --> failf --> usage

The similar report #63977

What did you expect to see?

At least some way to suppress usage message in errors.

Right now the only way to do this is to completely turn off all flag output and handle it all manually.

@gabyhelp
Copy link

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Apr 10, 2025
@seankhliao
Copy link
Member

This is the documented and intended behaviour of FlagSet.Usage:

https://pkg.go.dev/flag#FlagSet

Usage is the function called when an error occurs while parsing flags.
...
What happens after Usage is called depends on the ErrorHandling setting; for the command line, this defaults to ExitOnError, which exits the program after calling Usage.

ErrorHandling only affects what happens after Usage.
Closing as working as intended.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 10, 2025
@abitrolly
Copy link
Author

@seankhliao but do you agree on the very strong code smell here?

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/664755 mentions this issue: flag: move usage printing from error factory to Parse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

4 participants