| 56 | |
| 57 | |
| 58 | def main(argv: Sequence[str] | None = None) -> int: |
| 59 | parser = argparse.ArgumentParser() |
| 60 | parser.add_argument( |
| 61 | '--autofix', |
| 62 | action='store_true', |
| 63 | dest='autofix', |
| 64 | help='Automatically fixes encountered not-pretty-formatted files', |
| 65 | ) |
| 66 | parser.add_argument( |
| 67 | '--indent', |
| 68 | type=parse_num_to_int, |
| 69 | default='2', |
| 70 | help=( |
| 71 | 'The number of indent spaces or a string to be used as delimiter' |
| 72 | ' for indentation level e.g. 4 or "\t" (Default: 2)' |
| 73 | ), |
| 74 | ) |
| 75 | parser.add_argument( |
| 76 | '--no-ensure-ascii', |
| 77 | action='store_true', |
| 78 | dest='no_ensure_ascii', |
| 79 | default=False, |
| 80 | help=( |
| 81 | 'Do NOT convert non-ASCII characters to Unicode escape sequences ' |
| 82 | '(\\uXXXX)' |
| 83 | ), |
| 84 | ) |
| 85 | parser.add_argument( |
| 86 | '--no-sort-keys', |
| 87 | action='store_true', |
| 88 | dest='no_sort_keys', |
| 89 | default=False, |
| 90 | help='Keep JSON nodes in the same order', |
| 91 | ) |
| 92 | parser.add_argument( |
| 93 | '--top-keys', |
| 94 | type=parse_topkeys, |
| 95 | dest='top_keys', |
| 96 | default=[], |
| 97 | help='Ordered list of keys to keep at the top of JSON hashes', |
| 98 | ) |
| 99 | parser.add_argument('filenames', nargs='*', help='Filenames to fix') |
| 100 | args = parser.parse_args(argv) |
| 101 | |
| 102 | status = 0 |
| 103 | |
| 104 | for json_file in args.filenames: |
| 105 | with open(json_file, encoding='UTF-8') as f: |
| 106 | contents = f.read() |
| 107 | |
| 108 | try: |
| 109 | pretty_contents = _get_pretty_format( |
| 110 | contents, args.indent, ensure_ascii=not args.no_ensure_ascii, |
| 111 | sort_keys=not args.no_sort_keys, top_keys=args.top_keys, |
| 112 | ) |
| 113 | except ValueError: |
| 114 | print( |
| 115 | f'Input File {json_file} is not a valid JSON, consider using ' |