Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to do multiple nested commands in Click 6

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.

like image 561
phrreakk Avatar asked Jun 12 '18 15:06

phrreakk


1 Answers

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()
@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={})
like image 191
phrreakk Avatar answered Oct 10 '22 17:10

phrreakk