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

phr*_*akk 12 python command-line-interface python-3.x python-click

我将写一些非常基本的东西,以解释我希望实现的目标。我写了一些代码来做一些有趣的 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
Run Code Online (Sandbox Code Playgroud)

我的基本代码:

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()
Run Code Online (Sandbox Code Playgroud)

返回:

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
Run Code Online (Sandbox Code Playgroud)

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

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

phr*_*akk 18

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

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

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

这是基本代码:

导入点击

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


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


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


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


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


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

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


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)