mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
improve the command-line interface of json.tool (closes #21000)
A patch from Berker Peksag.
This commit is contained in:
parent
a191b91a43
commit
940e207412
4 changed files with 76 additions and 12 deletions
|
@ -10,21 +10,24 @@ Usage::
|
|||
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
|
||||
|
||||
"""
|
||||
import sys
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) == 1:
|
||||
infile = sys.stdin
|
||||
outfile = sys.stdout
|
||||
elif len(sys.argv) == 2:
|
||||
infile = open(sys.argv[1], 'r')
|
||||
outfile = sys.stdout
|
||||
elif len(sys.argv) == 3:
|
||||
infile = open(sys.argv[1], 'r')
|
||||
outfile = open(sys.argv[2], 'w')
|
||||
else:
|
||||
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
|
||||
prog = 'python -m json.tool'
|
||||
description = ('A simple command line interface for json module '
|
||||
'to validate and pretty-print JSON objects.')
|
||||
parser = argparse.ArgumentParser(prog=prog, description=description)
|
||||
parser.add_argument('infile', nargs='?', type=argparse.FileType(),
|
||||
help='a JSON file to be validated or pretty-printed')
|
||||
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
|
||||
help='write the output of infile to outfile')
|
||||
options = parser.parse_args()
|
||||
|
||||
infile = options.infile or sys.stdin
|
||||
outfile = options.outfile or sys.stdout
|
||||
with infile:
|
||||
try:
|
||||
obj = json.load(infile)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue