Skip to content

Commit 4d45a3b

Browse files
4383vstinner
authored andcommitted
json.tool: use stdin and stdout in default cmdlne arguments (GH-11992)
Argparse can handle default value as stdin and stdout for parameters as file type (infile, outfile).
1 parent f12ba7c commit 4d45a3b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Lib/json/tool.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ def main():
2121
'to validate and pretty-print JSON objects.')
2222
parser = argparse.ArgumentParser(prog=prog, description=description)
2323
parser.add_argument('infile', nargs='?', type=argparse.FileType(),
24-
help='a JSON file to be validated or pretty-printed')
24+
help='a JSON file to be validated or pretty-printed',
25+
default=sys.stdin)
2526
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
26-
help='write the output of infile to outfile')
27+
help='write the output of infile to outfile',
28+
default=sys.stdout)
2729
parser.add_argument('--sort-keys', action='store_true', default=False,
2830
help='sort the output of dictionaries alphabetically by key')
2931
parser.add_argument('--json-lines', action='store_true', default=False,
3032
help='parse input using the jsonlines format')
3133
options = parser.parse_args()
3234

33-
infile = options.infile or sys.stdin
34-
outfile = options.outfile or sys.stdout
35+
infile = options.infile
36+
outfile = options.outfile
3537
sort_keys = options.sort_keys
3638
json_lines = options.json_lines
3739
with infile, outfile:

0 commit comments

Comments
 (0)