Skip to main content

Simple and lightweight async console runner.

Project description

Async cli client/commander framework

PyPI version PyPIDownloads CI

aiocli is a Python library for simple and lightweight async console runner.

Full compatibility with argparse module and highly inspired by aiohttp module.

Installation

Use the package manager pip to install aiocli.

pip install aiocli

Documentation

Usage

from logging import getLogger, Logger, StreamHandler
from os import getenv

from aiocli.commander import run_app, Application, Depends, State

app = Application(state={
    'envs': {
        'LOGGER_NAME': str(getenv('LOGGER_NAME', 'example_app')),
        'LOGGER_LEVEL': str(getenv('LOGGER_LEVEL', 'INFO')),
    }
})

def _get_logger(state: State) -> Logger:
    logger = getLogger(state.get('envs')['LOGGER_NAME'])
    logger.setLevel(state.get('envs')['LOGGER_LEVEL'])
    handler = StreamHandler()
    logger.addHandler(handler)
    return logger

@app.command(name='greet:to', positionals=[('name', {'default': 'World!'})])
async def handle_greeting(name: str, logger: Logger = Depends(_get_logger)) -> int:
    logger.info(f'Hello {name}')
    return 0

@app.command(name='div', optionals=[('--a', {'type': float}), ('--b', {'type': float})])
async def handle_division(a: float, b: float, logger: Logger = Depends(_get_logger)) -> int:
    try:
        logger.info(f'Result {a} / {b} = {(a / b)}')
        return 0
    except BaseException as err:
        logger.error(f'Error: {err}')
        return 1

# python3 main.py <command> <positionals> <optionals>
if __name__ == '__main__':
    run_app(app)

Requirements

  • Python >= 3.9

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Project details


Download files

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

Source Distribution

aiocli-1.10.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

aiocli-1.10.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file aiocli-1.10.0.tar.gz.

File metadata

  • Download URL: aiocli-1.10.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0rc2

File hashes

Hashes for aiocli-1.10.0.tar.gz
Algorithm Hash digest
SHA256 a06031367e782f7ad26b1a9f3664cd93c691379ea1ed51810832bcb4357b04d9
MD5 994d4138f8f97b629ba1bddee011b6d3
BLAKE2b-256 d240099db8562d3ea531e25bbbad8c9c987e964580db20a99eff4442fc0d5f0a

See more details on using hashes here.

File details

Details for the file aiocli-1.10.0-py3-none-any.whl.

File metadata

  • Download URL: aiocli-1.10.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0rc2

File hashes

Hashes for aiocli-1.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a24aff0d12502117b570d5e7a5d64453b7d649670a90ab923b3ceaf3098c7c0e
MD5 90c54e9a979a40900ce942c2bd2c06db
BLAKE2b-256 218810c08ddb885d618d3ba50b83596ac2d1f43074386c242571ce2b8b2c1b4d

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