设置自动日志,任何时间单击CLI命令?

发布于 2025-02-12 19:19:52 字数 168 浏览 0 评论 0原文

我当前在代码中使用单击来设置大量命令行工具,但是我想运行相当于click.echo(“ Cli Function X”)任何时间函数x被调用。有没有办法在我的项目中进行一般设置?还是我必须为每个CLI功能分别添加此功能?

I currently use click inside my code for setting up plenty of command line tools, but I'd like to run the equivalent of click.echo("CLI function X was called") any time function X is called. Is there a way to set up this up generally across my project? Or do I have to add this function individually for each CLI function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2025-02-19 19:19:52

我需要这个并修改这个答案以获取解决方案。给定此基本单击组:

import click

@click.group()
def cli():
    pass

@cli.command()
@click.argument("name", default="World")
def hello(name):
    print(f"Hello, {name}!")

if __name__ == "__main__":
    cli()

我添加了一个函数,该函数采用上下文并调用方法:

def _invoke(self, ctx, invoke):
    command = getattr(ctx, "command", ctx).name
    print(f"Running {command}: {ctx.params}")
    result = invoke(self, ctx)
    print(f"Completed {command}")
    return result

然后,我创建了一些自定义类,覆盖Invoke命令以调用上述函数:

class _Group(click.Group):
    def invoke(self, ctx):
        return _invoke(self, ctx, click.Group.invoke)

class _Command(click.Command):
    def invoke(self, ctx):
        return _invoke(self, ctx, click.Command.invoke)

将功能添加到现有脚本中很简单,将类通过cls参数将类传递到装饰符中,即:

@click.group(cls=_Group)
def cli():
    pass

@cli.command(cls=_Command)
@click.argument("name", default="World")
def hello(name):
    print(f"Hello, {name}!")

运行Hello命令现在在命令执行之前和之后打印到控制台:

$ python3 cli.py hello StackOverflow
Running cli: {}
Running hello: {'name': 'StackOverflow'}
Hello, StackOverflow!
Completed hello
Completed cli

I needed this and modified this answer to get a solution. Given this basic click group:

import click

@click.group()
def cli():
    pass

@cli.command()
@click.argument("name", default="World")
def hello(name):
    print(f"Hello, {name}!")

if __name__ == "__main__":
    cli()

I added a function that takes a context and invoke method:

def _invoke(self, ctx, invoke):
    command = getattr(ctx, "command", ctx).name
    print(f"Running {command}: {ctx.params}")
    result = invoke(self, ctx)
    print(f"Completed {command}")
    return result

I then created a few custom classes that override the invoke command to call the above function:

class _Group(click.Group):
    def invoke(self, ctx):
        return _invoke(self, ctx, click.Group.invoke)

class _Command(click.Command):
    def invoke(self, ctx):
        return _invoke(self, ctx, click.Command.invoke)

Adding the functions to the existing script is simple, pass the classes into the decorators for the groups and methods with the cls argument, i.e.:

@click.group(cls=_Group)
def cli():
    pass

@cli.command(cls=_Command)
@click.argument("name", default="World")
def hello(name):
    print(f"Hello, {name}!")

Running the hello command now prints to the console before and after the command's execution:

$ python3 cli.py hello StackOverflow
Running cli: {}
Running hello: {'name': 'StackOverflow'}
Hello, StackOverflow!
Completed hello
Completed cli
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文