An interpreter framework
kmd allows to easily build command line driven shells with powerful tab-completion capabilities.
The kmd.Kmd class derives from cmd.Cmd and modifies it in the following ways:
- Instead of Python’s readline module, kmd.Kmd uses the alternative rl readline bindings.
- Setup and tear-down of the readline completer have been moved to preloop and postloop respectively. Subclasses must make sure to call their parents’ implementations.
- Incomplete command names are automatically expanded if they are unique.
- Command aliases can be defined by extending the aliases dictionary. Alias names apply to all do_, complete_, and help_ attributes.
- Lines starting with ‘#’ are treated as comments. The new comment method is invoked, receiving the line as argument. By default this method clears the lastcmd.
- It is now possible to configure the shell_escape_chars. The default is ‘!’.
- If a history_file is set, kmd.Kmd loads and saves the history in preloop and postloop. The history size is controlled by setting history_max_entries.
- The new run method encapsulates the full execution cycle of a Kmd.
- Implements the mechanics of a command shell, similar to cmd.Cmd.
- Implements a set of ready-to-use completions.
- Complete names of files and directories on the filesystem. This is the real deal, thanks to rl providing access to all the necessary readline features. Includes full quoting support as well as support for decomposed UTF-8 on HFS Plus!
- Complete names of users known to the system.
- Complete names of hosts in the system’s /etc/hosts file.
- Complete names of variables in the process environment.
- Complete names of executables on the system PATH.
import os import kmd from kmd.completions.filename import FilenameCompletion from kmd.completions.environment import EnvironmentCompletion class MyShell(kmd.Kmd): def preloop(self): super(MyShell, self).preloop() self.completefilename = FilenameCompletion() self.completeenviron = EnvironmentCompletion() def do_cat(self, args): os.system('cat ' + args) def complete_cat(self, text, *ignored): return self.completefilename(text) def do_echo(self, args): os.system('echo ' + args) def complete_echo(self, text, *ignored): return self.completeenviron(text) def main(): MyShell().run()
Installation requires Python 2.5 or higher.
To install the kmd package, type:
2.2 - 2012-05-10
- String and filename quoting was not respected when Kmd.run was called with arguments. [stefan]
- Switch to a happier looking Sphinx theme. [stefan]
- Require rl >= 2.2. [stefan]
2.1.2 - 2012-04-14
- Update documentation more. [stefan]
2.1.1 - 2012-03-29
- Update documentation. [stefan]
2.1 - 2011-11-05
- Add Kmd.input method as extension point for subclasses. [stefan]
- Make sure hostname completion survives a missing hosts file. [stefan]
- Require rl >= 2.0.1. [stefan]
2.0 - 2011-10-06
- Add Kmd.aliases dictionary to define command aliases. [stefan]
- Refactor Kmd.do_help for easier customization. [stefan]
- Make sure error messages go to stderr. [stefan]
- Switch to pretty Sphinx-based docs. [stefan]
- Require rl >= 2.0. [stefan]
1.0 - 2011-07-14
- Initial release.