-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better error message on unrecognized command
Closes issue #1436 License: MIT Signed-off-by: Shaun Bruce <[email protected]>
- Loading branch information
Showing
5 changed files
with
298 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
166 changes: 166 additions & 0 deletions
166
Godeps/_workspace/src/github.com/texttheater/golang-levenshtein/levenshtein/levenshtein.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
.../_workspace/src/github.com/texttheater/golang-levenshtein/levenshtein/levenshtein_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cli | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
levenshtein "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/texttheater/golang-levenshtein/levenshtein" | ||
cmds "github.com/ipfs/go-ipfs/commands" | ||
) | ||
|
||
// Make a custom slice that can be sorted by its levenshtein value | ||
type suggestionSlice []*suggestion | ||
|
||
type suggestion struct { | ||
cmd string | ||
levenshtein int | ||
} | ||
|
||
func (s suggestionSlice) Len() int { | ||
return len(s) | ||
} | ||
|
||
func (s suggestionSlice) Swap(i, j int) { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
|
||
func (s suggestionSlice) Less(i, j int) bool { | ||
return s[i].levenshtein < s[j].levenshtein | ||
} | ||
|
||
func suggestUnknownCmd(args []string, root *cmds.Command) []string { | ||
arg := args[0] | ||
var suggestions []string | ||
sortableSuggestions := make(suggestionSlice, 0) | ||
var sFinal []string | ||
const MIN_LEVENSHTEIN = 3 | ||
|
||
var options levenshtein.Options = levenshtein.Options{ | ||
InsCost: 1, | ||
DelCost: 3, | ||
SubCost: 2, | ||
Matches: func(sourceCharacter rune, targetCharacter rune) bool { | ||
return sourceCharacter == targetCharacter | ||
}, | ||
} | ||
|
||
// Start with a simple strings.Contains check | ||
for name, _ := range root.Subcommands { | ||
if strings.Contains(arg, name) { | ||
suggestions = append(suggestions, name) | ||
} | ||
} | ||
|
||
// If the string compare returns a match, return | ||
if len(suggestions) > 0 { | ||
return suggestions | ||
} | ||
|
||
for name, _ := range root.Subcommands { | ||
lev := levenshtein.DistanceForStrings([]rune(arg), []rune(name), options) | ||
if lev <= MIN_LEVENSHTEIN { | ||
sortableSuggestions = append(sortableSuggestions, &suggestion{name, lev}) | ||
} | ||
} | ||
sort.Sort(sortableSuggestions) | ||
|
||
for _, j := range sortableSuggestions { | ||
sFinal = append(sFinal, j.cmd) | ||
} | ||
return sFinal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters