Registry / http-networking / cmd2
library4.2.2pypypi✓ verified 28d ago

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.

pip install cmd2
INSTALL
IMPORT
SIG · CMD2
C
cmd2
http-networkingpythonv4.2.2
Install
3.1s avg
Import
820ms
Disk
36MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v3.5.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.842s · 31.8MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 3.1s · import 0.798s · 32MB
36MB installed
● package 36MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Cmd
✓ from cmd2 import Cmd
Statement
✓ from cmd2 import Statement
Commonly used for type hinting command arguments, especially when not using @with_argparser.

This minimal example demonstrates how to create a basic `cmd2` application by subclassing `cmd2.Cmd` and defining a `do_` method for a command. Running `cmdloop()` starts the interactive shell, and you can type `hello_world` or `quit` to exit.

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())
cmd2 --version
Debug
Known issues
breakingMajor 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.
fix
Review the official 'Migration Guide' for upgrading from 2.x to 3.x in the cmd2 documentation. Adapt custom completer/choices_provider functions to return the new object types.
affects: 3.x and higher
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+.
fix
Ensure your project uses Python 3.10 or higher to be compatible with current `cmd2` versions.
affects: 2.6.0+, 3.x+
breakingThe `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.
fix
Avoid using `Cmd.cmdqueue`. `cmd2` provides alternative mechanisms for running commands via `cmdloop()`, text scripts, Python scripts, and history replays, which offer more consistent behavior.
affects: 0.9.13+
gotchaUnlike 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`.
fix
If you require custom behavior for empty lines, you will need to implement a post-parsing hook or similar mechanism to detect and handle them manually.
affects: All versions
deprecatedUsing `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.
fix
Migrate from `py run()` to the `pyscript` command for executing Python scripts within your `cmd2` application.
affects: Versions where `pyscript` is available
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'cmd2'
The 'cmd2' library is not installed in the Python environment, or the environment where it's installed is not active.
fix
Run `pip install cmd2` in your terminal to install the library.
AttributeError: class Cmd has no attribute 'path_complete'
This error often occurs when trying to access a method or attribute that has been changed, deprecated, or is not directly part of the `cmd2.Cmd` class in the version being used. Specifically, `path_complete` might have been used differently in older versions or for custom completers.
fix
Ensure the method or attribute you are trying to access exists in your `cmd2` version. For path completion with `argparse` decorators, utilize the `choices_provider` parameter of `parser.add_argument()` and pass `cmd2.Cmd.path_complete` to it.
TypeError: onecmd() got an unexpected keyword argument 'add_to_history'
This is a compatibility issue that arose in `cmd2` version 0.9.15, where the `add_to_history` argument to `onecmd()` was changed to be keyword-only, or the method itself was not intended for direct use with that parameter in that version.
fix
When programmatically executing commands and needing to control history, use `app.onecmd_plus_hooks(command_string, add_to_history=True)` instead of directly calling `onecmd()`.
SystemExit
When using `cmd2.with_argparser`, if `argparse` fails to parse command arguments (e.g., due to missing required arguments or invalid input), it raises a `SystemExit` exception, which can prematurely terminate the `cmd2` application.
fix
To gracefully handle `argparse` errors within `cmd2` commands, catch `SystemExit` and re-raise `cmd2.exceptions.Cmd2ArgparseError` within your command method. This allows `cmd2` to display the error message without exiting the application.
Upgrade
Version history
4.2.2latest on PyPI · released Aug 25, 2026
Audit
Dependencies
prompt-toolkitrequiredProvides cross-platform tab-completion, user terminal input, and history, replacing older readline dependencies.
richrequiredUsed for rich text and pretty formatting in the terminal.
Agent activity
26 hits · last 30 days
node
24
OpenAI (training)
1
Resources
cmd2 — pip install cmd2 · libregistry