簡體   English   中英

為什么單擊 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()

output如下: 使用和不使用命令調用腳本的輸出

我必須執行以下操作才能使其工作:

(1) 導入 json 模塊,這樣就可以使用 json.dump

import json

(2)/(3) 由於hello_worldfirst_command期望上下文,因此您必須通過

click.pass_context

根據此處的 Click 文檔,

[當]執行 Click 命令時,會創建一個上下文 object,其中包含此特定調用的 state。 它會記住解析的參數、創建它的命令、function 末尾需要清理哪些資源等等。 它還可以選擇保存應用程序定義的 object。

如上所述,上下文是 class“上下文”的 object(有關更多信息,請參見單擊 API此處)。 你在 hello_world 和 first_command 中使用的參數“ctx”就是這樣一個 object。 在 hello_world 中,您將 function 稱為“ensure_object”,它是 class“上下文”的 function。 由於您使用的是上下文 object,因此您必須確保將其傳遞給您的命令,這是由

click.pass_context

(4)最后我添加了一個主function,來調用命令

if __name__ == '__main__':
    hello_world()

最后一句話json.dump 我假設,您使用的是 python 2.x,在這種情況下調用沒問題。 對於 python 3.x,調用應如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM