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.7

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.7.4.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

aiocli-1.7.4-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiocli-1.7.4.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for aiocli-1.7.4.tar.gz
Algorithm Hash digest
SHA256 b89715d8b9d551af048240c3246d688a95f3f94563ef38e8026b2b325030f929
MD5 4faf94d03c3feee3cb9904ec64c547bd
BLAKE2b-256 234d7b181012c5f09c82cceffa76ac135566dda1440eec7836473fc3ddeabc4b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiocli-1.7.4-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for aiocli-1.7.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1e80d70f8d3eba24374ce39e74234f4e272e4947619b299b0b450cddf44c2869
MD5 889a6257672c665232ba1e2fb03033fb
BLAKE2b-256 5157ec24be8b285c72d6cf43120f5b60f5d076b992f2eb6aebd9b1339deba453

See more details on using hashes here.

Supported by

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