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