Is there a way to easily do word suggestions in a CLI?

Viewed 26

I am wondering if there is a package available to help create something similar to what git has built in when you misspell a command, and it suggests existing commands. I am currently writing a CLI, and one of the features of the CLI is dynamically defining commands to dispatch here is the link for context, so misspellings are quite common.

Ideally I am looking for a way to pass a word, with a list of target words and get suggestions based on how close the input word is to one of the target words.

So for example:

def suggest(input_word:str, valid_words:list) -> str:
  suggestion = "" # The word in list valid_words that input_word is closest to 
  ... # Do stuff
  return suggestion

Which can be used like so:

suggest("biild", ["build", "init", "preview"]) # Returns "build"

Then with the cli I can use the function to print a message if a command does not exist and is likely misspelled like:

$ ahd biild
Command "biild" does not exist, did you mean:
    
    ahd build

I am not even looking for a full implementation, even just a suggestion on libraries that could help to do the string similarity check would be appreciated. Everything I have found so far is only a CLI with no API to hook into.

0 Answers
Related