cmd2 - Feature-Rich Interactive Command Line Applications
cmd2 is a powerful Python library for quickly building feature-rich and user-friendly interactive command-line applications. It extends Python's built-in `cmd` module, providing a comprehensive set of enhancements including robust tab completion, searchable command history, text and Python scripting, and advanced argument parsing with `argparse`. The library is actively maintained with a frequent release cadence, continuously improving its capabilities and user experience.
Warnings
- breaking Major breaking changes occurred in `cmd2` version 3.x (from 2.x). Key changes include a new dependency on `prompt-toolkit` (replacing all `readline` usage) and `rich` for formatting. The Transcript Testing feature was removed, and `completer` and `choices_provider` functions now return `cmd2.Completions` and `cmd2.Choices` objects instead of lists of strings.
- breaking `cmd2` dropped support for Python 3.8 in version 2.6.0, and Python 3.5 support ended in version 2.0.0. Current versions require Python 3.10+.
- breaking The `Cmd.cmdqueue` attribute, which was used in Python's built-in `cmd` module for queued input lines, was removed in `cmd2` version 0.9.13.
- gotcha Unlike Python's standard `cmd` module, `cmd2` completely ignores empty lines entered at the prompt by default. The `cmd.emptyline()` method (which, by default, repeats the last command) is never called in `cmd2`.
- deprecated Using `py run('myscript.py')` to execute Python scripts is deprecated. The `pyscript` command is now the recommended method, offering improved features like tab-completion of file system paths and the ability to pass command-line arguments.
Install
-
pip install cmd2
Imports
- Cmd
from cmd2 import Cmd
- Statement
from cmd2 import Statement
Quickstart
import cmd2
import sys
class FirstApp(cmd2.Cmd):
"""A simple cmd2 application."""
def do_hello_world(self, _: cmd2.Statement):
"""Says hello to the world."""
self.poutput('Hello World')
if __name__ == '__main__':
c = FirstApp()
sys.exit(c.cmdloop())