Python单击传递未指定数量的kwarg

bea*_*gle 5 python dictionary kwargs python-click

最近发现的click,我想将不确定数量的kwargs传递给click命令。目前,这是我的命令:

@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)
Run Code Online (Sandbox Code Playgroud)

但是,当使用nargs时,将多于1的任何值作为元组([docs] [1])传递,但是我不能那样type=dict做。

但是应该可以这样做:

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

在此先感谢您提供的任何帮助或建议,与此同时,我将自己继续努力。

bea*_*gle 7

使用@rmn 提供的链接,我按如下方式重写了我的点击命令:

@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)
Run Code Online (Sandbox Code Playgroud)

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

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