Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
misspelled command suggestions
  • Loading branch information
ralsina committed Jun 10, 2015
1 parent f33f735 commit bbfeacb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ New in master
Features
--------

* Suggest misspelled commands (Issue #1807)
* New implementation of the ``nikola auto`` command.
* ``requests`` is now required for all Nikola sites
* New ``nikola version --check`` option (Issue #1767)
Expand Down
15 changes: 14 additions & 1 deletion nikola/__main__.py
Expand Up @@ -25,6 +25,7 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import print_function, unicode_literals
from collections import defaultdict
import os
import shutil
try:
Expand Down Expand Up @@ -316,18 +317,30 @@ def run(self, cmd_args):
args = ['version']
if args[0] not in sub_cmds.keys():
LOGGER.error("Unknown command {0}".format(args[0]))
sugg = defaultdict(list)
for c in sub_cmds.keys():
d = lev(c, args[0])
sugg[d].append(c)
LOGGER.info('Maybe you mean "{}"?', '" or "'.join(sugg[min(sugg.keys())]))
return 3
if sub_cmds[args[0]] is not Help and not isinstance(sub_cmds[args[0]], Command): # Is a doit command
if not self.nikola.configured:
LOGGER.error("This command needs to run inside an "
"existing Nikola site.")
return 3

return super(DoitNikola, self).run(cmd_args)

@staticmethod
def print_version():
print("Nikola v" + __version__)


# Stolen from http://stackoverflow.com/questions/4173579/implementing-levenshtein-distance-in-python
def lev(a, b):
if not a or not b:
return max(len(a), len(b))
return min(lev(a[1:], b[1:]) + (a[0] != b[0]), lev(a[1:], b) + 1, lev(a, b[1:]) + 1)


if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))

0 comments on commit bbfeacb

Please sign in to comment.