Skip to content

Commit 97152e9

Browse files
committed
Add --no-indent argument
1 parent a3e2b23 commit 97152e9

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

Lib/json/tool.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@
1616
import sys
1717

1818

19-
def parse_indent(indent):
20-
"""Parse the argparse indent argument."""
21-
if indent == 'None':
22-
return None
23-
if indent == r'\t':
24-
return '\t'
25-
try:
26-
return int(indent)
27-
except ValueError:
28-
return indent
29-
30-
3119
def main():
3220
prog = 'python -m json.tool'
3321
description = ('A simple command line interface for json module '
@@ -39,10 +27,11 @@ def main():
3927
help='write the output of infile to outfile')
4028
parser.add_argument('--no-ensure-ascii', action='store_true', default=False,
4129
help='Do not set ensure_ascii to escape non-ASCII characters')
42-
parser.add_argument('--indent', default='4', type=parse_indent,
43-
help='Indent level or str for pretty-printing. '
44-
'Use None for the most compact representation. '
45-
r'Use "\t" for tab indentation.')
30+
group = parser.add_mutually_exclusive_group()
31+
group.add_argument('--indent', default=4, type=int,
32+
help='Indent level for pretty-printing.')
33+
group.add_argument('--no-indent', action='store_true', default=False,
34+
help='Use compact mode.')
4635
parser.add_argument('--sort-keys', action='store_true', default=False,
4736
help='sort the output of dictionaries alphabetically by key')
4837
options = parser.parse_args()
@@ -63,7 +52,7 @@ def main():
6352
outfile = options.outfile or sys.stdout
6453
with outfile:
6554
json.dump(obj, outfile,
66-
indent=options.indent,
55+
indent=None if options.no_indent else options.indent,
6756
ensure_ascii=not options.no_ensure_ascii,
6857
sort_keys=options.sort_keys,
6958
)

0 commit comments

Comments
 (0)