From 9790d1a45f475f61642c9dedd24c8db6638fcd7f Mon Sep 17 00:00:00 2001 From: Sam Xie Date: Mon, 16 Jun 2025 17:48:31 -0700 Subject: [PATCH 1/2] Remove CC-BY-SA-3.0 license by replacing the implementation of levenshtein --- context.go | 4 +++- go.mod | 9 +++++++-- go.sum | 2 ++ levenshtein.go | 39 --------------------------------------- 4 files changed, 12 insertions(+), 42 deletions(-) delete mode 100644 levenshtein.go diff --git a/context.go b/context.go index 6a4989f6..193b080f 100644 --- a/context.go +++ b/context.go @@ -8,6 +8,8 @@ import ( "sort" "strconv" "strings" + + "github.com/agnivade/levenshtein" ) // Path records the nodes and parsed values from the current command-line. @@ -1134,7 +1136,7 @@ func findPotentialCandidates(needle string, haystack []string, format string, ar } closestCandidates := []string{} for _, candidate := range haystack { - if strings.HasPrefix(candidate, needle) || levenshtein(candidate, needle) <= 2 { + if strings.HasPrefix(candidate, needle) || levenshtein.ComputeDistance(candidate, needle) <= 2 { closestCandidates = append(closestCandidates, fmt.Sprintf("%q", candidate)) } } diff --git a/go.mod b/go.mod index 411174db..c5faa18b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,11 @@ require ( github.com/alecthomas/repr v0.4.0 ) -require github.com/hexops/gotextdiff v1.0.3 // indirect +require ( + github.com/agnivade/levenshtein v1.2.1 // indirect + github.com/hexops/gotextdiff v1.0.3 // indirect +) + +go 1.21 -go 1.20 +toolchain go1.24.0 diff --git a/go.sum b/go.sum index f571a34a..16e87e49 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/agnivade/levenshtein v1.2.1 h1:EHBY3UOn1gwdy/VbFwgo4cxecRznFk7fKWN1KOX7eoM= +github.com/agnivade/levenshtein v1.2.1/go.mod h1:QVVI16kDrtSuwcpd0p1+xMC6Z/VfhtCyDIjcwga4/DU= github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0= github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= diff --git a/levenshtein.go b/levenshtein.go deleted file mode 100644 index 6837d6c3..00000000 --- a/levenshtein.go +++ /dev/null @@ -1,39 +0,0 @@ -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 { //nolint:predeclared - if a <= b { - return a - } - return b -} From c8cddd2b1eb02f65590f25e761b295f5cdef28f1 Mon Sep 17 00:00:00 2001 From: Sam Xie Date: Mon, 16 Jun 2025 17:51:25 -0700 Subject: [PATCH 2/2] go mod tidy --- go.mod | 8 ++------ go.sum | 4 ++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index c5faa18b..277fcf92 100644 --- a/go.mod +++ b/go.mod @@ -1,15 +1,11 @@ module github.com/alecthomas/kong require ( + github.com/agnivade/levenshtein v1.2.1 github.com/alecthomas/assert/v2 v2.11.0 github.com/alecthomas/repr v0.4.0 ) -require ( - github.com/agnivade/levenshtein v1.2.1 // indirect - github.com/hexops/gotextdiff v1.0.3 // indirect -) +require github.com/hexops/gotextdiff v1.0.3 // indirect go 1.21 - -toolchain go1.24.0 diff --git a/go.sum b/go.sum index 16e87e49..848bfba3 100644 --- a/go.sum +++ b/go.sum @@ -4,5 +4,9 @@ github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8v github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= +github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q= +github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE= +github.com/dgryski/trifles v0.0.0-20230903005119-f50d829f2e54 h1:SG7nF6SRlWhcT7cNTs5R6Hk4V2lcmLz2NsG2VnInyNo= +github.com/dgryski/trifles v0.0.0-20230903005119-f50d829f2e54/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=