aiocli is a Python library for simple and lightweight async console runner.
Full compatibility with argparse module and highly inspired by aiohttp module.
Use the package manager pip to install aiocli.
pip install aiocli- Visit aiocli docs.
fromloggingimportgetLogger, Logger, StreamHandlerfromosimportgetenvfromaiocli.commanderimportrun_app, Application, Depends, Stateapp=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)
returnlogger@app.command(name='greet:to', positionals=[('name', {'default': 'World!'})])asyncdefhandle_greeting(name: str, logger: Logger=Depends(_get_logger)) ->int:
logger.info(f'Hello {name}')
return0@app.command(name='div', optionals=[('--a', {'type': float}), ('--b', {'type': float})])asyncdefhandle_division(a: float, b: float, logger: Logger=Depends(_get_logger)) ->int:
try:
logger.info(f'Result {a} / {b} = {(a/b)}')
return0exceptBaseExceptionaserr:
logger.error(f'Error: {err}')
return1# python3 main.py <command> <positionals> <optionals>if__name__=='__main__':
run_app(app)- Python >= 3.10
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.