2

I have a python script that I am trying to invoke from bash/shell/cli. I have used a module called 'Click' before; however, I don't think it's possible to both pass in parameters using 'Click' AND passing in non-click parameters (such as a dictionary or list). Is there a way I can still pass in the non-click parameter? (Below is my attempt at trying to accomplish this:)

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)

Thanks in advance.

1 Answer 1

1

You don't run find_pr_id directly, try this:

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()

run with :

python test.py find-pr-id 123
Sign up to request clarification or add additional context in comments.

3 Comments

(BTW): find-pr-id is considered a "object" right?
ctx.obj is the object, I renamed to request in function find_pr_id.
Oh I know :) I was just wondering how to properly reference it. (Thanks again)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.