Skip to content
Permalink
Browse files

misspelled command suggestions

  • Loading branch information
ralsina committed Jun 10, 2015
1 parent f33f735 commit bbfeacb7d527056b3fbbd3ec97d7821c0d8ac9ca
Showing with 15 additions and 1 deletion.
  1. +1 −0 CHANGES.txt
  2. +14 −1 nikola/__main__.py
@@ -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)
@@ -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:
@@ -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.