From 8370e69549820b4e2994971c4788b2cb1665c962 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Mon, 30 Aug 2021 00:00:49 +0530 Subject: [PATCH] Add stdin support to rich.json --- rich/json.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rich/json.py b/rich/json.py index f03be88a..61911edb 100644 --- a/rich/json.py +++ b/rich/json.py @@ -54,6 +54,7 @@ if __name__ == "__main__": "path", metavar="PATH", help="path to file, or - for stdin", + nargs='?', ) parser.add_argument( "-i", @@ -69,10 +70,13 @@ if __name__ == "__main__": console = Console() error_console = Console(stderr=True) - + try: - with open(args.path, "rt") as json_file: - json_data = json_file.read() + if args.path is None: + json_data = sys.stdin.read() + else: + with open(args.path, "rt") as json_file: + json_data = json_file.read() except Exception as error: error_console.print(f"Unable to read {args.path!r}; {error}") sys.exit(-1)