可以在单击 6 中执行多个嵌套命令



我将写一些非常基本的东西,以解释我希望实现的目标。 我已经编写了一些代码来做一些有趣的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

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

这是基本代码:

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={})

最新更新