gopls: take control of the flag printing

Add a copy of flag.PrintDefaults so that we can make changes.
This CL does not actually modify anything (although it does have
to detect the private stringValue type a slightly different way)

For #41860

Change-Id: I19512041f9c1bfde56226aea48302cd63cf670e2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/367837
Trust: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Ian Cottrell 2021-11-24 16:41:22 -05:00
parent afd524fc39
commit 217a9fc29a
23 changed files with 74 additions and 27 deletions

View File

@ -32,7 +32,7 @@ Example:
$ gopls call_hierarchy helper/helper.go:8:6
$ gopls call_hierarchy helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (c *callHierarchy) Run(ctx context.Context, args ...string) error {

View File

@ -28,7 +28,7 @@ Example: show the diagnostic results of this file:
$ gopls check internal/lsp/cmd/check.go
`)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run performs the check on the files specified by args and prints the

View File

@ -15,6 +15,7 @@ import (
"io/ioutil"
"log"
"os"
"reflect"
"strings"
"sync"
"text/tabwriter"
@ -132,7 +133,53 @@ command:
fmt.Fprintf(w, " %s\t%s\n", c.Name(), c.ShortHelp())
}
fmt.Fprint(w, "\nflags:\n")
f.PrintDefaults()
printFlagDefaults(f)
}
// this is a slightly modified version of flag.PrintDefaults to give us control
func printFlagDefaults(s *flag.FlagSet) {
s.VisitAll(func(f *flag.Flag) {
var b strings.Builder
fmt.Fprintf(&b, " -%s", f.Name) // Two spaces before -; see next two comments.
name, usage := flag.UnquoteUsage(f)
if len(name) > 0 {
b.WriteString(" ")
b.WriteString(name)
}
// Boolean flags of one ASCII letter are so common we
// treat them specially, putting their usage on the same line.
if b.Len() <= 4 { // space, space, '-', 'x'.
b.WriteString("\t")
} else {
// Four spaces before the tab triggers good alignment
// for both 4- and 8-space tab stops.
b.WriteString("\n \t")
}
b.WriteString(strings.ReplaceAll(usage, "\n", "\n \t"))
if !isZeroValue(f, f.DefValue) {
if reflect.TypeOf(f.Value).Elem().Name() == "stringValue" {
fmt.Fprintf(&b, " (default %q)", f.DefValue)
} else {
fmt.Fprintf(&b, " (default %v)", f.DefValue)
}
}
fmt.Fprint(s.Output(), b.String(), "\n")
})
}
// isZeroValue is copied from the flags package
func isZeroValue(f *flag.Flag, value string) bool {
// Build a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in.
// This works unless the Value type is itself an interface type.
typ := reflect.TypeOf(f.Value)
var z reflect.Value
if typ.Kind() == reflect.Ptr {
z = reflect.New(typ.Elem())
} else {
z = reflect.Zero(typ)
}
return value == z.Interface().(flag.Value).String()
}
// Run takes the args after top level flag processing, and invokes the correct

View File

@ -55,7 +55,7 @@ Example: show the definition of the identifier at syntax at offset %[1]v in this
definition-flags:
`, exampleLine, exampleColumn, exampleOffset)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run performs the definition query as specified by args and prints the

View File

@ -29,7 +29,7 @@ Example:
$ gopls folding_ranges helper/helper.go
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (r *foldingRanges) Run(ctx context.Context, args ...string) error {

View File

@ -40,7 +40,7 @@ Example: reformat this file:
format-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run performs the check on the files specified by args and prints the

View File

@ -32,7 +32,7 @@ Example:
$ gopls highlight helper/helper.go:8:6
$ gopls highlight helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (r *highlight) Run(ctx context.Context, args ...string) error {

View File

@ -32,7 +32,7 @@ Example:
$ gopls implementation helper/helper.go:8:6
$ gopls implementation helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (i *implementation) Run(ctx context.Context, args ...string) error {

View File

@ -38,7 +38,7 @@ Example: update imports statements in a file:
imports-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run performs diagnostic checks on the file specified and either;

View File

@ -30,7 +30,7 @@ func (v *version) Usage() string { return "" }
func (v *version) ShortHelp() string { return "print the gopls version information" }
func (v *version) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), ``)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run prints version information to stdout.
@ -50,7 +50,7 @@ func (b *bug) Usage() string { return "" }
func (b *bug) ShortHelp() string { return "report a bug in gopls" }
func (b *bug) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), ``)
f.PrintDefaults()
printFlagDefaults(f)
}
const goplsBugPrefix = "x/tools/gopls: <DESCRIBE THE PROBLEM>"
@ -98,7 +98,7 @@ func (j *apiJSON) Usage() string { return "" }
func (j *apiJSON) ShortHelp() string { return "print json describing gopls API" }
func (j *apiJSON) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), ``)
f.PrintDefaults()
printFlagDefaults(f)
}
func (j *apiJSON) Run(ctx context.Context, args ...string) error {
@ -120,7 +120,7 @@ func (l *licenses) Usage() string { return "" }
func (l *licenses) ShortHelp() string { return "print licenses of included software" }
func (l *licenses) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), ``)
f.PrintDefaults()
printFlagDefaults(f)
}
const licensePreamble = `

View File

@ -36,7 +36,7 @@ Example: list links contained within a file:
links-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run finds all the links within a document

View File

@ -32,7 +32,7 @@ Example:
$ gopls prepare_rename helper/helper.go:8:6
$ gopls prepare_rename helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}
// ErrInvalidRenamePosition is returned when prepareRename is run at a position that

View File

@ -36,7 +36,7 @@ Example:
references-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (r *references) Run(ctx context.Context, args ...string) error {

View File

@ -85,7 +85,7 @@ $ gopls -remote=localhost:8082 remote sessions
func (c *listSessions) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), listSessionsExamples)
f.PrintDefaults()
printFlagDefaults(f)
}
func (c *listSessions) Run(ctx context.Context, args ...string) error {
@ -131,7 +131,7 @@ $ gopls -remote=localhost:8082 remote debug localhost:8083
func (c *startDebugging) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), startDebuggingExamples)
f.PrintDefaults()
printFlagDefaults(f)
}
func (c *startDebugging) Run(ctx context.Context, args ...string) error {

View File

@ -44,7 +44,7 @@ Example:
rename-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run renames the specified identifier and either;

View File

@ -61,7 +61,7 @@ Example: show the semantic tokens for this file:
$ gopls semtok internal/lsp/cmd/semtok.go
`)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run performs the semtok on the files specified by args and prints the

View File

@ -54,7 +54,7 @@ a child of an editor process.
server-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (s *Serve) remoteArgs(network, address string) []string {

View File

@ -31,7 +31,7 @@ Example:
$ gopls signature helper/helper.go:8:6
$ gopls signature helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (r *signature) Run(ctx context.Context, args ...string) error {

View File

@ -24,7 +24,7 @@ func (s subcommands) DetailedHelp(f *flag.FlagSet) {
for _, c := range s {
fmt.Fprintf(w, " %s\t%s\n", c.Name(), c.ShortHelp())
}
f.PrintDefaults()
printFlagDefaults(f)
}
func (s subcommands) Usage() string { return "<subcommand> [arg]..." }

View File

@ -38,7 +38,7 @@ Example: apply suggested fixes for this file
fix-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}
// Run performs diagnostic checks on the file specified and either;

View File

@ -30,7 +30,7 @@ func (r *symbols) DetailedHelp(f *flag.FlagSet) {
Example:
$ gopls symbols helper/helper.go
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (r *symbols) Run(ctx context.Context, args ...string) error {
if len(args) != 1 {

View File

@ -51,7 +51,7 @@ func (c *generateWorkspaceMod) ShortHelp() string {
}
func (c *generateWorkspaceMod) DetailedHelp(f *flag.FlagSet) {
f.PrintDefaults()
printFlagDefaults(f)
}
func (c *generateWorkspaceMod) Run(ctx context.Context, args ...string) error {

View File

@ -33,7 +33,7 @@ Example:
workspace_symbol-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (r *workspaceSymbol) Run(ctx context.Context, args ...string) error {