Skip to main content

cmd2 - quickly build feature-rich and user-friendly interactive command line applications in Python

Project description

cmd2 : immersive interactive command line applications

Latest Version GitHub Actions codecov Documentation Status Chat

Developer's ToolboxPhilosophyInstallationDocumentationTutorialsHello WorldProjects using cmd2

Screenshot

cmd2 is a tool for building interactive command line applications in Python. Its goal is to make it quick and easy for developers to build feature-rich and user-friendly interactive command line applications. It provides a simple API which is an extension of Python's built-in cmd module. cmd2 provides a wealth of features on top of cmd to make your life easier and eliminates much of the boilerplate code which would be necessary when using cmd.

The developers toolbox

system schema

When creating solutions developers have no shortage of tools to create rich and smart user interfaces. System administrators have long been duct taping together brittle workflows based on a menagerie of simple command line tools created by strangers on github and the guy down the hall. Unfortunately, when CLIs become significantly complex the ease of command discoverability tends to fade quickly. On the other hand, Web and traditional desktop GUIs are first in class when it comes to easily discovering functionality. The price we pay for beautifully colored displays is complexity required to aggregate disperate applications into larger systems. cmd2 fills the niche between high ease of command discovery applications and smart workflow automation systems.

The cmd2 framework provides a great mixture of both worlds. Application designers can easily create complex applications and rely on the cmd2 library to offer effortless user facing help and extensive tab completion. When users become comfortable with functionality, cmd2 turns into a feature rich library enabling a smooth transition to full automation. If designed with enough forethought, a well implemented cmd2 application can serve as a boutique workflow tool. cmd2 pulls off this flexibility based on two pillars of philosophy:

  • Tab Completion
  • Automation Transition

Philosophy

Deep extensive tab completion and help text generation based on the argparse library create the first pillar of 'ease of command discovery'. The following is a list of features in this category.

  • Great tab completion of commands, subcommands, file system paths, and shell commands.
  • Custom tab completion for user designed commands via simple function overloading.
  • Tab completion from persistent_history_file sources added with very little friction.
  • Automatic tab completion of argparse flags and optional arguments.
  • Path completion easily enabled.
  • When all else fails, custom tab completion based on choices_provider can fill any gaps.

cmd2 creates the second pillar of 'ease of transition to automation' through alias/macro creation, command line argument parsing and execution of cmd2 scripting.

  • Flexible alias and macro creation for quick abstraction of commands.
  • Text file scripting of your application with run_script (@) and _relative_run_script (@@)
  • Powerful and flexible built-in Python scripting of your application using the run_pyscript command
  • Transcripts for use with built-in regression can be automatically generated from history -t or run_script -t

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.8+ on Windows, macOS, and Linux. It is pure Python code with few 3rd-party dependencies.

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

Documentation

The latest documentation for cmd2 can be read online here: https://cmd2.readthedocs.io/en/latest/

It is available in HTML, PDF, and ePub formats.

The best way to learn the cmd2 api is to delve into the example applications located in source under examples.

Tutorials

Hello World

#!/usr/bin/env python
"""A simple cmd2 application."""
import cmd2


class FirstApp(cmd2.Cmd):
    """A simple cmd2 application."""

  def do_hello_world(self, _: cmd2.Statement):
    self.poutput('Hello World')

if __name__ == '__main__':
    import sys
    c = FirstApp()
    sys.exit(c.cmdloop())

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
Jok3r Network & Web Pentest Automation Framework
CephFS Shell Ceph is a distributed object, block, and file storage platform
psiTurk An open platform for science on Amazon Mechanical Turk
Poseidon Leverages software-defined networks (SDNs) to acquire and then feed network traffic to a number of machine learning techniques.
Unipacker Automatic and platform-independent unpacker for Windows binaries based on emulation
tomcatmanager A command line tool and python library for managing a tomcat server
Expliot Internet of Things (IoT) exploitation framework
mptcpanalyzer Tool to help analyze mptcp pcaps
clanvas Command-line client for Canvas by Instructure

Possibly defunct but still good examples

Project details


Release history Release notifications | RSS feed

This version

2.5.4

Download files

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

Source Distribution

cmd2-2.5.4.tar.gz (690.1 kB view details)

Uploaded Source

Built Distribution

cmd2-2.5.4-py3-none-any.whl (151.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cmd2-2.5.4.tar.gz
  • Upload date:
  • Size: 690.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for cmd2-2.5.4.tar.gz
Algorithm Hash digest
SHA256 405016f6cee5d4155534a39c8089ebedcfffbb3e24c37150a2b61df32ec90122
MD5 0aa06d9e33cabc935517689ec41439bc
BLAKE2b-256 46473f621e0ddf0c5a1f69d3a0c8135d1f42fa6ea9da650e6882eaba979be6cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmd2-2.5.4-py3-none-any.whl
  • Upload date:
  • Size: 151.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for cmd2-2.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 287793e494faaa66d0f9e2549e79c1b923209b5a2261013eb282eb3b35d31691
MD5 cd269331d94f0a2c51e1181aadf01598
BLAKE2b-256 7395e24a506a7909f8f6a5daca6f60210a0fc0d460ea786877db43e0d4a38ec5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page