Skip to main content

cmd2: build feature-rich command-line applications in Python

Latest Version Tests codecov Documentation Status Chat

Quick startWhy cmd2?InstallationDocumentationProjects using cmd2

cmd2 makes it quick to build powerful, polished command-line applications and REPLs in Python. Start with a subclass of the standard library's cmd.Cmd, then add commands while cmd2 handles the details that make an application pleasant to build and use: argument parsing, generated help, tab completion, command history, scripting, output styling, and much more.

You can use cmd2 for a small internal tool and grow the same application into an extensible, scriptable interface without replacing the framework or writing the usual CLI boilerplate.

Quick start

Install cmd2:

pip install cmd2

Then create app.py:

from typing import Annotated

import cmd2
from cmd2.annotated import Argument, Option


class App(cmd2.Cmd):
    """A small interactive application."""

    @cmd2.with_annotated
    def do_greet(
        self,
        name: Annotated[str, Argument(help_text="person to greet")],
        count: Annotated[int, Option(help_text="number of greetings")] = 1,
    ) -> None:
        """Greet a person."""
        for _ in range(count):
            self.poutput(f"Hello, {name}!")


if __name__ == "__main__":
    App().cmdloop()

cmd2 supports Typer-style syntax for specifying command arguments with type annotations. In this example, @cmd2.with_annotated turns name and count into a positional argument and an option.

Run it with python app.py. Your new greet command already has input validation, generated help, and tab completion for its options. The application also includes discoverable help, command history, aliases, macros, scripting, shell integration, and other built-in commands.

(Cmd) help greet
Usage: greet [-h] [--count COUNT] name

(Cmd) greet --count 2 Ada
Hello, Ada!
Hello, Ada!

Animated demonstration of the cmd2 quick-start application

See the getting started tutorial to build a more complete application.

Why cmd2?

Build more with less code

  • Define commands as Python methods and use familiar argparse parsers for arguments and subcommands. A single parser definition drives validation, help, and completion.
  • Render Rich tables and other styled objects, with consistent helpers for normal output, errors, warnings, and paging.
  • Organize large applications into independently testable and dynamically loadable CommandSets, and customize the command lifecycle with hooks.
  • Integrate existing Python code, shell tools, or asynchronous work instead of restructuring your application around the framework.
  • Run on Windows, macOS, and Linux with a pure-Python package and a small dependency footprint.

Give users a capable interface from day one

  • Tab completion: provide context-aware completion for commands, subcommands, options, choices, file paths, and custom data sources, with descriptive hints for completion candidates. Learn more about completion.
  • History: search, edit, rerun, save, and optionally persist previously entered commands. Users also get familiar navigation and reverse search such as Ctrl+R. Learn more about history.
  • Unicode: accept Unicode in commands, arguments, file names, and output, and run UTF-8 command scripts for applications used in any language. Learn more about command scripts.
  • Shell shortcuts and hotkeys: use familiar Readline-style keyboard shortcuts for navigating and editing the command line, including Emacs-style bindings provided by prompt-toolkit. Learn more about keyboard shortcuts.
  • Help: generate discoverable, categorized help for commands, subcommands, and arguments directly from their argument parsers. Learn more about help.
  • Rich UI/UX: offer syntax highlighting, Fish-style history suggestions, multiline input, configurable prompts, completion menus, themes, and an optional bottom toolbar. Learn more about prompts and toolbars and themes.

Grow from exploration to automation

  • Shell scripting: automate the same commands users run interactively by passing them as command arguments or standard input from shell scripts. Learn more about automating cmd2 applications.
  • Python scripting: run Python scripts inside the application for loops, branching, complex control flow, and integration with the application's commands and data. Learn more about Python scripts.
  • Run shell commands: execute operating-system commands without leaving the application, using the built-in shell command or its ! shortcut. Learn more about OS integration.
  • Redirect output: send command output to files or the clipboard, or pipe it through one or more shell commands. Learn more about output redirection and pipes.
  • Aliases, macros, and shortcuts: let users customize names, parameterized commands, and terse shortcuts for repetitive workflows without changing the application. Learn more about aliases, macros, and shortcuts.
  • Startup scripts: initialize an application consistently by running saved commands every time it starts. Learn more about startup commands and scripts.
  • Embedded Python and IPython shells: drop into an interactive Python or IPython session for experimentation, debugging, object introspection, and access to application state. Learn more about embedded Python shells.
  • Color, style, and tables: produce readable output with Rich colors and styles, custom themes, paging, and flexible table layouts. Learn more about generating output and creating tables.

