【问题标题】:result_callback in Click 8.x not working anymore?Click 8.x 中的 result_callback 不再工作了?
【发布时间】:2022-08-22 14:23:03
【问题描述】:

我几乎一整天都在对我的一个脚本进行故障排除,这让我很头疼。它以问题告终,这似乎在 8.x 版本中,在我的情况下,result_callback 不再执行。

样本:

@click.command(cls=PluginCommands) # PluginCommands is inherited from click.MultiCommand)
@click.pass_context
def foo(ctx):
   pass

@foo.command(\'bar\')
def bar(ctx):
   do_stuff()

foo.result_callback = some_mailing_function

这曾经在 7.1.2 版中运行得很好,但在意外安装的 8.1.3 版中却不行。 result_callback 中的注册函数似乎永远不会执行。

有没有其他人遇到过这个问题?

非常感谢,问候,托马斯

    标签: python-3.x python-click


    【解决方案1】:

    result_callback 可以通过两种方式使用:

    1. 它可以通过 MultiCommand 类的初始化作为参数提供,或者
    2. 以后可以作为装饰器使用。

      如果我们使用第二种方法,您的代码应该类似于:

      @click.command(cls=PluginCommands) # PluginCommands is inherited from click.MultiCommand)
      @click.pass_context
      def foo(ctx):
         pass
      
      @foo.result_callback()
      def some_mailing_function():
          pass
      
      @foo.command('bar')
      def bar(ctx):
         do_stuff()
      
      

    【讨论】:

      猜你喜欢
      • 2014-08-21
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      相关资源
      最近更新 更多