Created
March 3, 2016 22:05
-
-
Save jasonish/d8a50692944b3df62958 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| from __future__ import print_function | |
| import sys | |
| import pprint | |
| import argparse | |
| import json | |
| import yaml | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("-f", "--format", | |
| metavar="<pretty|json|yaml>", | |
| default="pretty", | |
| help="Output format: pretty,json,yaml") | |
| parser.add_argument("input", help="YAML file to read.") | |
| args = parser.parse_args() | |
| try: | |
| content = yaml.load(open(args.input)) | |
| except Exception as err: | |
| print("ERROR:") | |
| print(err) | |
| return | |
| if args.format.lower() == "yaml": | |
| print(yaml.dump(content, default_flow_style=False)) | |
| elif args.format.lower() == "json": | |
| print(json.dumps(content, indent=4)) | |
| else: | |
| # Just a Python pretty print. | |
| pprint.PrettyPrinter().pprint(content) | |
| if __name__ == "__main__": | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment