cmd2

A python package for building powerful command-line interpreter (CLI) programs. Extends the Python Standard Library’s cmd package.

The basic use of cmd2 is identical to that of cmd.

  1. Create a subclass of cmd2.Cmd. Define attributes and do_* methods to control its behavior. Throughout this documentation, we will assume that you are naming your subclass App:

    from cmd2 import Cmd
    class App(Cmd):
        # customized attributes and methods here
    
  2. Instantiate App and start the command loop:

    app = App()
    app.cmdloop()
    

Note

The tab-completion feature provided by cmd relies on underlying capability provided by GNU readline or an equivalent library. Linux distros will almost always come with the required library installed. For macOS, we recommend using the gnureadline Python module which includes a statically linked version of GNU readline. Alternatively on macOS the conda package manager that comes with the Anaconda Python distro can be used to install readline (preferably from conda-forge) or the Homebrew package manager can be used to to install the readline package. For Windows, we recommend installing the pyreadline Python module.

Resources

These docs will refer to App as your cmd2.Cmd subclass, and app as an instance of App. Of course, in your program, you may name them whatever you want.

Contents:

Compatibility

Tested and working with Python 3.4+ on Windows, macOS, and Linux.

Indices and tables