|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | import dashio
|
3 | 3 | import json
|
4 |
| -import sys |
5 | 4 | import os
|
| 5 | +import argparse |
6 | 6 |
|
7 |
| -c64_filename = sys.argv[1] |
8 |
| -json_filename = os.path.splitext(c64_filename)[0] + ".json" |
9 |
| -try: |
10 |
| - with open(c64_filename, mode="r") as fp: |
11 |
| - cfg64 = fp.read() |
12 | 7 |
|
13 |
| -except FileNotFoundError: |
14 |
| - print("File not found") |
| 8 | +def parse_commandline_arguments(): |
| 9 | + parser = argparse.ArgumentParser() |
| 10 | + parser.add_argument('file', help="Input file name.") |
| 11 | + parser.add_argument("-p", "--print", dest="print", action='store_true', help="Print output.") |
| 12 | + parser.add_argument("-o", "--out", dest="out_file", default="", help="output filename.") |
| 13 | + parser.add_argument("-i", "--indent", dest="indent", type=int, default=4, help="Indent depth (Default 4).") |
15 | 14 |
|
16 |
| -# Remove formatting for different languages |
17 |
| -cfg64 = cfg64.translate({ord(i): None for i in '"\n \\;'}) |
| 15 | + args = parser.parse_args() |
| 16 | + return args |
18 | 17 |
|
19 |
| -print(cfg64) |
20 |
| -config_dict = dashio.decode_cfg64(cfg64) |
21 |
| -cfg_json = json.dumps(config_dict, indent=4) |
22 |
| -print(cfg_json) |
| 18 | +def main(): |
| 19 | + args = parse_commandline_arguments() |
23 | 20 |
|
24 |
| -json_file = open(json_filename, "w") |
25 |
| -json_file.write(cfg_json) |
26 |
| -json_file.close() |
| 21 | + c64_filename = args.file |
| 22 | + json_filename = os.path.splitext(c64_filename)[0] + ".json" |
| 23 | + |
| 24 | + if args.out_file: |
| 25 | + json_filename = args.out_file |
| 26 | + try: |
| 27 | + with open(c64_filename, mode="r") as fp: |
| 28 | + cfg64 = fp.read() |
| 29 | + except FileNotFoundError: |
| 30 | + print("File not found") |
| 31 | + |
| 32 | + # Remove formatting for different languages |
| 33 | + cfg64 = cfg64.translate({ord(i): None for i in '"\n \\;'}) |
| 34 | + |
| 35 | + config_dict = dashio.decode_cfg64(cfg64) |
| 36 | + cfg_json = json.dumps(config_dict, indent=args.indent) |
| 37 | + |
| 38 | + if args.print: |
| 39 | + print(cfg_json) |
| 40 | + |
| 41 | + json_file = open(json_filename, "w") |
| 42 | + json_file.write(cfg_json) |
| 43 | + json_file.close() |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + main() |
0 commit comments