click-contrib/click-help-colors

Is there a way to use this plugin with click.MultiCommand

tutume opened this issue · 3 comments

Hi,

I'm trying to build a fancy cli using click and now I found this click plugin. I was able to change colors on the command1.py, but how I could do it from the first level when using the click.MultiCommand?

I'm using this approuch that best suit my needs:

.
|-- cli
|   |-- __init__.py
|   |-- cli.py
|   `-- commands
|       |-- __init__.py
|       |-- commands1.py
|       `-- commands2.py
`-- setup.py
#cli.py
import click
import os

plugin_folder = os.path.join(os.path.dirname(__file__), 'commands')

class MyCLI(click.MultiCommand):

    def list_commands(self, ctx):
        """Dynamically get the list of commands."""
        rv = []
        for filename in os.listdir(plugin_folder):
            if filename.endswith('.py') and not filename.startswith('__init__'):
                rv.append(filename[:-3])
        rv.sort()
        return rv

    def get_command(self, ctx, name):
        """Dynamically get the command."""
        ns = {}
        fn = os.path.join(plugin_folder, name + '.py')
        with open(fn) as f:
            code = compile(f.read(), fn, 'exec')
            eval(code, ns, ns)
        return ns['cli']


@click.command(cls=MyCLI)
def cli():
    """The CLI."""
    pass

if __name__ == '__main__':
    cli()
#commands1.py
import click
from click_help_colors import HelpColorsGroup, HelpColorsCommand

@click.group(cls=HelpColorsGroup,
    help_headers_color='yellow',
    help_options_color='green'
    )
def cli(): pass
@cli.command()
def c1(): click.echo("g1 c1")
@cli.command()
def c2(): click.echo("g1 c1")

if __name__ == "__main__":
    cli()
r-m-n commented

Hi, I added the HelpColorsMultiCommand class. In your example, it would be:

#cli.py
from click_help_colors import HelpColorsMultiCommand

class MyCLI(HelpColorsMultiCommand):
    ...

@click.command(
    cls=MyCLI,
    help_headers_color='yellow',
    help_options_color='green'
)
def cli():
    """The CLI."""
    pass

See also the example in examples/multi_commands.py

Great, worked perfectly.
When do you expect to update the pypi repo in order to be able to install with pip?

Thanks.

Closing