Skip to content

Commit 113393b

Browse files
committed
Implement --in-place for compact-json#36
1 parent b8998bf commit 113393b

6 files changed

Lines changed: 61 additions & 28 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The [FracturedJson Wiki](https://github.com/j-brooke/FracturedJson/wiki) provide
2626
The package installs a command-line script `fractured-json` which can compact one or more JSON files according to command-line switches.
2727

2828
``` text
29-
usage: fractured-json [-h] [-V] [--output OUTPUT]
29+
usage: fractured-json [-h] [-V] [--in-place] [--output OUTPUT]
3030
[--allow-trailing-commas] [--always-expand-depth N]
3131
[--colon-before-prop-name-padding] [--colon-padding]
3232
[--comma-padding] [--comment-padding]
@@ -55,8 +55,9 @@ positional arguments:
5555
json JSON file(s) to parse (or stdin with "-")
5656
5757
options:
58-
-h, --help show this help message and exit
59-
-V, --version
58+
-h, --help Show this help message and exit
59+
-V, --version Output the script version and exit
60+
--in-place Save any edits back to the input file
6061
--output, -o OUTPUT The output file name(s). The number of output file
6162
names must match the number of input files.
6263
--allow-trailing-commas

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ license = "MIT"
1616
name = "fractured-json"
1717
readme = "README.md"
1818
requires-python = ">=3.11, <3.14"
19-
version = "5.0.0post3"
19+
version = "5.0.0post4"
2020

2121
[[project.authors]]
2222
email = "python@figsandfudge.com"

src/fractured_json/_fractured_json.py

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,35 @@
1111
def 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

116148
if __name__ == "__main__": # pragma: no cover

src/fractured_json/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Auto-generated during build. Do not edit.
22

3-
__version__ = "5.0.0post3"
3+
__version__ = "5.0.0post4"

tests/test_console_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_main(script_runner):
108108
ret = script_runner.run(["python3", "-m", "fractured_json", "--help"])
109109
assert ret.stderr == ""
110110
assert ret.success
111-
assert "[-h] [-V] [--output" in ret.stdout
111+
assert "[-h] [-V] [--in-place] [--output OUTPUT]" in ret.stdout
112112

113113

114114
@pytest.mark.script_launch_mode("subprocess")

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)