-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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: FlagSet.Parse(...) unexpected behavior with non-flag args #26104
Comments
This is working as intended.
|
Indeed, working as intended. Flag packages from other providers might behave differently; I suggest you investigate them. |
OK; thanks for investigating. Nit: while this is WAI it is still not a good experience for developers since there is no way to check if all command line arguments have been parsed or ignored. While it is possible to find a different package; it can be hard to retrofit it to existing libraries that already use this one (they don't compose well; you essentially end up writing a tiny parser to make sure the right arguments ends up in the right parsers) |
However it is a good experience for programmers who write subcommands, for which this behavior is ideal. |
Agreed; Once I actually read the docs for FlagSet.Arg(...) and FlagSet.Args(...) (and tried to code a little) |
There is: by iterating |
I think there is a simpler way; but I've not verified it in detail. If you use flag.Parse() then you can use flag.CommandLine.Args() to get the "remaining" arguments. |
What version of Go are you using (
go version
)?go version go1.9.4 linux/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/dape/go"
GORACE=""
GOROOT="/usr/lib/go-1.9"
GOTOOLDIR="/usr/lib/go-1.9/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build733299665=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
What did you do?
Passing a non-flag (not prefixed with -) causes the FlagSet to stop parsing. I expected it to fail instead of silently behaving as if the input was valid.
https://play.golang.org/p/qLslIW5FbqI
fs := flag.NewFlagSet("test", flag.PanicOnError)
err := fs.Parse([]string{"no_such_thing", "-unknown"})
fmt.Println(err)
What did you expect to see?
I expected FlagSet.Parse([]string{"something", "-unknown"}) to fail.
What did you see instead?
FlagSet.Parse([]{"something", "-unknown"}) behave as if the input was valid.
This is implemented here but there are no tests to verify that this is the expected behavior.
This behavior make it possible to break almost any go program that runs on the terminal since there is no way for the developer to check the input without duplicating a lot of the code in the flag package.
The text was updated successfully, but these errors were encountered: