Skip to content

Commit 07aae19

Browse files
authored
Merge pull request #14446 from bas-vk/cli-help
Rewrite templates for (sub)commands help section
2 parents b596b4b + a346aed commit 07aae19

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

cmd/geth/consolecmd.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ import (
3030

3131
var (
3232
consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
33-
)
3433

35-
var (
3634
consoleCommand = cli.Command{
3735
Action: utils.MigrateFlags(localConsole),
3836
Name: "console",

cmd/geth/usage.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package main
2020

2121
import (
2222
"io"
23+
"sort"
2324

2425
"github.com/ethereum/go-ethereum/cmd/utils"
2526
"github.com/ethereum/go-ethereum/internal/debug"
@@ -189,6 +190,39 @@ var AppHelpFlagGroups = []flagGroup{
189190
},
190191
}
191192

193+
// byCategory sorts an array of flagGroup by Name in the order
194+
// defined in AppHelpFlagGroups.
195+
type byCategory []flagGroup
196+
197+
func (a byCategory) Len() int { return len(a) }
198+
func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
199+
func (a byCategory) Less(i, j int) bool {
200+
iCat, jCat := a[i].Name, a[j].Name
201+
iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
202+
203+
for i, group := range AppHelpFlagGroups {
204+
if iCat == group.Name {
205+
iIdx = i
206+
}
207+
if jCat == group.Name {
208+
jIdx = i
209+
}
210+
}
211+
212+
return iIdx < jIdx
213+
}
214+
215+
func flagCategory(flag cli.Flag) string {
216+
for _, category := range AppHelpFlagGroups {
217+
for _, flg := range category.Flags {
218+
if flg.GetName() == flag.GetName() {
219+
return category.Name
220+
}
221+
}
222+
}
223+
return "MISC"
224+
}
225+
192226
func init() {
193227
// Override the default app help template
194228
cli.AppHelpTemplate = AppHelpTemplate
@@ -198,6 +232,7 @@ func init() {
198232
App interface{}
199233
FlagGroups []flagGroup
200234
}
235+
201236
// Override the default app help printer, but only for the global app help
202237
originalHelpPrinter := cli.HelpPrinter
203238
cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
@@ -227,6 +262,27 @@ func init() {
227262
}
228263
// Render out custom usage screen
229264
originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
265+
} else if tmpl == utils.CommandHelpTemplate {
266+
// Iterate over all command specific flags and categorize them
267+
categorized := make(map[string][]cli.Flag)
268+
for _, flag := range data.(cli.Command).Flags {
269+
if _, ok := categorized[flag.String()]; !ok {
270+
categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
271+
}
272+
}
273+
274+
// sort to get a stable ordering
275+
sorted := make([]flagGroup, 0, len(categorized))
276+
for cat, flgs := range categorized {
277+
sorted = append(sorted, flagGroup{cat, flgs})
278+
}
279+
sort.Sort(byCategory(sorted))
280+
281+
// add sorted array to data and render with default printer
282+
originalHelpPrinter(w, tmpl, map[string]interface{}{
283+
"cmd": data,
284+
"categorizedFlags": sorted,
285+
})
230286
} else {
231287
originalHelpPrinter(w, tmpl, data)
232288
}

cmd/utils/flags.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ import (
5656
"gopkg.in/urfave/cli.v1"
5757
)
5858

59+
var (
60+
CommandHelpTemplate = `{{.cmd.Name}}{{if .cmd.Subcommands}} command{{end}}{{if .cmd.Flags}} [command options]{{end}} [arguments...]
61+
{{if .cmd.Description}}{{.cmd.Description}}
62+
{{end}}{{if .cmd.Subcommands}}
63+
SUBCOMMANDS:
64+
{{range .cmd.Subcommands}}{{.cmd.Name}}{{with .cmd.ShortName}}, {{.cmd}}{{end}}{{ "\t" }}{{.cmd.Usage}}
65+
{{end}}{{end}}{{if .categorizedFlags}}
66+
{{range $idx, $categorized := .categorizedFlags}}{{$categorized.Name}} OPTIONS:
67+
{{range $categorized.Flags}}{{"\t"}}{{.}}
68+
{{end}}
69+
{{end}}{{end}}`
70+
)
71+
5972
func init() {
6073
cli.AppHelpTemplate = `{{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...]
6174
@@ -70,16 +83,7 @@ GLOBAL OPTIONS:
7083
{{end}}{{end}}
7184
`
7285

73-
cli.CommandHelpTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} [arguments...]
74-
{{if .Description}}{{.Description}}
75-
{{end}}{{if .Subcommands}}
76-
SUBCOMMANDS:
77-
{{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
78-
{{end}}{{end}}{{if .Flags}}
79-
OPTIONS:
80-
{{range .Flags}}{{.}}
81-
{{end}}{{end}}
82-
`
86+
cli.CommandHelpTemplate = CommandHelpTemplate
8387
}
8488

8589
// NewApp creates an app with sane defaults.

0 commit comments

Comments
 (0)