Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cliar/cliar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from argparse import ArgumentParser, RawTextHelpFormatter
from inspect import signature, getmembers, ismethod, isclass
from asyncio import get_event_loop
from inspect import signature, getmembers, ismethod, isclass, iscoroutine
from collections import OrderedDict
from typing import List, Iterable, Callable, Set, Type, get_type_hints

Expand Down Expand Up @@ -291,7 +292,12 @@ def parse(self):
for subcli in self._subclis:
subcli.global_args = self.global_args

if command.handler(**handler_args) == NotImplemented:
result = command.handler(**handler_args)

if iscoroutine(result):
result = get_event_loop().run_until_complete(result)

if result == NotImplemented:
command.handler.__self__._parser.print_help()

def _root(self):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_async_fns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from subprocess import run


def test_help(capfd, datadir):
run(f'python {datadir/"async_fns.py"} wait -h', shell=True)

output = capfd.readouterr().out

assert '-s SECONDS-TO-WAIT, --seconds-to-wait SECONDS-TO-WAIT' in output

def test_wait(capfd, datadir):
seconds_to_wait = 1.0

run(f'python {datadir/"async_fns.py"} wait -s "{seconds_to_wait}"',
shell=True)
seconds_awaited = float(capfd.readouterr().out.strip())
assert round(seconds_awaited, 1) == round(seconds_to_wait, 1)
15 changes: 15 additions & 0 deletions tests/test_async_fns/async_fns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import asyncio
from time import perf_counter

from cliar import Cliar


class AsyncFunctions(Cliar):
async def wait(self, seconds_to_wait: float = 1.0):
t1 = perf_counter()
await asyncio.sleep(seconds_to_wait)
elapsed = perf_counter() - t1
print(elapsed)

if __name__ == "__main__":
AsyncFunctions().parse()