TL; DR
parser.add_argument(
'-f', '--file',
help='JSON input file',
type=argparse.FileType('r'),
)
Description
Simple command line script to reformat JSON files
reformat-json \
-f package.json \
--indent=2 \
--sort-keys \
--output=sorted_package.json
can be code in Python as follows
#!/usr/bin/env python3
import argparse, json, sys
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-f', '--file',
help='JSON input file',
type=argparse.FileType('r'),
)
parser.add_argument(
'-i', '--indent',
help='Non-negative integer indent level',
type=int
)
parser.add_argument(
'-o', '--output',
help='Write JSON into output file',
type=argparse.FileType('w'),
)
parser.add_argument(
'-s', '--sort-keys',
action='store_true',
help='Sort output JSON by keys',
)
args = parser.parse_args()
if not args.file:
parser.print_usage()
return sys.exit(EXIT_FAILURE)
gson = json.dumps(
json.load(args.file),
indent=args.indent,
sort_keys=args.sort_keys
)
args.file.close()
if args.output:
args.output.write(gson)
args.output.write('\n')
args.output.close()
else:
print(gson)
return sys.exit(EXIT_SUCCESS)
if __name__ == '__main__':
main()
type = fncworks iffncis a function that takes a string, and returns a desired object, or raises an error. There isn't afilefunction in Python.type=opendoes work since there is a Python functionopen(filename)'. argparse provides a better function,argparse.FileType()` (actually a function constructor). But a simple string argument as inwims answer is a better starting place.type=filedoes work in Python2.7 (and earlier). But in Python3,filehas been removed. stackoverflow.com/questions/112970/…