@ wrote... (1 year, 6 months ago)

I had a hell of a time trying to make a nice help command while using click.

I wanted the ability to have a help sub-command for each real command.

mycommand --help
mycommand help

mycommand foo --help
mycommand help foo

It's actually fairly easy (once you know how).

CTX_SETTINGS=dict(help_option_names=['-h','--help'])

@click.group(context_settings=CTX_SETTINGS)
@click.pass_context
def cli(ctx, **kw):
    ...

@cli.command()
@click.pass_context
def foo(ctx):
    ...

@cli.command()
@click.argument('topic', default=None, required=False, nargs=1 )
@click.pass_context
def help(ctx, topic, **kw):
    if topic is None:
        print ctx.parent.get_help()
    else:
        print cli.commands[topic].get_help(ctx)
Category: tech, Tags: python
Comments: 0
Click here to add a comment