我有一个 python 脚本,我试图从 bash/shell/cli 调用它。我之前使用过一个名为“Click”的模块;但是,我认为不可能既使用“单击”传入参数又传入非单击参数(例如字典或列表)。有没有办法仍然可以传递非点击参数? (以下是我尝试实现此目标的尝试:)
response_dict = json.loads(response.text)
@click.command()
@click.argument("md5hash_passed")
def find_pr_id (response, md5hash_passed):
values = response["values"]
for item in values:
item_id = item["id"]
fromref = item["fromRef"]
md5_hash = fromref["latestCommit"]
branch_name = fromref["displayId"]
if md5_hash == md5hash_passed:
return item_id
find_pr_id(response_dict)
提前致谢。
您不直接运行 find_pr_id,请尝试以下操作:
import click
@click.group()
@click.pass_context
def cli(ctx):
ctx.obj = json.loads(response.text)
@cli.command()
@click.pass_obj
@click.argument("md5hash_passed")
def find_pr_id (request, md5hash_passed):
values = request["values"]
for item in values:
item_id = item["id"]
fromref = item["fromRef"]
md5_hash = fromref["latestCommit"]
branch_name = fromref["displayId"]
if md5_hash == md5hash_passed:
return item_id
if __name__ == "__main__":
cli()
运行:
python test.py find-pr-id 123