【问题标题】:Possible to do multiple nested commands in Click 6可以在 Click 6 中执行多个嵌套命令
【发布时间】:2018-06-12 15:50:00
【问题描述】:

我将写一些非常基本的东西来解释我想要实现的目标。我写了一些代码来做一些有趣的 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

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

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

【问题讨论】:

    标签: python python-3.x command-line-interface python-click


    【解决方案1】:

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

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

    【讨论】:

    • httpshttps_create 中的必要条件吗?
    • @arshbot,在这个例子中没有必要,但是如果你在wphttps下都有相同的命令create,那么你需要区分它们。这样做是个好习惯。
    猜你喜欢
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多