Skip to content

cmd/go, flag: make undefined flag msg less ambiguous. Fixes #36364 #37944

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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cmd/go/internal/cmdflag/flag.go
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ type FlagNotDefinedError struct {
}

func (e FlagNotDefinedError) Error() string {
return fmt.Sprintf("flag provided but not defined: -%s", e.Name)
return fmt.Sprintf("The flag '-%s' is an unknown flag.", e.Name)
}

// A NonFlagError indicates an argument that is not a syntactically-valid flag.
2 changes: 1 addition & 1 deletion src/cmd/go/testdata/script/mod_getmode_vendor.txt
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text[\\/]l
! go list -mod=vendor -m rsc.io/quote@latest
stderr 'go list -m: rsc.io/quote@latest: cannot query module due to -mod=vendor'
! go get -mod=vendor -u
stderr 'flag provided but not defined: -mod'
stderr 'The flag ''-mod'' is an unknown flag.'

# Since we don't have a complete module graph, 'go list -m' queries
# that require the complete graph should fail with a useful error.
2 changes: 1 addition & 1 deletion src/flag/flag.go
Original file line number Diff line number Diff line change
@@ -926,7 +926,7 @@ func (f *FlagSet) parseOne() (bool, error) {
f.usage()
return false, ErrHelp
}
return false, f.failf("flag provided but not defined: -%s", name)
return false, f.failf("The flag '-%s' is an unknown flag.", name)
}

if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
2 changes: 1 addition & 1 deletion src/flag/flag_test.go
Original file line number Diff line number Diff line change
@@ -454,7 +454,7 @@ func TestUsageOutput(t *testing.T) {
defer func(old []string) { os.Args = old }(os.Args)
os.Args = []string{"app", "-i=1", "-unknown"}
Parse()
const want = "flag provided but not defined: -i\nUsage of app:\n"
const want = "The flag '-i' is an unknown flag.\nUsage of app:\n"
if got := buf.String(); got != want {
t.Errorf("output = %q; want %q", got, want)
}