Installation

On all operating systems, the latest stable version of cmd2 can be installed using pip:

pip install -U cmd2

cmd2 works with Python 3.11+ on Windows, macOS, and Linux. It is pure Python code with few 3rd-party dependencies. It works with both conventional CPython and free-threaded variants.

For information on other installation options, see Installation Instructions in the cmd2 documentation.

[!IMPORTANT] Upgrading from an older release? Versions 3.x and 4.x include significant backwards-incompatible changes. Review the changelog and migration guide before upgrading.

Documentation

Read the latest documentation online or download it in HTML, PDF, and ePub formats. The examples directory contains focused, runnable demonstrations of individual features.

Tutorials

Found a bug?

If you think you've found a bug, please first read through the open Issues. If you're confident it's a new bug, go ahead and create a new GitHub issue. Be sure to include as much information as possible so we can reproduce the bug. At a minimum, please state the following:

  • cmd2 version
  • Python version
  • OS name and version
  • What you did to cause the bug to occur
  • Include any traceback or error message associated with the bug

Projects using cmd2

Application Name Description Organization or Author
CephFS Shell The Ceph File System, or CephFS, is a POSIX-compliant file system built on top of Ceph’s distributed object store ceph
garak LLM vulnerability scanner that checks if an LLM can be made to fail in a way we don't want NVIDIA
Argus The Ultimate Information Gathering Toolkit JASON13
medusa Binary instrumentation framework that that automates processes for the dynamic analysis of Android and iOS Applications Ch0pin
InternalBlue Bluetooth experimentation framework for Broadcom and Cypress chips Secure Mobile Networking Lab
SCCMHunter A post-ex tool built to streamline identifying, profiling, and attacking SCCM related assets in an Active Directory domain Garret Foster
Unipacker Automatic and platform-independent unpacker for Windows binaries based on emulation unipacker
Frankenstein Broadcom and Cypress firmware emulation for fuzzing and further full-stack debugging Secure Mobile Networking Lab
Poseidon Leverages software-defined networks (SDNs) to acquire and then feed network traffic to a number of machine learning techniques. Faucet SDN
DFTimewolf A framework for orchestrating forensic collection, processing and data export log2timeline
LazyOwn The first RedTeam/APT Framework with an AI-powered C&C, featuring rootkits to conceal campaigns, undetectable malleable implants Grisuno
GAP SDK SDK for Greenwaves Technologies' GAP8 IoT Application Processor GreenWaves Technologies
REW Sploit Emulate and Dissect Metasploit Framework (MSF) and other attacks REW-sploit
tomcatmanager A command line tool and python library for managing a tomcat server tomcatmanager
Falcon Toolkit Unleash the power of the CrowdStrike Falcon Platform at the CLI CrowdStrike
EXPLIoT Internet of Things Security Testing and Exploitation framework expliot_framework
Pobshell A Bash‑like shell for live Python objects: cd, ls, cat, find and CLI piping for object code, str values & more Peter Dalloz

Possibly defunct but still good examples

Application Name Description Organization or Author
Katana Automatic CTF Challenge Solver John Hammond
SatanSword (in Chinese) Comprehensive Penetration Framework for Red Teaming Lucifer1993
Jok3r Network & Web Pentest Automation Framework Koutto
Counterfit a CLI that provides a generic automation layer for assessing the security of ML models Microsoft Azure
Overlord Red Teaming Infrastructure Automation QSecure Labs
Automated Reconnaissance Pipeline An automated target reconnaissance pipeline epi052
JSShell An interactive multi-user web JavaScript (JS) shell Den1al
RedShell An interactive command prompt for red teaming and pentesting Verizon
FLASHMINGO Automatic analysis of SWF files based on some heuristics. Extensible via plugins. Mandiant
psiTurk An open platform for science on Amazon Mechanical Turk NYU Computation and Cognition Lab

