github.com/klaytn/klaytn@v1.12.1/cmd/utils/usage.go (about) 1 // Modifications Copyright 2019 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of go-ethereum. 4 // 5 // go-ethereum is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // go-ethereum is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from cmd/geth/usage.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 // Contains the Klaytn node command usage template and generator. 22 23 package utils 24 25 import ( 26 "io" 27 "strings" 28 29 "github.com/urfave/cli/v2" 30 ) 31 32 func NewHelpPrinter(fg []FlagGroup) func(w io.Writer, tmp string, data interface{}) { 33 originalHelpPrinter := cli.HelpPrinter 34 return func(w io.Writer, tmpl string, data interface{}) { 35 type helpData struct { 36 App interface{} 37 FlagGroups []FlagGroup 38 } 39 40 if tmpl == GlobalAppHelpTemplate { 41 // Iterate over all the flags and add any uncategorized ones 42 categorized := make(map[string]struct{}) 43 for _, group := range fg { 44 for _, flag := range group.Flags { 45 categorized[flag.String()] = struct{}{} 46 } 47 } 48 var uncategorized []cli.Flag 49 for _, flag := range data.(*cli.App).Flags { 50 if _, ok := categorized[flag.String()]; !ok { 51 if strings.HasPrefix(flag.Names()[0], "dashboard") { 52 continue 53 } 54 uncategorized = append(uncategorized, flag) 55 } 56 } 57 if len(uncategorized) > 0 { 58 // Append all ungategorized options to the misc group 59 miscs := len(fg[len(fg)-1].Flags) 60 fg[len(fg)-1].Flags = append(fg[len(fg)-1].Flags, uncategorized...) 61 62 // Make sure they are removed afterwards 63 defer func() { 64 fg[len(fg)-1].Flags = fg[len(fg)-1].Flags[:miscs] 65 }() 66 } 67 // Render out custom usage screen 68 originalHelpPrinter(w, tmpl, helpData{data, fg}) 69 } else if tmpl == CommandHelpTemplate { 70 // Iterate over all command specific flags and categorize them 71 categorized := make(map[string][]cli.Flag) 72 for _, flag := range data.(*cli.Command).Flags { 73 if _, ok := categorized[flag.String()]; !ok { 74 categorized[flagCategory(flag, fg)] = append(categorized[flagCategory(flag, fg)], flag) 75 } 76 } 77 78 // sort to get a stable ordering 79 sorted := make([]FlagGroup, 0, len(categorized)) 80 for cat, flgs := range categorized { 81 sorted = append(sorted, FlagGroup{cat, flgs}) 82 } 83 84 // add sorted array to data and render with default printer 85 originalHelpPrinter(w, tmpl, map[string]interface{}{ 86 "cmd": data, 87 "categorizedFlags": sorted, 88 }) 89 } else { 90 originalHelpPrinter(w, tmpl, data) 91 } 92 } 93 }