You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python Click优雅处理多键值对形式的参数传递?

Handling Repeated Key-Value Parameters in Python Click

Great question! This pattern is totally common, and Click actually has a clean, idiomatic way to handle it without relying on clunky custom callbacks for parsing. Here are two approaches, from a simple quick fix to a more robust, reusable solution:

The cleanest way is to create a custom ParamType that handles parsing and validation of each key=value pair. This integrates seamlessly with Click's built-in error handling:

import click

class KeyValuePair(click.ParamType):
    name = 'key=value'

    def convert(self, value, param, ctx):
        # Split on the first '=' to allow values that contain '=' characters
        try:
            key, val = value.split('=', 1)
            return (key.strip(), val.strip())
        except ValueError:
            # Click will automatically show this error message to the user
            self.fail(f"'{value}' is not a valid key=value pair. Use format 'key=value'", param, ctx)

@click.group()
def myprogram():
    pass

@myprogram.command()
@click.option(
    '--parameter', '-p',
    type=KeyValuePair(),
    multiple=True,  # Allow multiple --parameter flags
    help='Specify key-value parameters (e.g., param1=value1)'
)
def mycommand(parameter):
    # Convert the tuple of (key, value) pairs into a dictionary
    params = dict(parameter)
    click.echo(f"Parsed parameters: {params}")

Why this works:

  • multiple=True lets users pass --parameter as many times as needed.
  • The custom KeyValuePair type handles validating that each input follows the key=value format, and splits keys/values correctly (even if values contain = characters).
  • Click takes care of error messaging if a user passes an invalid format, so you don't have to write custom error handling code.

2. Quick Fix: Inline Parsing

If you don't need reusable validation, you can parse the pairs directly in your command function:

import click

@click.group()
def myprogram():
    pass

@myprogram.command()
@click.option(
    '--parameter', '-p',
    multiple=True,
    help='Specify key-value parameters (e.g., param1=value1)'
)
def mycommand(parameter):
    params = {}
    for item in parameter:
        try:
            key, val = item.split('=', 1)
            params[key.strip()] = val.strip()
        except ValueError:
            click.echo(f"Error: '{item}' is not a valid key=value pair", err=True)
            raise click.Abort()
    
    click.echo(f"Parsed parameters: {params}")

This is simpler for one-off use cases, but the custom ParamType approach is better for reusability and cleaner code separation.

Testing it out

Run either version with your desired command format:

myprogram mycommand --parameter param1=value1 --parameter param2=value2

You'll get output like:

Parsed parameters: {'param1': 'value1', 'param2': 'value2'}

内容的提问来源于stack exchange,提问作者Naigel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:27:39