gpt4 book ai didi

python - 可以在 Click 6 中执行多个嵌套命令

转载 作者:太空狗 更新时间:2023-10-30 02:53:08 26 4
gpt4 key购买 nike

我将写一些非常基础的东西来解释我想要实现的目标。我已经编写了一些代码来执行一些有趣的 WordPress 管理。该程序将创建实例,但也会为 apache 创建 https 设置。

我希望它做什么以及我遇到问题的地方:(如果你在 wp cli 上运行帮助,你会看到我想要发生的事情......但我不是开发人员所以我需要一些帮助)

python3 testcommands.py --help
Usage: testcommands.py [OPTIONS] COMMAND [ARGS]...

This is the help

Options:
--help Show this message and exit.

Commands:
https Commands for HTTPS
wp Commands for WP



python3 testcommands.py https --help
Usage: testcommands.py [OPTIONS] COMMAND [ARGS]...

This is the help

Options:
--help Show this message and exit.

Commands:
create Creates config for HTTPS
sync Syncs config for Apache

我的基本代码:

import click


@click.group()
def cli1():
pass
"""First Command"""


@cli1.command('wp')
def cmd1():
"""Commands for WP"""
pass


@cli1.command('install')
@click.option("--test", help="This is the test option")
def install():
"""Test Install for WP"""
print('Installing...')


@click.group()
def cli2():
pass
"""Second Command"""


@cli2.command('https')
def cmd2():
click.echo('Test 2 called')


wp = click.CommandCollection(sources=[cli1, cli2], help="This is the help")


if __name__ == '__main__':
wp()

返回:

python3 testcommands.py --help
Usage: testcommands.py [OPTIONS] COMMAND [ARGS]...

This is the help

Options:
--help Show this message and exit.

Commands:
https
install Test Install for WP
wp Commands for WP

我想不通。我不想安装在这里显示,因为它应该在 wp 下面,以免显示。

如果你能帮助我,谢谢你......我相信这很简单......或者也许不可能......但无论如何谢谢。

最佳答案

一旦我找到一个试图做同样事情的网站,我就能够弄明白了。

https://github.com/chancez/igor-cli

我想不出名字...但我正在寻找一种执行 HIERARCHICAL 命令的方法。

这是基本代码:

import click

@click.group()
@click.pass_context
def main(ctx):
"""Demo WP Control Help"""

@main.group()
@click.pass_context
def wp(ctx):
"""Commands for WP"""

@wp.command('install')
@click.pass_context
def wp_install(ctx):
"""Install WP instance"""

@wp.command('duplicate')
@click.pass_context
def wp_dup(ctx):
"""Duplicate WP instance"""

@main.group()
@click.pass_context
def https(ctx):
"""Commands for HTTPS"""

@https.command('create')
@click.pass_context
def https_create(ctx):
"""Create HTTPS configuration"""

@https.command('sync')
@click.pass_context
def https_sync(ctx):
"""Sync HTTPS configuration with Apache"""

if __name__ == '__main__':
main(obj={})

关于python - 可以在 Click 6 中执行多个嵌套命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50821202/

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