Possible to do multiple nested commands in Click 6
Asked Answered
P

1

19

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.

Palpitant answered 12/6, 2018 at 15:50 Comment(0)
P
29

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={})
Palpitant answered 12/6, 2018 at 18:54 Comment(3)
is the https necessary in https_create?Featured
@arshbot, It's not necessary in this example, but if you had the same command create under both wp and https, then you would need to differentiate them. It's a good practice to do so.Tannie
In my cli, I had each group split into its own Python file (ie module). The decorator doesn't work when splitting across files, so I had to manually call main.add_command(wp) within main.py to add the wp function that was in a separate file.Bautzen

© 2022 - 2024 — McMap. All rights reserved.