Click: KeyError 'cli'

Created on 13 Aug 2018  路  3Comments  路  Source: pallets/click

http://click.pocoo.org/6/commands/#custom-multi-commands

line 22, in get_command return ns['cli']
KeyError: 'cli'

How do I fix this?

Most helpful comment

You make sure there's a command object named "cli" in the plugin file you load. The example is just an example of multi commands, it's not meant to be particularly robust.

All 3 comments

You make sure there's a command object named "cli" in the plugin file you load. The example is just an example of multi commands, it's not meant to be particularly robust.

Oh, okey. Thanks!

I know this is an old closed issue, but would it be acceptable to modify the get_command method in MyCli like this?

def get_command(self, ctx, name):
    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[name]

If I understand correctly, this would require the files to have a group with the same name to work, for example in the commands/test.py file:

import click
import time

@click.group(invoke_without_command=True)
@click.pass_context
def test(ctx):
    if ctx.invoked_subcommand is None:
        print("default command")

@test.command()
def task():
    print("custom test task")

Now that I think about it, I'd rather use a cli object in each file to keep the group names consistent, but I was curious if that would work as I expect it to.

Was this page helpful?
0 / 5 - 0 ratings