为什么点击 add_command 不起作用?

Why is the click add_command not working?

运行 以下代码作为 python script.py 什么都不做,我希望至少打印 click.echo 语句,听起来像对 first_command 的调用没有工作,知道为什么吗?

import click

def multiple_input_option(**option_kwargs):
    def decorator(func):
        def _callback(ctx, param, value):
            ctx.obj['multiple_options'] = value

        help_text = 'some options to get from user'

        keyword_arguments = {
            'help': help_text,
            'callback': _callback,
            'expose_value': False,
            'multiple': True,
        }
        keyword_arguments.update(**option_kwargs)

        return click.option('--multiple-options', '-e', **keyword_arguments)(func)

    return decorator

@click.command()
@multiple_input_option()
def first_command(ctx):
    click.echo('> hello...first_command')
    command_param_data = ctx.obj.keys()

    click.echo(json.dumps(command_param_data, indent=2))

@click.group()
def hello_world(ctx):
    """
    Your plugin description here
    """
    ctx.ensure_object(dict)



# This is how we add more sub-commands
hello_world.add_command(first_command)

我想到了以下解决方案。

import click
#(1) import json, other wise json.dump will not work
import json

def multiple_input_option(**option_kwargs):
    def decorator(func):
        def _callback(ctx, param, value):
          ctx.obj['multiple_options'] = value

        help_text = 'some options to get from user'

        keyword_arguments = {
            'help': help_text,
            'callback': _callback,
            'expose_value': False,
            'multiple': True,
        }
        keyword_arguments.update(**option_kwargs)

        return click.option('--multiple-options', '-e', **keyword_arguments)(func)

    return decorator

@click.command()
@multiple_input_option()
#(2) pass context here
@click.pass_context
def first_command(ctx):
    click.echo('> hello...first_command')
    command_param_data = ctx.obj.keys()   
    click.echo(json.dumps(command_param_data, indent=2))

@click.group()
#(3) pass context here
@click.pass_context
def hello_world(ctx):
    """
    Your plugin description here
    """
    ctx.ensure_object(dict)

# This is how we add more sub-commands
hello_world.add_command(first_command)

#(4) make a call in the main function
if __name__ == '__main__':
  hello_world()

输出结果如下:

我必须执行以下操作才能使其正常工作:

(1)导入json模块,这样就可以使用json.dump

import json

(2)/(3) 因为 hello_worldfirst_command 期望上下文,你有通过

传递
click.pass_context

根据 Click 文档 here

[w]henever a Click command is executed, a Context object is created which holds state for this particular invocation. It remembers parsed parameters, what command created it, which resources need to be cleaned up at the end of the function, and so forth. It can also optionally hold an application-defined object.

如上所述,上下文是 class“上下文”的对象(有关详细信息,请参阅单击 API here)。你在hello_world和first_command中使用的参数“ctx”就是这样一个对象。在 hello_world 中,您调用函数“ensure_object”,它是 class“上下文”的函数。由于您正在使用上下文对象,因此您必须确保它已传递给您的命令,这是由

完成的
click.pass_context

(4) 最后我添加了一个main函数,用来调用命令

if __name__ == '__main__':
    hello_world()

json.dump的最后一句话。我假设,您使用的是 python 2.x,在这种情况下,调用没问题。对于 python 3.x,调用应如下所示:

click.echo(json.dumps(list(command_param_data), indent=2))