1111def command_line_parser () -> argparse .ArgumentParser :
1212 parser = argparse .ArgumentParser (
1313 description = "Format JSON into compact, human readable form" ,
14+ add_help = False ,
1415 )
15- parser .add_argument ("-V" , "--version" , action = "store_true" )
1616
17+ parser .add_argument (
18+ "-h" ,
19+ "--help" ,
20+ default = False ,
21+ action = "store_true" ,
22+ help = "Show this help message and exit" ,
23+ )
24+ parser .add_argument (
25+ "-?" ,
26+ dest = "dos_help" ,
27+ default = False ,
28+ action = "store_true" ,
29+ help = argparse .SUPPRESS ,
30+ )
31+ parser .add_argument (
32+ "-V" ,
33+ "--version" ,
34+ action = "store_true" ,
35+ help = "Output the script version and exit" ,
36+ )
37+ parser .add_argument (
38+ "--in-place" ,
39+ action = "store_true" ,
40+ default = False ,
41+ help = "Save any edits back to the input file" ,
42+ )
1743 parser .add_argument (
1844 "--output" ,
1945 "-o" ,
@@ -59,7 +85,6 @@ def command_line_parser() -> argparse.ArgumentParser:
5985 parser .add_argument (
6086 "json" ,
6187 nargs = "*" ,
62- type = argparse .FileType ("r" ),
6388 help = 'JSON file(s) to parse (or stdin with "-")' ,
6489 )
6590 parser .add_argument (
@@ -68,7 +93,6 @@ def command_line_parser() -> argparse.ArgumentParser:
6893 action = "store_true" ,
6994 help = "Treat strings as unicode East Asian characters" ,
7095 )
71- parser .add_argument ("-?" , dest = "dos_help" , action = "store_true" , help = argparse .SUPPRESS )
7296
7397 return parser
7498
@@ -83,7 +107,7 @@ def die(message: str) -> None:
83107 args = parser .parse_args ()
84108 if args .version :
85109 print (fractured_json_version )
86- elif len (args .json ) == 0 or args .dos_help :
110+ elif len (args .json ) == 0 or args .help or args . dos_help :
87111 parser .print_help ()
88112 else :
89113 options = FracturedJsonOptions ()
@@ -94,23 +118,31 @@ def die(message: str) -> None:
94118 formatter .string_length_func = lambda s : wcswidth (s )
95119
96120 in_files = args .json
97- out_files = args .output
98-
99- if out_files is None :
100- for fh in args .json :
101- json_input = fh .read ()
102- output_json = formatter .reformat (json_input )
103- print (output_json , end = "" )
104- return
105-
106- if len (in_files ) != len (out_files ):
107- die ("the numbers of input and output file names do not match" )
108-
109- for fh_in , fn_out in zip (args .json , args .output , strict = True ):
110- json_input = fh_in .read ()
111- output_json = formatter .reformat (json_input )
112- with open (fn_out , "w" , newline = "" ) as fh_out :
113- fh_out .write (output_json )
121+
122+ if args .in_place :
123+ out_files = args .json
124+ elif args .output is None :
125+ out_files = [None ] * len (in_files )
126+ else :
127+ if len (in_files ) != len (args .output ):
128+ die ("the numbers of input and output file names do not match" )
129+ out_files = args .output
130+
131+ for fn_in , fn_out in zip (in_files , out_files , strict = True ):
132+ if fn_in == "-" :
133+ in_json_string = sys .stdin .read ()
134+ else :
135+ try :
136+ in_json_string = open (fn_in ).read ()
137+ except FileNotFoundError as e :
138+ die (str (e ))
139+
140+ out_json_string = formatter .reformat (in_json_string )
141+
142+ if fn_out is None :
143+ sys .stdout .write (out_json_string )
144+ elif not args .in_place or in_json_string != out_json_string :
145+ open (fn_out , "w" ).write (out_json_string )
114146
115147
116148if __name__ == "__main__" : # pragma: no cover
0 commit comments