github.com/starshine-sys/bcr@v0.21.0/arg_check.go (about) 1 package bcr 2 3 import ( 4 "errors" 5 "strings" 6 ) 7 8 // Errors 9 var ( 10 ErrorNotEnoughArgs = errors.New("not enough arguments") 11 ErrorTooManyArgs = errors.New("too many arguments") 12 ) 13 14 // CheckMinArgs checks if the argument count is less than the given count 15 func (ctx *Context) CheckMinArgs(c int) (err error) { 16 if len(ctx.Args) < c { 17 return ErrorNotEnoughArgs 18 } 19 return nil 20 } 21 22 // CheckRequiredArgs checks if the arg count is exactly the given count 23 func (ctx *Context) CheckRequiredArgs(c int) (err error) { 24 if len(ctx.Args) != c { 25 if len(ctx.Args) > c { 26 return ErrorTooManyArgs 27 } 28 return ErrorNotEnoughArgs 29 } 30 return nil 31 } 32 33 // CheckArgRange checks if the number of arguments is within the given range 34 func (ctx *Context) CheckArgRange(min, max int) (err error) { 35 if len(ctx.Args) > max { 36 return ErrorTooManyArgs 37 } 38 if len(ctx.Args) < min { 39 return ErrorNotEnoughArgs 40 } 41 return nil 42 } 43 44 func (ctx *Context) argCheck() (err error) { 45 // if there's no requirements, return 46 if ctx.Cmd.Args == nil { 47 return nil 48 } 49 50 // there's only a minimum number of arguments 51 // if there's too few, show an error 52 if ctx.Cmd.Args[1] == -1 && len(ctx.Args) < ctx.Cmd.Args[0] { 53 _, err = ctx.Sendf( 54 ":x: You didn't give enough arguments: this command requires %v arguments, but you gave %v.\n> **Usage:**\n> ```%v%v %v```", 55 ctx.Cmd.Args[0], 56 len(ctx.Args), 57 ctx.Router.Prefixes[0], strings.Join(ctx.FullCommandPath, " "), ctx.Cmd.Usage, 58 ) 59 if err != nil { 60 return err 61 } 62 return errCommandRun 63 } 64 65 // there's only a maximum number of arguments 66 // if there's too many, show an error 67 if ctx.Cmd.Args[0] == -1 && len(ctx.Args) > ctx.Cmd.Args[1] { 68 _, err = ctx.Sendf( 69 ":x: You gave too many arguments: this command requires at most %v arguments, but you gave %v.\n> **Usage:**\n> ```%v%v %v```", 70 ctx.Cmd.Args[1], 71 len(ctx.Args), 72 ctx.Router.Prefixes[0], strings.Join(ctx.FullCommandPath, " "), ctx.Cmd.Usage, 73 ) 74 if err != nil { 75 return err 76 } 77 return errCommandRun 78 } 79 80 // there's both a minimum and maximum number of arguments 81 if ctx.Cmd.Args[0] != -1 && ctx.Cmd.Args[1] != -1 { 82 if ctx.Cmd.Args[0] == ctx.Cmd.Args[1] && len(ctx.Args) != ctx.Cmd.Args[0] { 83 _, err = ctx.Sendf( 84 ":x: This command requires exactly %v arguments, but you gave %v.\n> **Usage:**\n> ```%v%v %v```", 85 ctx.Cmd.Args[0], 86 len(ctx.Args), 87 ctx.Router.Prefixes[0], strings.Join(ctx.FullCommandPath, " "), ctx.Cmd.Usage, 88 ) 89 } else if len(ctx.Args) < ctx.Cmd.Args[0] { 90 _, err = ctx.Sendf( 91 ":x: You didn't give enough arguments: this command requires %v arguments, but you gave %v.\n> **Usage:**\n> ```%v%v %v```", 92 ctx.Cmd.Args[0], 93 len(ctx.Args), 94 ctx.Router.Prefixes[0], strings.Join(ctx.FullCommandPath, " "), ctx.Cmd.Usage, 95 ) 96 } else if len(ctx.Args) > ctx.Cmd.Args[1] { 97 _, err = ctx.Sendf( 98 ":x: You gave too many arguments: this command requires at most %v arguments, but you gave %v.\n> **Usage:**\n> ```%v%v %v```", 99 ctx.Cmd.Args[1], 100 len(ctx.Args), 101 ctx.Router.Prefixes[0], strings.Join(ctx.FullCommandPath, " "), ctx.Cmd.Usage, 102 ) 103 } 104 if err != nil { 105 return err 106 } 107 return errCommandRun 108 } 109 110 // everything's fine, return nil 111 return nil 112 }