Note: If you have created an application based on cmd2 that you would like us to mention here, please get in touch.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cmd2-4.2.3.tar.gz (733.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cmd2-4.2.3-py3-none-any.whl (195.5 kB view details)

Uploaded Python 3

File details

Details for the file cmd2-4.2.3.tar.gz.

File metadata

  • Download URL: cmd2-4.2.3.tar.gz
  • Upload date:
  • Size: 733.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cmd2-4.2.3.tar.gz
Algorithm Hash digest
SHA256 b5543c81e01eea9445f1248879855b727c949d5af27c0390fbb8796f782783e9
MD5 3e6de45a1387fd5643ccbade2179ce5d
BLAKE2b-256 64166864f7f344c0c83fbe78fdaf4c4ea280631ce2ad49efed98c61209106b3a

See more details on using hashes here.

File details

Details for the file cmd2-4.2.3-py3-none-any.whl.

File metadata

  • Download URL: cmd2-4.2.3-py3-none-any.whl
  • Upload date:
  • Size: 195.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cmd2-4.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3ffecdbae14a1288fe7b5455c8481483186b6f394065b98a0e4899b6e2c048a8
MD5 e8cff74ec917cf78600edb54820010cb
BLAKE2b-256 f3dbc4a9d80c3be030903224dde7232c0b471352062e0c72d69ead6e98c243cf

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

4.2.3 This release

2 files

4.2.2

2 files

4.2.1

2 files

4.2.0

2 files

4.1.2

2 files

4.1.1

2 files

4.1.0

2 files

4.0.1

2 files

4.0.0

2 files

3.5.1

2 files

3.5.0

2 files

3.4.0

2 files

3.3.0

2 files

3.2.2

2 files

3.2.1

2 files

3.2.0

2 files

3.1.3

2 files

3.1.2

2 files

3.1.1

2 files

3.1.0

2 files

3.0.0

2 files

2.7.0

2 files

2.6.2

2 files

2.6.1

2 files

2.6.0

2 files

2.5.11

2 files

2.5.10

2 files

2.5.9

2 files

2.5.8

2 files

2.5.7

2 files

2.5.6

2 files

2.5.5

2 files

2.5.4

2 files

2.5.3

2 files

2.5.2

2 files

2.5.1

2 files

2.5.0

2 files

2.4.3

2 files

2.4.2

2 files

2.4.1

2 files

2.4.0

2 files

2.3.3

2 files

2.3.2

2 files

2.3.1

2 files

2.3.0

2 files

2.2.0

2 files

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

2.0.1

2 files

2.0.0

2 files

1.5.0

2 files

1.4.0

2 files

1.3.11

2 files

1.3.10

2 files

1.3.9

2 files

1.3.8

2 files

1.3.7

2 files

1.3.6

2 files

1.3.5

2 files

1.3.4

2 files

1.3.3

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.1

2 files

1.2.0

2 files

1.1.0

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.10.1

2 files

0.10.0

2 files

0.9.25

2 files

0.9.24

2 files

0.9.23

2 files

0.9.22

2 files

0.9.21

2 files

0.9.20

2 files

0.9.19

2 files

0.9.18

2 files

0.9.17

2 files

0.9.16

2 files

0.9.15

2 files

0.9.14

2 files

0.9.13

2 files

0.9.12

2 files

0.9.11

2 files

0.9.10

2 files

0.9.9

2 files

0.9.8

2 files

0.9.7

2 files

0.9.6

2 files

0.9.5

2 files

0.9.4

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0.1

2 files

0.8.9

2 files

0.8.8

2 files

0.8.7

2 files

0.8.6.1

2 files

0.8.6

2 files

0.8.5

2 files

0.8.4

2 files

0.8.3

2 files

0.8.2

1 file

0.8.1

1 file

0.8.0

1 file

0.7.9

1 file

0.7.8

1 file

0.7.7

1 file

0.7.6

1 file

0.7.5

1 file

0.7.4

1 file

0.7.3

1 file

0.7.2

1 file

0.7.1

1 file

0.7.0

1 file

0.6.9

1 file

0.6.8

2 files

0.6.7

3 files

0.6.6.1

2 files

0.6.6

3 files

0.6.5.1

4 files

0.6.5

2 files

0.6.4

2 files

0.6.3

4 files

0.6.2

3 files

0.6.1

4 files

0.6.0

4 files

0.5.5

2 files

0.5.4

4 files

0.5.3

2 files

0.5.2

3 files

0.5.1

3 files

0.5.0

3 files

0.4.8

4 files

0.4.7

4 files

0.4.6

4 files

0.4.5

4 files

0.4.4

5 files

0.4.3

5 files

0.4.2

4 files

0.4.1

5 files

0.4

3 files

0.3.7

2 files

0.3.6

2 files

0.3.5

3 files

0.3.4.1

2 files

0.3.4

4 files

0.3.3

4 files

0.3.2

3 files

0.3.1

3 files

0.3.0

3 files

0.2.3

4 files

0.2.2

4 files

0.2.1

3 files

0.2

3 files

0.1

3 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page