Python 点击传递未指定数量的 kwargs

Python click pass unspecified number of kwargs

最近发现点击,我想将未指定数量的 kwargs 传递给点击命令。目前这是我的命令:

@click.command()
@click.argument('tgt')
@click.argument('fun')
@click.argument('args', nargs=-1)
def runner(tgt, fun, args):
    req = pyaml.p(meh.PostAdapter(tgt, fun, *args))
    click.echo(req)

然而,当使用 nargs 时,超过 1 的任何东西都会作为元组传递 ([docs][1]),不幸的是我不能 type=dict

但是应该可以这样做:

command positional1 positional2 foo='bar' baz='qux' xxx='yyy'

在此先感谢您的任何帮助或建议,与此同时,我会自己不断改进。

使用@rmn提供的link,我重写了我的点击命令如下:

@click.command(context_settings=dict(
    ignore_unknown_options=True,
    allow_extra_args=True,
))
@click.pass_context
def runner(ctx, tgt, fun):
    d = dict()
    for item in ctx.args:
        d.update([item.split('=')])
    req = pyaml.p(meh.PostAdapter(tgt, fun, d))
    click.echo(req)

这让我可以正确发出以下命令:

mycmd tgt fun foo='bar' baz='qux' xxx='yyy'