gpt4 book ai didi

python - 单击单独文件中的命令

转载 作者:行者123 更新时间:2023-11-28 18:03:52 26 4
gpt4 key购买 nike

我有一个使用最新点击包的 Python 3.6 脚本。我有很多命令,所以我想将一些命令移到单独的模块中。例如。

main.py: root, with commands A and B as children
mylib1.py: commands C and D
mylib2.py: commands E and F

然后在 main 中,我想将 C“导入”到 F,这样 main 似乎具有所有 6 个命令,即所有 6 个都是 root 的子级。

蛮力需要大量维护:

# mylib1.py
@click.command()
def cmd1():
...
...
@click.command()
def cmdN():
...

# main.py
import click
from mylib1 import cmd1, cmd2, ... cmdN

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

@main.command()
def main_cmd1(): pass

main.add_command(cmd1)
...
main.add_command(cmdN)

少一点维护(无需管理导入):

# mylib1.py
def add_commands(group):
group.add_command(cmd1)
...
group.add_command(cmdN)

# main.py
import click, mylib

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

@main.command()
def main_cmd1(): pass

mylib1.add_commands(main)
main()

但最可维护的似乎是使用 click.CommandCollection,这让我可以让它看起来好像一组中的命令实际上在另一组中:

# mylib1.py
import click

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

@commands.command()
def cmd1():
...
...
@commands.command()
def cmdN():
...

# main.py
import click, mylib1

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

@main_group.command()
def main_cmd1(): pass

main = click.CommandCollection([main_group, mylib1.commands])
main()

这似乎工作得很好,到目前为止没有问题,但是 CommandCollection 的文档说这不是“推荐的”:

it can also be interesting to merge multiple together into one script. While this is generally not as recommended as it nests one below the other, the merging approach can be useful in some circumstances for a nicer shell experience.

有谁知道“不推荐,因为它嵌套在另一个之下”是什么意思,这种方法可能有哪些陷阱?

最佳答案

从单独的文件加载点击命令

来自文档:

Click in three points:

  • arbitrary nesting of commands

  • automatic help page generation

  • supports lazy loading of subcommands at runtime

我认为第三点(它被认为足够重要,足以放入那个短名单)指出了您正在尝试做的事情的最佳方式。

只有当我不是某些命令代码的所有者(无法轻松编辑)时,我才倾向于使用 click.CommandCollection。我认为对于此处的用例,首选解决方案是使用 click.MultiCommand。说明 from the docs :

In addition to using click.group(), you can also build your own custom multi commands. This is useful when you want to support commands being loaded lazily from plugins.

docs 中有一个简单的用法示例, 在 SO 上, 和一个更复杂的例子 on GitHub ,它被描述为:

complex is an example of building very complex cli applications that load subcommands dynamically from a plugin folder and other things.

如果你需要避免 CommandCollection 的原因,在 other issues 中,github 上有一个长期存在的错误,标题为:Adding group to CommandCollection loses group options .

回答你的问题

作为quotedquestion :

it can also be interesting to merge multiple together into one script. While this is generally not as recommended as it nests one below the other, the merging approach can be useful in some circumstances for a nicer shell experience.

您的问题:

Does anyone know what is meant by "not as recommended as it nests one below the other", what could be some possible gotchas to that approach.

我认为这种重写可能会使声明更加清晰。

While this is generally not as recommended as nesting one below the other, the merging approach can be ...

我相信这就是想要表达的意思。

关于python - 单击单独文件中的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54662962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com