cmd is the Python Standard Library’s module for creating simple interactive command-line applications. cmd is an extremely bare-bones framework which leaves a lot to be desired. It doesn’t even include a built-in way to exit from an application!
Since the API provided by cmd provides the foundation on which
based, understanding the use of cmd is the first step in learning the use of
cmd2. Once you have read the cmd docs, return here to learn the ways that
cmd2 differs from cmd.
cmd2 is a batteries-included extension of cmd, which provides a wealth of
functionality to make it quicker and easier for developers to create
feature-rich interactive command-line applications which delight customers.
cmd2 can be used as a drop-in replacement for cmd with a few minor
discrepancies as discussed in the
Incompatibilities section. Simply importing
cmd2 in place of cmd will add many features to an application without any
further modifications. Migrating to
cmd2 will also open many additional
doors for making it possible for developers to provide a top-notch interactive
command-line experience for their users.
After switching from cmd to
cmd2, your application will have the following
new features and capabilities, without you having to do anything:
- More robust History. Both cmd and
cmd2have readline history, but
cmd2also has a robust
historycommand which allows you to edit prior commands in a text editor of your choosing, re-run multiple commands at a time, and save prior commands as a script to be executed later.
- Users can redirect output to a file or pipe it to some other operating system
command. You did remember to use
sys.stdoutin all of your print functions, right? If you did, then this will work out of the box. If you didn’t, you’ll have to go back and fix them. Before you do, you might consider the various ways
cmd2has of Generating Output.
- Users can load script files, which contain a series of commands to be executed.
- Users can create Shortcuts, Aliases, and Macros to reduce the typing required for repetitive commands.
- Embedded python shell allows a user to execute python code from within your
cmd2app. How meta.
- Clipboard Integration allows you to save command output to the operating system clipboard.
- A built-in Timer can show how long it takes a command to execute
- A Transcript is a file which
contains both the input and output of a successful session of a
cmd2-based app. The transcript can be played back into the app as a unit test.