16
16
import sys
17
17
18
18
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
-
31
19
def main ():
32
20
prog = 'python -m json.tool'
33
21
description = ('A simple command line interface for json module '
@@ -39,10 +27,11 @@ def main():
39
27
help = 'write the output of infile to outfile' )
40
28
parser .add_argument ('--no-ensure-ascii' , action = 'store_true' , default = False ,
41
29
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.' )
46
35
parser .add_argument ('--sort-keys' , action = 'store_true' , default = False ,
47
36
help = 'sort the output of dictionaries alphabetically by key' )
48
37
options = parser .parse_args ()
@@ -63,7 +52,7 @@ def main():
63
52
outfile = options .outfile or sys .stdout
64
53
with outfile :
65
54
json .dump (obj , outfile ,
66
- indent = options .indent ,
55
+ indent = None if options . no_indent else options .indent ,
67
56
ensure_ascii = not options .no_ensure_ascii ,
68
57
sort_keys = options .sort_keys ,
69
58
)
0 commit comments