Python单击传递未指定数量的KWARG
最近发现的click,我想将未指定数量的Kwarg传递给click命令。目前这是我的命令:Python单击传递未指定数量的KWARG,python,dictionary,keyword-argument,python-click,Python,Dictionary,Keyword Argument,Python Click,最近发现的click,我想将未指定数量的Kwarg传递给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) 然而,当使用
@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
这样做
但这样做应该是可能的:
命令position1position2foo='bar'baz='qux'xxx='yyy'
提前感谢您提供的任何帮助或建议,在此期间,我将自己继续对此进行修改。使用@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)
这允许我正确地发出以下命令:
mycmd tgt fun foo='bar'baz='qux'xxx='yyy'
也许这会有所帮助:我会在这里使用列表理解:d=dict([item.strip('--').split('='),用于ctx.args中的项)