diff --git a/src/cmd/go/main.go b/src/cmd/go/main.go index e81969ca4a3144..d6f5c8992a7587 100644 --- a/src/cmd/go/main.go +++ b/src/cmd/go/main.go @@ -109,12 +109,22 @@ func main() { if !cmdIsGoTelemetryOff { telemetry.MaybeParent() // Run the upload process. Opening the counter file is idempotent. } + // Add global -help flag support + var globalHelp bool + flag.BoolVar(&globalHelp, "help", false, "show help") flag.Usage = base.Usage flag.Parse() counter.Inc("go/invocations") counter.CountFlags("go/flag:", *flag.CommandLine) args := flag.Args() + + // Handle global -help flag + if globalHelp { + help.Help(os.Stdout, nil) + return + } + if len(args) < 1 { base.Usage() } @@ -310,12 +320,33 @@ func invoke(cmd *base.Command, args []string) { } } - cmd.Flag.Usage = func() { cmd.Usage() } + // Add -help flag support to all commands + var helpFlag bool + if !cmd.CustomFlags { + cmd.Flag.BoolVar(&helpFlag, "help", false, "show help") + } + + cmd.Flag.Usage = func() { + if helpFlag { + // Show full help like "go help " + help.Help(os.Stdout, strings.Fields(cmd.LongName())) + base.Exit() + } else { + cmd.Usage() + } + } if cmd.CustomFlags { args = args[1:] } else { base.SetFromGOFLAGS(&cmd.Flag) cmd.Flag.Parse(args[1:]) + + // Check if -help flag was set and show full help + if helpFlag { + help.Help(os.Stdout, strings.Fields(cmd.LongName())) + base.Exit() + } + flagCounterPrefix := "go/" + strings.ReplaceAll(cfg.CmdName, " ", "-") + "/flag" counter.CountFlags(flagCounterPrefix+":", cmd.Flag) counter.CountFlagValue(flagCounterPrefix+"/", cmd.Flag, "buildmode") diff --git a/src/cmd/go/testdata/script/help_flag.txt b/src/cmd/go/testdata/script/help_flag.txt new file mode 100644 index 00000000000000..2f265e52bf87cd --- /dev/null +++ b/src/cmd/go/testdata/script/help_flag.txt @@ -0,0 +1,62 @@ +# Test that -help flag works for go commands + +# go -help shows main help (not an error) +go -help +stdout 'Go is a tool for managing Go source code' +stdout 'Usage:' +stdout 'go \[arguments\]' + +# go build -help shows full help +go build -help +stdout 'usage: go build' +stdout 'Build compiles the packages' +stdout 'For more about specifying packages' + +# go install -help shows full help +go install -help +stdout 'usage: go install' +stdout 'Install compiles and installs' +stdout 'For more about specifying packages' + +# go get -help shows full help +go get -help +stdout 'usage: go get' +stdout 'Get resolves its command-line arguments' +stdout 'See also: go build, go install' + +# go fmt -help shows full help +go fmt -help +stdout 'usage: go fmt' +stdout 'Fmt runs the command' +stdout 'See also: go fix, go vet' + +# go run -help shows full help +go run -help +stdout 'usage: go run' +stdout 'Run compiles and runs' +stdout 'See also: go build' + +# go run program.go -help should pass -help to the program, not show go run help +go run helpprog.go -help +stdout 'Program help message' +stdout 'Arguments: \[.*helpprog.*-help\]' + +-- helpprog.go -- +package main + +import ( + "flag" + "fmt" + "os" +) + +func main() { + var help = flag.Bool("help", false, "show help") + flag.Parse() + + fmt.Printf("Arguments: %v\n", os.Args) + + if *help { + fmt.Println("Program help message") + } +}

AltStyle によって変換されたページ (->オリジナル) /