{"id":3431,"library":"cmd2","title":"cmd2 - Feature-Rich Interactive Command Line Applications","description":"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.","status":"active","version":"3.4.0","language":"python","source_language":"en","source_url":"https://github.com/python-cmd2/cmd2","tags":["CLI","REPL","command-line interface","interactive","argparse","shell"],"install":[{"cmd":"pip install cmd2","lang":"bash","label":"Install latest stable version"}],"dependencies":[{"reason":"Provides cross-platform tab-completion, user terminal input, and history, replacing older readline dependencies.","package":"prompt-toolkit"},{"reason":"Used for rich text and pretty formatting in the terminal.","package":"rich"}],"imports":[{"symbol":"Cmd","correct":"from cmd2 import Cmd"},{"note":"Commonly used for type hinting command arguments, especially when not using @with_argparser.","symbol":"Statement","correct":"from cmd2 import Statement"}],"quickstart":{"code":"import cmd2\nimport sys\n\nclass FirstApp(cmd2.Cmd):\n    \"\"\"A simple cmd2 application.\"\"\"\n\n    def do_hello_world(self, _: cmd2.Statement):\n        \"\"\"Says hello to the world.\"\"\"\n        self.poutput('Hello World')\n\nif __name__ == '__main__':\n    c = FirstApp()\n    sys.exit(c.cmdloop())","lang":"python","description":"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."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":"3.x and higher"},{"fix":"Ensure your project uses Python 3.10 or higher to be compatible with current `cmd2` versions.","message":"`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+.","severity":"breaking","affected_versions":"2.6.0+, 3.x+"},{"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.","message":"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.","severity":"breaking","affected_versions":"0.9.13+"},{"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.","message":"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`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Migrate from `py run()` to the `pyscript` command for executing Python scripts within your `cmd2` application.","message":"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.","severity":"deprecated","affected_versions":"Versions where `pyscript` is available"}],"env_vars":null,"search_vec":"'activ':65 'advanc':57 'applic':8,29 'argpars':61,86 'argument':58 'build':17 'built':35 'built-in':34 'cadenc':71 'capabl':75 'cli':79 'cmd':37 'cmd2':1,9 'command':6,27,50,82 'command-lin':26,81 'complet':48 'comprehens':41 'continu':72 'enhanc':44 'experi':78 'extend':31 'featur':3,19 'feature-rich':2,18 'frequent':69 'friend':24 'histori':51 'improv':73 'includ':45 'interact':5,25,85 'interfac':84 'librari':14,63 'line':7,28,83 'maintain':66 'modul':38 'pars':59 'power':12 'provid':39 'python':13,32,54 'quick':16 'releas':70 'repl':80 'rich':4,20 'robust':46 'script':55 'searchabl':49 'set':42 'shell':87 'tab':47 'text':52 'user':23,77 'user-friend':22","created_at":"2026-04-11T17:28:49.331434+00:00","updated_at":"2026-04-16T02:49:50.571621+00:00","problems":[{"fix":"Run `pip install cmd2` in your terminal to install the library.","cause":"The 'cmd2' library is not installed in the Python environment, or the environment where it's installed is not active.","error":"ModuleNotFoundError: No module named 'cmd2'"},{"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.","cause":"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.","error":"AttributeError: class Cmd has no attribute 'path_complete'"},{"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()`.","cause":"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.","error":"TypeError: onecmd() got an unexpected keyword argument 'add_to_history'"},{"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.","cause":"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.","error":"SystemExit"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"4.2.2","cli_name":"cmd2","cli_version":"sh: 1: cmd2: not found","type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/cmd2/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}