Interpreters

A base class for custom command interpreters.

Kmd Class

class kmd.Kmd(completekey='TAB', stdin=None, stdout=None, stderr=None)

Interpreter base class.

This is a subclass of the standard library’s cmd.Cmd class, using the new rl bindings for GNU Readline. The standard library documentation applies unless noted otherwise. Changes include:

  1. Kmd is a new-style class.
  2. The Kmd constructor accepts an additional ‘stderr’ argument; all error messages are printed to ‘stderr’.
  3. preloop() and postloop() are no longer stubs but contain important code bits. Subclasses must make sure to call their parents’ implementations.
  4. New methods: input(), comment(), help(), run(), and word_break_hook().
  5. Incomplete command names are automatically expanded if they are unique.
  6. Command aliases can be defined by overriding __init__() and extending the ‘aliases’ dictionary.
  7. help_*() methods optionally receive the help topic as argument.
  8. complete_*() methods may return any kind of iterable, not just lists.

Example:

import kmd

class MyShell(kmd.Kmd):
    ...
Kmd.alias_header = 'Command aliases (type help <topic>):'

Header for the aliases section of the default help screen. If set to the empty string, the aliases section is omitted.

Kmd.shell_escape_chars = '!'

Special, single-character aliases for do_shell().

Kmd.history_file = ''

If a history file is set, Kmd loads and saves the history in preloop() and postloop().

Kmd.history_max_entries = -1

A non-negative value limits the history size.

Kmd.cmdloop(intro=None)

Repeatedly issue a prompt, accept input, parse an initial prefix off the received input, and dispatch to action methods, passing them the remainder of the line as argument.

Kmd.preloop()

Called when the cmdloop() method is entered. Configures the readline completer and loads the history file.

Kmd.postloop()

Called when the cmdloop() method is exited. Resets the readline completer and saves the history file. Note that postloop() is called even if cmdloop() exits with an exception!

Kmd.input(prompt)

Read a line from the keyboard using raw_input() (input() in Python 3). When the user presses the TAB key, invoke the readline completer.

Kmd.complete(text, state)

Return the next possible completion for ‘text’.

If a command has not been entered, complete against the command list. Otherwise try to call complete_<command> to get a list of completions. Installed as rl.completer.completer.

Kmd.word_break_hook(begidx, endidx)

When completing ‘?<topic>’ make sure ‘?’ is a word break character. Ditto for ‘!<command>’. Installed as rl.completer.word_break_hook.

Kmd.onecmd(line)

Interpret a command line.

This may be overridden, but should not normally need to be; see the precmd() and postcmd() methods for useful execution hooks. The return value is a flag indicating whether interpretation of commands by the interpreter should stop.

If there is a do_<command> method for the command prefix, that method is called, with the remainder of the line as argument, and its return value is returned. Otherwise the return value of the default() method is returned.

Kmd.parseline(line)

Parse the line into a command name and a string containing the arguments. Returns a tuple containing (command, args, line). ‘command’ and ‘args’ may be None if the line couldn’t be parsed.

Kmd.comment(line)

Called when the input line starts with a ‘#’. By default clears the lastcmd.

Kmd.default(line)

Called when the command prefix is not recognized. By default prints an error message.

Kmd.do_help(topic='')

Print the help screen for ‘topic’.

Call help_<topic> if it exists. Otherwise, and if ‘topic’ is a command, print do_<command>’s docstring. If ‘topic’ is empty, print the default help screen.

Kmd.help()

Called when no help topic is specified. Prints the default help screen; empty sections and sections with empty headers are omitted.

Kmd.run(args=None)

Run the Kmd.

If ‘args’ is None, it defaults to sys.argv[1:].

Table Of Contents

Previous topic

kmd 2.2 – Interpreter Framework

Next topic

Completions