def parse_arguments(fn: Any) -> Callable[[Any, str], None]: _, prog_name = fn.__name__.split("_", 1) orig_fn = copy.deepcopy(fn) fn = click.pass_obj(fn) command = click.command()(fn) def wrapped(self: Any, argstr: str) -> None: argv = shlex.split(argstr) logger.debug("%s", argv) try: command.main( args=argv, prog_name=prog_name, standalone_mode=False, obj=self, help_option_names=["-h", "--help"], ) except click.MissingParameter: click.echo(f"usage: {fn.__doc__}") wrapped.__doc__ = fn.__doc__ # type: ignore wrapped.__name__ = fn.__name__ # type: ignore wrapped.__orig_fn__ = orig_fn # type: ignore return wrapped
def run_steps(name: str, steps: List[Step]): def command(config): _steps(steps, config) command.__name__ = name return cli.command()(click_log.simple_verbosity_option(logger)(capture( click.pass_obj(command))))
def pass_obj(f): if iscoroutinefunction(f): @wraps(f) async def wrapper(*args, **kwargs): return await f(get_current_context().obj, *args, **kwargs) return wrapper return click.pass_obj(f)
def _decorator(callback: Callable[..., Awaitable[_T]]) -> Callable[..., _T]: assert iscoroutinefunction(callback) @functools.wraps(callback) def wrapper(*args: Any, **kwargs: Any) -> _T: with _runner() as runner: return runner.run(callback(*args, **kwargs)) if pass_obj: wrapper = click.pass_obj(wrapper) return wrapper
def wrap(f, group, name=None): """ Wraps a 'plugin' function into a click command and registers it to the given group. a plugin function receives the scanner object in its first argument """ if not name: name = cmdname(f.__module__) d = click.command(name=name) cmd = d(click.pass_obj(f)) group.add_command(cmd) return cmd
def async_cmd(func: Callable[..., Awaitable[None]]) -> Callable[..., None]: @functools.wraps(func) def inner(root: Root, **kwargs: Any) -> None: try: return asyncio.run(func(root, **kwargs)) except Exception as exc: if root.show_traceback: raise else: click.echo(f"Error: {exc}") inner = click.pass_obj(inner) return inner
def packargs(func): import click return click.pass_obj(_packargs(func))
def command(func): return main.command()(click.pass_obj(func))