Skip to main content

A modern version of cmd.Cmd using rl readline bindings

Project description

Introduction

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:

  1. Instead of Python’s readline module, kmd.Kmd uses the alternative rl readline bindings.

  2. Setup and tear-down of the readline completer have been moved to preloop and postloop respectively. Subclasses must make sure to call their parent’s implementations.

  3. Command aliases can be configured simply by extending the aliases dict. Alias names apply to all do_, complete_, and help_ attributes.

  4. Incomplete command names are automatically expanded given they are unique.

  5. 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.

  6. It is now possible to configure the shell_escape_chars. The default is ‘!’.

  7. If a history_file is configured, kmd.Kmd loads and saves the history in preloop and postloop; the history size can be limited by setting history_max_entries.

  8. The new run method encapsulates the full execution cycle of a Kmd.

Package Contents

kmd.Kmd

Implements the mechanics of a command shell, similar to cmd.Cmd.

kmd.completions

Implements a set of ready-to-use completions.

Completions

FilenameCompletion

Completes names of files and directories. This is the real deal, thanks to rl providing access to all necessary readline features. Includes full quoting support as well as support for decomposed UTF-8 on HFS Plus.

UsernameCompletion

Completes names of users known to the system.

HostnameCompletion

Completes names of hosts in the system’s /etc/hosts file.

EnvironmentCompletion

Completes names of variables in the process environment.

CommandCompletion

Completes names of executables on the system PATH.

Example Code

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, line, begidx, endidx):
        return self.completefilename(text)

    def do_echo(self, args):
        os.system('echo ' + args)

    def complete_echo(self, text, line, begidx, endidx):
        return self.completeenviron(text)

def main():
    MyShell().run()

For further details, please refer to the API Documentation.

Also see gpgkeys, a front-end for GnuPG built entirely around tab completion.

Repository Access

kmd development is hosted on GitHub.

Installation

Installation requires Python 2.5 or higher.

Note: kmd uses the rl library. Since rl contains a C extension, it is a good idea to review its installation instructions and make sure all dependencies are in place.

To install the kmd package, type:

easy_install kmd

Changelog

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kmd-2.1.zip (34.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page