Skip to content
Open
Show file tree
Hide file tree
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
33 changes: 32 additions & 1 deletion src/cmd/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 <command>"
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")
Expand Down
62 changes: 62 additions & 0 deletions src/cmd/go/testdata/script/help_flag.txt
Original file line number Diff line number Diff line change
@@ -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 <command> \[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")
}
}