Possible to do multiple nested commands in Click 6

戏子无情 提交于 2020-01-01 06:10:31

问题


I am going to write something very basic so as to explain what I'm looking to make happen. I have written some code to do some interesting WordPress administration. The program will create instances but also create https settings for apache.

What I would like it to do and where I'm having a problem: (If you run a help on the wp cli you will see exactly what I want to have happen...but I'm not a developer so I'd like some help)

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

My basic code:

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()

Returns:

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

I can't figure this out. I don't want install to show here, as it should be underneath wp so as not to show.

Thank you if you can help me out...I'm sure it is simple...or maybe not possible...but thanks anyways.


回答1:


I was able to figure it out once I found a site that was trying to do the same thing.

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

I couldn't figure out the name...but I was looking for a way to do HIERARCHICAL commands.

Here is the base code:

import click

@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()


来源:https://stackoverflow.com/questions/50821202/possible-to-do-multiple-nested-commands-in-click-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!