cosmossdk.io/client/v2@v2.0.0-beta.1/autocli/util.go (about) 1 package autocli 2 3 import ( 4 "strings" 5 6 "github.com/spf13/cobra" 7 "google.golang.org/protobuf/reflect/protoreflect" 8 9 "cosmossdk.io/client/v2/internal/strcase" 10 ) 11 12 // findSubCommand finds a sub-command of the provided command whose Use 13 // string is or begins with the provided subCmdName. 14 // It verifies the command's aliases as well. 15 func findSubCommand(cmd *cobra.Command, subCmdName string) *cobra.Command { 16 for _, subCmd := range cmd.Commands() { 17 use := subCmd.Use 18 if use == subCmdName || strings.HasPrefix(use, subCmdName+" ") { 19 return subCmd 20 } 21 22 for _, alias := range subCmd.Aliases { 23 if alias == subCmdName || strings.HasPrefix(alias, subCmdName+" ") { 24 return subCmd 25 } 26 } 27 } 28 return nil 29 } 30 31 // topLevelCmd creates a new top-level command with the provided name and 32 // description. The command will have DisableFlagParsing set to false and 33 // SuggestionsMinimumDistance set to 2. 34 func topLevelCmd(use, short string) *cobra.Command { 35 return &cobra.Command{ 36 Use: use, 37 Short: short, 38 DisableFlagParsing: false, 39 SuggestionsMinimumDistance: 2, 40 RunE: validateCmd, 41 } 42 } 43 44 func protoNameToCliName(name protoreflect.Name) string { 45 return strcase.ToKebab(string(name)) 46 }