Modern and asynchronous support for cmd.Cmd
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asiocmd-1.0.0.tar.gz.
File metadata
- Download URL: asiocmd-1.0.0.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50f4303f40f85103302d9f6d08031e7ca1f747ea1025b6bead45142f66d2cf20
|
|
| MD5 |
e70cf0a88ef4d86536be46daf682d233
|
|
| BLAKE2b-256 |
63b3044dee64344845a6f69783b5c0c779872cfbe2aa002345b8afcd3fb49260
|
File details
Details for the file asiocmd-1.0.0-py3-none-any.whl.
File metadata
- Download URL: asiocmd-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
593f88020f09ffc85fef241659747696076ce27c950049fa5afb3f2bd8292cb3
|
|
| MD5 |
5115554b08db4cbe6fac9e50264db652
|
|
| BLAKE2b-256 |
ad6493b60af24f8ee124424cee96cc9caf8f9bf98eb447a721244a325021b2c2
|