From a0e659d5136104039ac16d1718b6ef26f458e22c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Szczepaniak?= Date: Sat, 7 Dec 2019 14:10:13 +0100 Subject: [PATCH] internal/lsp, internal/tool: clean up command line of gopls Descriptions of certain commands were changed to not start with capital letter. All of the commands were splitted into so called main commands and feature commands. Package tool did have a limitation that revealed itself when command was invoked only with `-h`, i.e. `gopls -h`. Limitation was that in above mentioned case, FlagSet.Parse() was intercepting `-h` flag and printing just default usage. Updates golang/go#35855 Change-Id: I9bd27fc72e8fb8d18025d95ebcae974dd5583e91 Reviewed-on: https://go-review.googlesource.com/c/tools/+/210359 Run-TryBot: Ian Cottrell TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- internal/lsp/cmd/cmd.go | 33 +++++++++++++++++++++++++++++---- internal/lsp/cmd/serve.go | 4 ++-- internal/tool/tool.go | 5 +++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/internal/lsp/cmd/cmd.go b/internal/lsp/cmd/cmd.go index e0737ad3e5..fe957c50f8 100644 --- a/internal/lsp/cmd/cmd.go +++ b/internal/lsp/cmd/cmd.go @@ -61,10 +61,10 @@ type Application struct { Remote string `flag:"remote" help:"*EXPERIMENTAL* - forward all commands to a remote lsp"` // Enable verbose logging - Verbose bool `flag:"v" help:"Verbose output"` + Verbose bool `flag:"v" help:"verbose output"` // Control ocagent export of telemetry - OCAgent string `flag:"ocagent" help:"The address of the ocagent, or off"` + OCAgent string `flag:"ocagent" help:"the address of the ocagent, or off"` // PrepareOptions is called to update the options when a new view is built. // It is primarily to allow the behavior of gopls to be modified by hooks. @@ -101,9 +101,22 @@ func (app *Application) ShortHelp() string { // This includes the short help for all the sub commands. func (app *Application) DetailedHelp(f *flag.FlagSet) { fmt.Fprint(f.Output(), ` +gopls is a Go language server. It is typically used with an editor to provide +language features. When no command is specified, gopls will default to the 'serve' +command. The language features can also be accessed via the gopls command-line interface. + Available commands are: +`, app.Name(), app.Name(), app.Serve.Name()) + fmt.Fprint(f.Output(), ` +main: `) - for _, c := range app.commands() { + for _, c := range app.mainCommands() { + fmt.Fprintf(f.Output(), " %s : %v\n", c.Name(), c.ShortHelp()) + } + fmt.Fprint(f.Output(), ` +features: +`) + for _, c := range app.featureCommands() { fmt.Fprintf(f.Output(), " %s : %v\n", c.Name(), c.ShortHelp()) } fmt.Fprint(f.Output(), ` @@ -138,9 +151,22 @@ func (app *Application) Run(ctx context.Context, args ...string) error { // command line. // The command is specified by the first non flag argument. func (app *Application) commands() []tool.Application { + var commands []tool.Application + commands = append(commands, app.mainCommands()...) + commands = append(commands, app.featureCommands()...) + return commands +} + +func (app *Application) mainCommands() []tool.Application { return []tool.Application{ &app.Serve, + &version{app: app}, &bug{}, + } +} + +func (app *Application) featureCommands() []tool.Application { + return []tool.Application{ &check{app: app}, &foldingRanges{app: app}, &format{app: app}, @@ -154,7 +180,6 @@ func (app *Application) commands() []tool.Application { &signature{app: app}, &suggestedfix{app: app}, &symbols{app: app}, - &version{app: app}, } } diff --git a/internal/lsp/cmd/serve.go b/internal/lsp/cmd/serve.go index 2fdec959a7..df99f4ae03 100644 --- a/internal/lsp/cmd/serve.go +++ b/internal/lsp/cmd/serve.go @@ -34,8 +34,8 @@ type Serve struct { Mode string `flag:"mode" help:"no effect"` Port int `flag:"port" help:"port on which to run gopls for debugging purposes"` Address string `flag:"listen" help:"address on which to listen for remote connections"` - Trace bool `flag:"rpc.trace" help:"Print the full rpc trace in lsp inspector format"` - Debug string `flag:"debug" help:"Serve debug information on the supplied address"` + Trace bool `flag:"rpc.trace" help:"print the full rpc trace in lsp inspector format"` + Debug string `flag:"debug" help:"serve debug information on the supplied address"` app *Application } diff --git a/internal/tool/tool.go b/internal/tool/tool.go index b50569aced..9865d2a6b1 100644 --- a/internal/tool/tool.go +++ b/internal/tool/tool.go @@ -102,6 +102,11 @@ func Main(ctx context.Context, app Application, args []string) { // error. func Run(ctx context.Context, app Application, args []string) error { s := flag.NewFlagSet(app.Name(), flag.ExitOnError) + s.Usage = func() { + fmt.Fprint(s.Output(), app.ShortHelp()) + fmt.Fprintf(s.Output(), "\n\nUsage: %v [flags] %v\n", app.Name(), app.Usage()) + app.DetailedHelp(s) + } p := addFlags(s, reflect.StructField{}, reflect.ValueOf(app)) s.Parse(args)