[英]python click framework - custom multi command implementation in oop's method
I have written some scripts that I'm trying to integrate with click.我已经编写了一些我试图与 click 集成的脚本。 All the scripts are written in python OOP's.
所有的脚本都是用 python OOP 编写的。
The issue is that i am trying to build command section in oop's way but couldn't do it properly.问题是我正在尝试以 oop 的方式构建命令部分,但无法正确执行。
let me show you, what i am trying to do and please note that i am sharing here dummy code it is very similar to the real code.让我告诉你,我正在尝试做什么,请注意我在这里分享的是虚拟代码,它与真实代码非常相似。
First thing the directory structure:首先是目录结构:
-customcmd <dir>
|
|->commands <dir>
| -> abc-command.py
| -> __init__.py
|
|->__init__.py
|->main.py
|->setup.py
1) I have created one file called main.py, which contains following code: 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 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) This is how I converted the procedural code of abc-command.py code: 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()
Here i am calling the main.py我在这里调用 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".
In the above output you can see it is directly going into the sub-command options things and giving error as well.在上面的 output 中,您可以看到它直接进入子命令选项的内容并给出错误。
As far as i understand list_commands() which is in main.py can't list out the commands, this part i can't understand why it is not working properly.据我了解 main.py 中的 list_commands() 无法列出命令,这部分我无法理解为什么它不能正常工作。
I tried various things but couldn't find the proper way to implement OOP's in abc-command.py because of that my ouput doesn't match.我尝试了各种方法,但找不到在 abc-command.py 中实现 OOP 的正确方法,因为我的输出不匹配。
I am new to this click framework, please suggest any new changes in my approach if needed.我是这个点击框架的新手,如果需要,请在我的方法中提出任何新的更改。
please look into this, sorry for this weird way to explaining this.请调查一下,对于这种奇怪的解释方式感到抱歉。
abc-command.py is evaluated before click parses command options because of this line in the file invoking the cli
method: abc-command.py 在 click 解析命令选项之前被评估,因为文件中的这一行调用了
cli
方法:
obj.cli()
Also, in the get_command
method implemented for the multi-command, commands are supposed to expose a 'cli' name in their namespace.此外,在为多命令实现的
get_command
方法中,命令应该在其命名空间中公开一个“cli”名称。
To fix this error, in abc-command.py
update the line invoking the cli command with:要修复此错误,请在
abc-command.py
中更新调用 cli 命令的行:
cli = obj.cli
so that a cli name is exposed in abc-command.py
module以便在
abc-command.py
模块中公开 cli 名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.