Examples

Introductory code examples.

Simple App

A bare-bones application demonstrating command and filename completion:

"""Complete system commands and filenames on the same line."""

import os
import kmd

from kmd.completions.command import CommandCompletion
from kmd.completions.filename import FilenameCompletion


class SimpleApp(kmd.Kmd):

    intro = 'Completion example (type Ctrl+D to exit)\n'
    prompt = 'simpleapp> '

    def preloop(self):
        """Set up completions."""
        super(SimpleApp, self).preloop()
        self.completecommand = CommandCompletion()
        self.completefilename = FilenameCompletion()

    def emptyline(self):
        """Do nothing."""

    def do_EOF(self, args):
        """Usage: Ctrl+D"""
        self.stdout.write('\n')
        return True

    def do_shell(self, args):
        """Usage: !<command> [<filename> ...]"""
        os.system(args)

    def complete_shell(self, text, line, begidx, endidx):
        """Complete shell commands.

        This function is called when the command line starts with
        an exclamation mark. It further dispatches to command or
        filename completion, depending on position and format of
        the completion word.
        """
        if line[0:begidx].strip() in ('!', 'shell'):
            if not text.startswith('~') and os.sep not in text:
                return self.completecommand(text)
        return self.completefilename(text)


def main():
    SimpleApp().run()


if __name__ == '__main__':
    main()

Custom Completion

An implementation of environment variable completion:

"""Environment variable completion."""

import os

from rl import completer


class EnvironmentCompletion(object):
    """Complete names of variables in the process environment."""

    def __init__(self):
        """Configure the readline completer.
        """
        if '$' not in completer.word_break_characters:
            completer.word_break_characters += '$'
        if '$' not in completer.special_prefixes:
            completer.special_prefixes += '$'

    def __call__(self, text):
        """Return environment variables matching 'text'.

        Variable names are returned with a leading '$' character.
        The search string may start with a '$' character which is
        stripped before matching.
        """
        if text[0] == '$':
            text = text[1:]
        return ['$'+x for x in os.environ if x.startswith(text)]

Table Of Contents

Previous topic

Constants