Skip to main content

asiocmd: Modern cmd with async support

Overview

asiocmd provides a very lightweight repacking of Python's cmd.Cmd class for building command line interfaces. The package contains 2 classes, namely Cmd and an inherited AsyncCmd

Cmd

Cmd provides virtually the same functionality and development interface as cmd.Cmd, with the primary difference being in the way methods are looked up at runtime.

AsyncCmd

An implementation of Cmd with added support for asynchronous methods

Usage

from asiocmd import (AsyncCmd,
                  command, command_helper,
                  async_command, async_command_helper)

class DemoCmd(AsyncCmd):
    # Instance methods decorated with @command are registered as CLI commands

    # Command names can be specified as the decorator argument
    @command("foo")
    def arbitrary_name(self, line: str) -> None: ...

    # If the name argument is not provided,
    # the function name is taken as command name
    @command
    def bar(self, line: str) -> None: ...

    # Legacy support for cmd.Cmd's naming convention of do_*
    def do_xyz(self, line: str) -> None: ...

    # Helpers can be registered with @command_helper(<command_name>)
    @command_helper("foo")
    def arbitrary_helper(self) -> None: ...

    # Again, cmd.Cmd's helper naming convention is still supported
    def help_bar(self) -> None: ...

    # Asynchronous methods and helpers follow
    # the same convention, just with different decorators

    @async_command("afoo")
    async def async_name(self, line: str) -> None: ...

    @async_command
    async def abar(self, line: str) -> None: ...

    async def do_abc(self, line: str) -> None: ...

    @async_command_helper("afoo")
    async def afoo_helper(self) -> None: ...

# Launching the CLI
if __name__ == '__main__':
    asyncio.run(DemoCmd().acmdloop())

Note: Cmd uses cmdloop() to launch itself, the coroutine acmdloop belongs only to AsyncCmd to provide support for asynchronous methods.

Stacking of decorators are also supported, given that function metadata is preserved using functools.wraps

from asiocmd import (AsyncCmd, command)
import functools

def generic_method_decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # Some working
        return func(*args, **kwargs)
    return wrapper

class DemoCmd(AsyncCmd):
    @generic_method_decorator
    @command_helper("foo")
    def abc(self) -> None: pass

    @generic_method_decorator
    def do_bar(self, line: str) -> None: pass

Metadata

Release files for asiocmd 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asiocmd 1.0.0
File Size Uploaded
asiocmd-1.0.0.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asiocmd 1.0.0
File Interpreter ABI Platform
asiocmd-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.0 kB

Release files / asiocmd-1.0.0.tar.gz

Download URL asiocmd-1.0.0.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
50f4303f40f85103302d9f6d08031e7ca1f747ea1025b6bead45142f66d2cf20
BLAKE2b-256 checksum
How to use checksums
63b3044dee64344845a6f69783b5c0c779872cfbe2aa002345b8afcd3fb49260
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / asiocmd-1.0.0-py3-none-any.whl

Download URL asiocmd-1.0.0-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
593f88020f09ffc85fef241659747696076ce27c950049fa5afb3f2bd8292cb3
BLAKE2b-256 checksum
How to use checksums
ad6493b60af24f8ee124424cee96cc9caf8f9bf98eb447a721244a325021b2c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release 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