Skip to content

Commit 78c0c66

Browse files
committed
Pack with pyinstaller.
1 parent 40e481c commit 78c0c66

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/input.json
22
.idea
3+
/dist/
4+
/build/
5+
/main.spec

main.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
import argparse
3+
import sys
24

35

46
def format_php_array(data, indent=0):
@@ -38,9 +40,31 @@ def format_php_value(value, indent):
3840
return str(value)
3941

4042

43+
def parse_arguments():
44+
parser = argparse.ArgumentParser(description="Convert JSON to PHP array.")
45+
parser.add_argument("input_file", nargs='?', default="./input.json",
46+
help="Path to the input JSON file. Default is './input.json'")
47+
parser.add_argument("output_file", nargs='?', default=None,
48+
help="Path to the output PHP file. If not provided, output will be printed to console.")
49+
50+
return parser.parse_args()
51+
52+
4153
if __name__ == "__main__":
42-
json_data = json.load(open("./input.json", "r"))
54+
args = parse_arguments()
55+
56+
try:
57+
with open(args.input_file, "r") as file:
58+
json_data = json.load(file)
59+
except FileNotFoundError:
60+
sys.exit(f"Error: The file '{args.input_file}' does not exist.")
61+
except json.JSONDecodeError:
62+
sys.exit(f"Error: The file '{args.input_file}' contains invalid JSON.")
4363

44-
# 格式化PHP数组
4564
formatted_php_array = format_php_array(json_data)
46-
print("<?php\n$myArray = " + formatted_php_array + ";\n?>")
65+
66+
if args.output_file:
67+
with open(args.output_file, "w") as output_file:
68+
output_file.write("<?php\n$myArray = " + formatted_php_array + ";\n?>")
69+
else:
70+
print("<?php\n$myArray = " + formatted_php_array + ";\n?>")

0 commit comments

Comments
 (0)