public
Created

A version of Ubuntu's command-not-found script modified with an option to show all packages identified as possible misspellings. (by default these are only shown if there are <= 15)

  • Download Gist
command-not-found
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#!/usr/bin/python3
# (c) Zygmunt Krynicki 2005, 2006, 2007, 2008
# Licensed under GPL, see COPYING for the whole text
 
from __future__ import absolute_import, print_function
 
 
__version__ = "0.3"
BUG_REPORT_URL = "https://bugs.launchpad.net/command-not-found/+filebug"
 
try:
import sys
if sys.path and sys.path[0] == '/usr/lib':
# Avoid ImportError noise due to odd installation location.
sys.path.pop(0)
if sys.version < '3':
# We might end up being executed with Python 2 due to an old
# /etc/bash.bashrc.
import os
if "COMMAND_NOT_FOUND_FORCE_PYTHON2" not in os.environ:
os.execvp("python3", [sys.argv[0]] + sys.argv)
 
import gettext
import locale
from optparse import OptionParser
 
from CommandNotFound.util import crash_guard
from CommandNotFound import CommandNotFound
except KeyboardInterrupt:
import sys
sys.exit(127)
 
 
def enable_i18n():
cnf = gettext.translation("command-not-found", fallback=True)
kwargs = {}
if sys.version < '3':
kwargs["unicode"] = True
cnf.install(**kwargs)
locale.setlocale(locale.LC_ALL, '')
 
 
def fix_sys_argv(encoding=None):
"""
Fix sys.argv to have only unicode strings, not binary strings.
This is required by various places where such argument might be
automatically coerced to unicode string for formatting
"""
if encoding is None:
encoding = locale.getpreferredencoding()
sys.argv = [arg.decode(encoding) for arg in sys.argv]
 
 
class LocaleOptionParser(OptionParser):
"""
OptionParser is broken as its implementation of _get_encoding() uses
sys.getdefaultencoding() which is ascii, what it should be using is
locale.getpreferredencoding() which returns value based on LC_CTYPE (most
likely) and allows for UTF-8 encoding to be used.
"""
def _get_encoding(self, file):
encoding = getattr(file, "encoding", None)
if not encoding:
encoding = locale.getpreferredencoding()
return encoding
 
 
def main():
enable_i18n()
if sys.version < '3':
fix_sys_argv()
parser = LocaleOptionParser(
version=__version__,
usage=_("%prog [options] <command-name>"))
parser.add_option('-d', '--data-dir', action='store',
default="/usr/share/command-not-found",
help=_("use this path to locate data fields"))
parser.add_option('--ignore-installed', '--ignore-installed',
action='store_true', default=False,
help=_("ignore local binaries and display the available packages"))
parser.add_option('--no-failure-msg',
action='store_true', default=False,
help=_("don't print '<command-name>: command not found'"))
parser.add_option('-n', '--num-matches', type="int", action='store',
default=15, help=_("maximum number of suggested spellings to print"))
parser.add_option('--all', action='store_const', const=99999, dest='num_matches',
help=_("print all suggested spellings"))
(options, args) = parser.parse_args()
 
class CNFMoreSpellingPrinting(CommandNotFound):
def print_spelling_suggestion(self, word, min_len=3, max_len=options.num_matches):
return CommandNotFound.print_spelling_suggestion(self, word, min_len, max_len)
 
if len(args) == 1:
cnf = CNFMoreSpellingPrinting(options.data_dir)
if not cnf.advise(args[0], options.ignore_installed) and not options.no_failure_msg:
print(_("%s: command not found") % args[0], file=sys.stderr)
 
if __name__ == "__main__":
crash_guard(main, BUG_REPORT_URL, __version__)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.