| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | ) |
| 6 | |
| 7 | const suggestDidYouMeanTemplate = "Did you mean %q?" |
| 8 | |
| 9 | var ( |
| 10 | SuggestFlag SuggestFlagFunc = suggestFlag |
| 11 | SuggestCommand SuggestCommandFunc = suggestCommand |
| 12 | SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate |
| 13 | ) |
| 14 | |
| 15 | type SuggestFlagFunc func(flags []Flag, provided string, hideHelp bool) string |
| 16 | |
| 17 | type SuggestCommandFunc func(commands []*Command, provided string) string |
| 18 | |
| 19 | // jaroDistance is the measure of similarity between two strings. It returns a |
| 20 | // value between 0 and 1, where 1 indicates identical strings and 0 indicates |
| 21 | // completely different strings. |
| 22 | // |
| 23 | // Adapted from https://github.com/xrash/smetrics/blob/5f08fbb34913bc8ab95bb4f2a89a0637ca922666/jaro.go. |
| 24 | func jaroDistance(a, b string) float64 { |
| 25 | if len(a) == 0 && len(b) == 0 { |
| 26 | return 1 |
| 27 | } |
| 28 | if len(a) == 0 || len(b) == 0 { |
| 29 | return 0 |
| 30 | } |
| 31 | |
| 32 | lenA := float64(len(a)) |
| 33 | lenB := float64(len(b)) |
| 34 | hashA := make([]bool, len(a)) |
| 35 | hashB := make([]bool, len(b)) |
| 36 | maxDistance := int(math.Max(0, math.Floor(math.Max(lenA, lenB)/2.0)-1)) |
| 37 | |
| 38 | var matches float64 |
| 39 | for i := 0; i < len(a); i++ { |
| 40 | start := int(math.Max(0, float64(i-maxDistance))) |
| 41 | end := int(math.Min(lenB-1, float64(i+maxDistance))) |
| 42 | |
| 43 | for j := start; j <= end; j++ { |
| 44 | if hashB[j] { |
| 45 | continue |
| 46 | } |
| 47 | if a[i] == b[j] { |
| 48 | hashA[i] = true |
| 49 | hashB[j] = true |
| 50 | matches++ |
| 51 | break |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | if matches == 0 { |
| 56 | return 0 |
| 57 | } |
| 58 | |
| 59 | var transpositions float64 |
| 60 | var j int |
| 61 | for i := 0; i < len(a); i++ { |
| 62 | if !hashA[i] { |
| 63 | continue |
| 64 | } |
| 65 | for !hashB[j] { |
| 66 | j++ |
| 67 | } |
| 68 | if a[i] != b[j] { |
| 69 | transpositions++ |
| 70 | } |
| 71 | j++ |
| 72 | } |
| 73 | |
| 74 | transpositions /= 2 |
| 75 | return ((matches / lenA) + (matches / lenB) + ((matches - transpositions) / matches)) / 3.0 |
| 76 | } |
| 77 | |
| 78 | // jaroWinkler is more accurate when strings have a common prefix up to a |
| 79 | // defined maximum length. |
| 80 | // |
| 81 | // Adapted from https://github.com/xrash/smetrics/blob/5f08fbb34913bc8ab95bb4f2a89a0637ca922666/jaro-winkler.go. |
| 82 | func jaroWinkler(a, b string) float64 { |
| 83 | const ( |
| 84 | boostThreshold = 0.7 |
| 85 | prefixSize = 4 |
| 86 | ) |
| 87 | jaroDist := jaroDistance(a, b) |
| 88 | if jaroDist <= boostThreshold { |
| 89 | return jaroDist |
| 90 | } |
| 91 | |
| 92 | prefix := int(math.Min(float64(len(a)), math.Min(float64(prefixSize), float64(len(b))))) |
| 93 | |
| 94 | var prefixMatch float64 |
| 95 | for i := range prefix { |
| 96 | if a[i] == b[i] { |
| 97 | prefixMatch++ |
| 98 | } else { |
| 99 | break |
| 100 | } |
| 101 | } |
| 102 | return jaroDist + 0.1*prefixMatch*(1.0-jaroDist) |
| 103 | } |
| 104 | |
| 105 | func suggestFlag(flags []Flag, provided string, hideHelp bool) string { |
| 106 | distance := 0.0 |
| 107 | suggestion := "" |
| 108 | |
| 109 | for _, flag := range flags { |
| 110 | flagNames := flag.Names() |
| 111 | if !hideHelp && HelpFlag != nil { |
| 112 | flagNames = append(flagNames, HelpFlag.Names()...) |
| 113 | } |
| 114 | for _, name := range flagNames { |
| 115 | newDistance := jaroWinkler(name, provided) |
| 116 | if newDistance > distance { |
| 117 | distance = newDistance |
| 118 | suggestion = name |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if len(suggestion) == 1 { |
| 124 | suggestion = "-" + suggestion |
| 125 | } else if len(suggestion) > 1 { |
| 126 | suggestion = "--" + suggestion |
| 127 | } |
| 128 | |
| 129 | return suggestion |
| 130 | } |
| 131 | |
| 132 | // suggestCommand takes a list of commands and a provided string to suggest a |
| 133 | // command name |
| 134 | func suggestCommand(commands []*Command, provided string) (suggestion string) { |
| 135 | distance := 0.0 |
| 136 | for _, command := range commands { |
| 137 | for _, name := range append(command.Names(), helpName, helpAlias) { |
| 138 | newDistance := jaroWinkler(name, provided) |
| 139 | if newDistance > distance { |
| 140 | distance = newDistance |
| 141 | suggestion = name |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return suggestion |
| 147 | } |