From 4a376d5f6b921deb49ca461f33ae4bd24a401a0a Mon Sep 17 00:00:00 2001 From: Juan Gonzalez Date: Sat, 30 Aug 2025 12:41:40 -0700 Subject: [PATCH 1/4] support the --help flag --- src/cmd/go/main.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/main.go b/src/cmd/go/main.go index e81969ca4a3144..07fae8aa19bedd 100644 --- a/src/cmd/go/main.go +++ b/src/cmd/go/main.go @@ -310,12 +310,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") From e89f7cae3f693e39eb4d33170ad5c92771f80f86 Mon Sep 17 00:00:00 2001 From: Juan Gonzalez Date: Sat, 30 Aug 2025 12:52:50 -0700 Subject: [PATCH 2/4] add test --- src/cmd/go/testdata/script/help_flag.txt | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/cmd/go/testdata/script/help_flag.txt 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..c6f076f9fd4fb2 --- /dev/null +++ b/src/cmd/go/testdata/script/help_flag.txt @@ -0,0 +1,56 @@ +# Test that --help flag works for go commands + +# 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") + } +} From 70b640fb1b7863de022e33386551816a5aea91be Mon Sep 17 00:00:00 2001 From: Juan Gonzalez Date: Sat, 30 Aug 2025 13:06:28 -0700 Subject: [PATCH 3/4] support `go --help` --- src/cmd/go/main.go | 11 +++++++++++ src/cmd/go/testdata/script/help_flag.txt | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/src/cmd/go/main.go b/src/cmd/go/main.go index 07fae8aa19bedd..43d80e34860205 100644 --- a/src/cmd/go/main.go +++ b/src/cmd/go/main.go @@ -109,12 +109,23 @@ 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() } diff --git a/src/cmd/go/testdata/script/help_flag.txt b/src/cmd/go/testdata/script/help_flag.txt index c6f076f9fd4fb2..3e3938080ef650 100644 --- a/src/cmd/go/testdata/script/help_flag.txt +++ b/src/cmd/go/testdata/script/help_flag.txt @@ -1,5 +1,11 @@ # 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' From 37c78b527baa78e415d885b5c99b3caeb75dc5e9 Mon Sep 17 00:00:00 2001 From: Juan Gonzalez Date: Sun, 31 Aug 2025 10:19:30 -0700 Subject: [PATCH 4/4] Update help flag from --help to -help across commands and tests --- src/cmd/go/main.go | 13 +++++----- src/cmd/go/testdata/script/help_flag.txt | 32 ++++++++++++------------ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/cmd/go/main.go b/src/cmd/go/main.go index 43d80e34860205..d6f5c8992a7587 100644 --- a/src/cmd/go/main.go +++ b/src/cmd/go/main.go @@ -109,23 +109,22 @@ func main() { if !cmdIsGoTelemetryOff { telemetry.MaybeParent() // Run the upload process. Opening the counter file is idempotent. } - // Add global --help flag support + // 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 + + // Handle global -help flag if globalHelp { help.Help(os.Stdout, nil) return } - + if len(args) < 1 { base.Usage() } @@ -321,7 +320,7 @@ func invoke(cmd *base.Command, args []string) { } } - // Add --help flag support to all commands + // Add -help flag support to all commands var helpFlag bool if !cmd.CustomFlags { cmd.Flag.BoolVar(&helpFlag, "help", false, "show help") @@ -342,7 +341,7 @@ func invoke(cmd *base.Command, args []string) { base.SetFromGOFLAGS(&cmd.Flag) cmd.Flag.Parse(args[1:]) - // Check if --help flag was set and show full help + // Check if -help flag was set and show full help if helpFlag { help.Help(os.Stdout, strings.Fields(cmd.LongName())) base.Exit() diff --git a/src/cmd/go/testdata/script/help_flag.txt b/src/cmd/go/testdata/script/help_flag.txt index 3e3938080ef650..2f265e52bf87cd 100644 --- a/src/cmd/go/testdata/script/help_flag.txt +++ b/src/cmd/go/testdata/script/help_flag.txt @@ -1,45 +1,45 @@ -# Test that --help flag works for go commands +# Test that -help flag works for go commands -# go --help shows main help (not an error) -go --help +# 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 +# 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 +# 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 +# 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 +# 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 +# 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 +# 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\]' +stdout 'Arguments: \[.*helpprog.*-help\]' -- helpprog.go -- package main