1

I'm using click to build a CLI tool. I need my code to open a file in a specified directory (this is what is passed as a command line argument) and parse the json inside that file. What is the best way of doing this?

1

1 Answer 1

1

You can check the documentation for more information, but to give you a warm start:

import json

import click


@click.command()
@click.argument("json_file", type=click.File("rb"))
def cli(json_file):
    # click already handles opening up the file
    # and passes `input_path` as a stream of bytes
    # since we specified `"rb"`
    _json_file = json.load(json_file)
    print(_json_file)


if __name__ == "__main__":
    cli()
Sign up to request clarification or add additional context in comments.

Comments

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.