-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-29636: improve CLI of json.tool
#9765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,10 @@ def main(): | |
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') | ||
parser.add_argument('--indent', default=4, | ||
help='the indentation for pretty-print (int or str)') | ||
parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false', | ||
help='disable escaping of non-ASCII characters') | ||
parser.add_argument('--sort-keys', action='store_true', default=False, | ||
help='sort the output of dictionaries alphabetically by key') | ||
parser.add_argument('--json-lines', action='store_true', default=False, | ||
|
@@ -33,6 +37,11 @@ def main(): | |
infile = options.infile or sys.stdin | ||
outfile = options.outfile or sys.stdout | ||
sort_keys = options.sort_keys | ||
try: | ||
indent = int(options.indent) | ||
except ValueError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you use type in argparse you can remove this section There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, then it will be impossible to pass tab character through to json dumps from the command-line, for example by using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ack! |
||
# it is also possible to specify a string here | ||
indent = options.indent | ||
json_lines = options.json_lines | ||
with infile, outfile: | ||
try: | ||
|
@@ -41,7 +50,8 @@ def main(): | |
else: | ||
objs = (json.load(infile), ) | ||
for obj in objs: | ||
json.dump(obj, outfile, sort_keys=sort_keys, indent=4) | ||
json.dump(obj, outfile, sort_keys=sort_keys, indent=indent, | ||
ensure_ascii=options.ensure_ascii) | ||
outfile.write('\n') | ||
except ValueError as e: | ||
raise SystemExit(e) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Added ability to pass through `indent` and `ensure_ascii` options in | ||
`json.tool` command-line interface. |
Uh oh!
There was an error while loading. Please reload this page.