首页 > 解决方案 > python click框架-oop方法中的自定义多命令实现

问题描述

我已经编写了一些我试图与 click 集成的脚本。所有脚本都是用 python OOP 编写的。

问题是我正在尝试以 oop 的方式构建命令部分,但无法正确执行。

让我告诉你,我正在尝试做什么,请注意我在这里分享的是虚拟代码,它与真实代码非常相似。

首先是目录结构:

-customcmd <dir>
|
|->commands <dir>
|  -> abc-command.py
|  -> __init__.py
|
|->__init__.py
|->main.py
|->setup.py

1) 我创建了一个名为 main.py 的文件,其中包含以下代码:

import click
import os

plugin_folder = os.path.join(os.path.dirname(__file__), 'commands')

class MyCLI(click.MultiCommand):

    def list_commands(self, ctx):
        rv = []
        for filename in os.listdir(plugin_folder):
            if filename.startswith('__'):
                continue
            if filename.endswith('.py'):
                rv.append(filename[:-3])
        rv.sort()
        return rv

    def get_command(self, ctx, name):
        ns = {}
        fn = os.path.join(plugin_folder, name + '.py')
        with open(fn) as f:
            code = compile(f.read(), fn, 'exec')
            eval(code, ns, ns)
        return ns['cli']

cli = MyCLI()#help='This tool\'s subcommands are loaded from a ''plugin folder dynamically.'
if __name__ == '__main__':
    cli()

2) abc-command.py

@click.command()
@click.option("--file-loc", '-fl', type=open, required=True, default=None, help="Path to the file")

def cli(file_loc):
    """ 
        This is test command

    """
    print("Path to file {}".format(file_loc))

(automation) K:\Pythonenv\automation\customcmd>python main.py
Usage: main.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  policyresult  This is test command
(automation) K:\Pythonenv\automation\customcmd>python main.py policyresult --help
Usage: main.py policyresult [OPTIONS]

  This is test command

Options:
  -fl, --file-loc OPEN    Path to the file  [required]
  --help                  Show this message and exit.

3) 这就是我转换 abc-command.py 代码的程序代码的方式:

  class policyresult():



    def __init__(self):
        pass


    @click.command()
    @click.option("--file-loc", '-fl', type=open, required=True, default=None, help="Path to the file")

    def cli(self,file_loc):
        """ 
            This is test command

        """
        print("Path to file {}".format(file_loc))



obj = policyresult()
obj.cli()

我在这里调用 main.py

(automation) K:\Pythonenv\automation\customcmd>python main.py
Usage: main.py [OPTIONS]
Try "main.py --help" for help.

Error: Missing option "--file-loc" / "-fl".

在上面的输出中,您可以看到它直接进入子命令选项的事情并给出错误。

据我了解 main.py 中的 list_commands() 无法列出命令,这部分我无法理解为什么它不能正常工作。

我尝试了各种方法,但找不到在 abc-command.py 中实现 OOP 的正确方法,因为我的输出不匹配。

我是这个点击框架的新手,如果需要,请在我的方法中提出任何新的更改。

请调查一下,对于这种奇怪的解释方式感到抱歉。

标签: pythonpython-3.xpython-click

解决方案


abc-command.py 在单击解析命令选项之前被评估,因为文件中调用该cli方法的这一行:

obj.cli()

此外,在get_command为多命令实现的方法中,命令应该在其命名空间中公开一个“cli”名称。

要修复此错误,abc-command.py请使用以下命令更新调用 cli 命令的行:

cli = obj.cli

以便在abc-command.py模块中公开 cli 名称


推荐阅读