6408010083
Previously, there was a confusing mix of functionality shared between
the two wherein you would need to use the Kong type for printing errors,
etc. but it did not have access to the context in order to print
context-sensitive usage information. This has been fixed.
Additionally, there are now fuzzy correction suggestions for flags and
commands
Also added a server example which shows how Kong can be used for parsing
in interactive shells. Run with:
$ go run ./_examples/server/*.go
Then interact with:
$ ssh -p 6740 127.0.0.1
40 lines
778 B
Go
40 lines
778 B
Go
package kong
|
|
|
|
import "unicode/utf8"
|
|
|
|
// https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Go
|
|
// License: https://creativecommons.org/licenses/by-sa/3.0/
|
|
func levenshtein(a, b string) int {
|
|
f := make([]int, utf8.RuneCountInString(b)+1)
|
|
|
|
for j := range f {
|
|
f[j] = j
|
|
}
|
|
|
|
for _, ca := range a {
|
|
j := 1
|
|
fj1 := f[0] // fj1 is the value of f[j - 1] in last iteration
|
|
f[0]++
|
|
for _, cb := range b {
|
|
mn := min(f[j]+1, f[j-1]+1) // delete & insert
|
|
if cb != ca {
|
|
mn = min(mn, fj1+1) // change
|
|
} else {
|
|
mn = min(mn, fj1) // matched
|
|
}
|
|
|
|
fj1, f[j] = f[j], mn // save f[j] to fj1(j is about to increase), update f[j] to mn
|
|
j++
|
|
}
|
|
}
|
|
|
|
return f[len(f)-1]
|
|
}
|
|
|
|
func min(a, b int) int {
|
|
if a <= b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|