-
Notifications
You must be signed in to change notification settings - Fork 18k
flag: handles unknown arguments in an unexpected less-helpful way #15352
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
Comments
The flag package can't tell whether an unknown flag takes an argument or not, so to simply continue parsing flags seems risky. |
I want to make sure I understand your concern correctly: Let's say the command line looks like this: The user intends for the value of unknown to be "--known" and "foo" to be a positional argument. I agree this could be problematic. But it seems unlikely to come up regularly. Most likely, the program will break in a way apparent to the user and they can modify their command line to read What about the other half of my proposal? It seems to me at least that flagSet.Arg() should return the first unknown argument instead of only subsequent arguments. |
flag package recognizes only the following syntax for flags:
If you change |
I just encountered the same issue where a user supplied a value after a boolean |
+1 -- I was recently writing a small go program that invoked another program with the args that were passed (amongst doing other things). I wanted to parse 2 of the args, but leave the others unchanged. I couldn't use the flag package. ContinueOnError really means AbortOnError. There is no "real" continue on error |
@josh-tepper I have a similar situation. What did you end up doing? |
@wburningham -- Unfortunately, I ended up doing my own argument parsing |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…rror handling Fixes golang#15352 Go's flag.FlagSet struct has ErrorHandling support with three values: ContinueOnError, ExitOnError, and PanicOnError. ExitOnError and PanicOnError are straightforward, but ContinueOnError stops processing arguments if an error is encountered. This commit changes the behavior of ContinueOnError to "continue" parsing arguments. Errors are not returned and are silently dropped.
Change https://go.dev/cl/472935 mentions this issue: |
If the flag library encounters an unknown parameter, it discards that parameter and dumps everything else in the positional arguments bucket.
Instead, the flag library should (optionally, probably when ContinueOnError is specified) put all unknown parameters in the positional arguments bucket (returned by Args) and keep attempting to parse the following arguments. (unless encountering something that clearly indicated the beginning of positional arguments such as the naked double-dash "--")
go version
)?go version go1.3.3 linux/amd64
go env
)?GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/azani/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
https://play.golang.org/p/44mXvwTfLD
list == "first", "second", "third"
flagSet.Args() == "--something", "blah"
list == "first", "second"
flagSet.Args() == "-I", "third", "blah"
also, Parse printed an error.
The text was updated successfully, but these errors were encountered: