diff options
author | Bas van Kervel <basvankervel@gmail.com> | 2017-05-25 15:15:51 +0800 |
---|---|---|
committer | Bas van Kervel <basvankervel@gmail.com> | 2017-05-25 15:15:51 +0800 |
commit | a346aedb90937de822e421cfa64405f9078af8a0 (patch) | |
tree | 38edc8f75e8294e7e36e6447945524c4a83b60b5 /cmd/geth | |
parent | ef25b826e655f8e6a57fc7a05454bf356382bd5f (diff) | |
download | go-tangerine-a346aedb90937de822e421cfa64405f9078af8a0.tar.gz go-tangerine-a346aedb90937de822e421cfa64405f9078af8a0.tar.zst go-tangerine-a346aedb90937de822e421cfa64405f9078af8a0.zip |
cmd/geth: reorganise help section for new cli flag handling
Diffstat (limited to 'cmd/geth')
-rw-r--r-- | cmd/geth/consolecmd.go | 2 | ||||
-rw-r--r-- | cmd/geth/usage.go | 56 |
2 files changed, 56 insertions, 2 deletions
diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 6efdbbf57..f8a923aac 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -30,9 +30,7 @@ import ( var ( consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag} -) -var ( consoleCommand = cli.Command{ Action: utils.MigrateFlags(localConsole), Name: "console", diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index c719a87b1..403e93a9d 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -20,6 +20,7 @@ package main import ( "io" + "sort" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/internal/debug" @@ -189,6 +190,39 @@ var AppHelpFlagGroups = []flagGroup{ }, } +// byCategory sorts an array of flagGroup by Name in the order +// defined in AppHelpFlagGroups. +type byCategory []flagGroup + +func (a byCategory) Len() int { return len(a) } +func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a byCategory) Less(i, j int) bool { + iCat, jCat := a[i].Name, a[j].Name + iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last + + for i, group := range AppHelpFlagGroups { + if iCat == group.Name { + iIdx = i + } + if jCat == group.Name { + jIdx = i + } + } + + return iIdx < jIdx +} + +func flagCategory(flag cli.Flag) string { + for _, category := range AppHelpFlagGroups { + for _, flg := range category.Flags { + if flg.GetName() == flag.GetName() { + return category.Name + } + } + } + return "MISC" +} + func init() { // Override the default app help template cli.AppHelpTemplate = AppHelpTemplate @@ -198,6 +232,7 @@ func init() { App interface{} FlagGroups []flagGroup } + // Override the default app help printer, but only for the global app help originalHelpPrinter := cli.HelpPrinter cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) { @@ -227,6 +262,27 @@ func init() { } // Render out custom usage screen originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups}) + } else if tmpl == utils.CommandHelpTemplate { + // Iterate over all command specific flags and categorize them + categorized := make(map[string][]cli.Flag) + for _, flag := range data.(cli.Command).Flags { + if _, ok := categorized[flag.String()]; !ok { + categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag) + } + } + + // sort to get a stable ordering + sorted := make([]flagGroup, 0, len(categorized)) + for cat, flgs := range categorized { + sorted = append(sorted, flagGroup{cat, flgs}) + } + sort.Sort(byCategory(sorted)) + + // add sorted array to data and render with default printer + originalHelpPrinter(w, tmpl, map[string]interface{}{ + "cmd": data, + "categorizedFlags": sorted, + }) } else { originalHelpPrinter(w, tmpl, data) } |