13 // HasSingleFlagArgument checks whether selected flag was used once and has requested argument.
14 func HasSingleFlagArgument(flag string, argument string, params []string) bool {
15 found := filterFlags(params, flag)
20 _, value := splitKV(found[0], "=")
21 if value != argument {
27 // HasFlagArgumentIncluded checks whether selected flag includes requested argument.
28 func HasFlagArgumentIncluded(flag string, argument string, params []string) bool {
29 found := filterFlags(params, flag)
34 _, values := splitKV(found[0], "=")
35 for _, v := range strings.Split(values, ",") {
43 // HasSingleFlagNonemptyArgument checks whether selected flag was used once and has non-empty argument.
44 func HasSingleFlagNonemptyArgument(flag string, params []string) bool {
45 found := filterFlags(params, flag)
50 _, value := splitKV(found[0], "=")
57 // HasSingleFlagValidPort checks whether selected flag has valid port as an argument in given command.
58 func HasSingleFlagValidPort(flag string, params []string) bool {
59 found := filterFlags(params, flag)
64 _, value := splitKV(found[0], "=")
65 port, err := strconv.Atoi(value) // what about empty parameter?
69 if port < portLowest || port > portHighest {
75 // HasSingleFlagValidTimeout checks whether selected flag has valid timeout as an argument in given command.
76 func HasSingleFlagValidTimeout(flag string, min int, max int, params []string) bool {
77 found := filterFlags(params, flag)
82 _, value := splitKV(found[0], "=")
83 timeout, err := strconv.Atoi(value) // what about empty parameter?
87 if timeout < min || timeout > max {
93 // HasSingleFlagRecommendedNumericArgument checks whether selected flag was used once and has
94 // an argument that is greater or equal than the recommended value for given command.
95 func HasSingleFlagRecommendedNumericArgument(flag string, recommendation int, params []string) bool {
96 found := filterFlags(params, flag)
101 _, value := splitKV(found[0], "=")
102 arg, err := strconv.Atoi(value) // what about empty parameter?
106 if arg < recommendation {
112 // filterFlags returns all occurrences of selected flag.
113 func filterFlags(strs []string, flag string) []string {
114 var filtered []string
115 for _, str := range strs {
116 if strings.HasPrefix(str, flag) {
117 filtered = append(filtered, str)
123 // splitKV splits key and value (after first occurrence of separator).
124 func splitKV(s, sep string) (string, string) {
125 ret := strings.SplitN(s, sep, 2)
126 return ret[0], ret[1]