cosmossdk.io/client/v2@v2.0.0-beta.1/autocli/validate.go (about) 1 package autocli 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/spf13/cobra" 9 ) 10 11 // NOTE: this was copied from client/cmd.go to avoid introducing a dependency 12 // on the v1 client package. 13 14 // validateCmd returns unknown command error or Help display if help flag set 15 func validateCmd(cmd *cobra.Command, args []string) error { 16 var unknownCmd string 17 var skipNext bool 18 19 for _, arg := range args { 20 // search for help flag 21 if arg == "--help" || arg == "-h" { 22 return cmd.Help() 23 } 24 25 // check if the current arg is a flag 26 switch { 27 case len(arg) > 0 && (arg[0] == '-'): 28 // the next arg should be skipped if the current arg is a 29 // flag and does not use "=" to assign the flag's value 30 if !strings.Contains(arg, "=") { 31 skipNext = true 32 } else { 33 skipNext = false 34 } 35 case skipNext: 36 // skip current arg 37 skipNext = false 38 case unknownCmd == "": 39 // unknown command found 40 // continue searching for help flag 41 unknownCmd = arg 42 } 43 } 44 45 // return the help screen if no unknown command is found 46 if unknownCmd != "" { 47 err := fmt.Sprintf("unknown command \"%s\" for \"%s\"", unknownCmd, cmd.CalledAs()) 48 49 // build suggestions for unknown argument 50 if suggestions := cmd.SuggestionsFor(unknownCmd); len(suggestions) > 0 { 51 err += "\n\nDid you mean this?\n" 52 for _, s := range suggestions { 53 err += fmt.Sprintf("\t%v\n", s) 54 } 55 } 56 return errors.New(err) 57 } 58 59 return cmd.Help() 60 }