gdritter repos lib-static / master bin / json-dict
master

Tree @master (Download .tar.gz)

json-dict @masterraw · history · blame

#!/usr/bin/python2

import json
import sys
import yaml

def coerce(arg):
  if not arg: return None
  if arg[0] == '@':
    try:
      with open(arg[1:]) as f: return coerce(f.read())
    except: pass
  if arg == '-': return coerce(sys.stdin.read())
  try:    return yaml.safe_load(arg)
  except: pass
  try:    return json.loads(arg)
  except: pass
  return arg

if len(sys.argv[1:]) % 2 == 1:
  sys.stderr.write('error: non-even number of arguments to {0}\n'.format(sys.argv[0]))
  sys.exit(1)
elif len(sys.argv[1:]) == 0:
  sys.stdout.write("{}\n")
  sys.exit(1)
else:
  d = dict(zip(sys.argv[1::2], map(coerce, sys.argv[2::2])))
  sys.stdout.write(json.dumps(d))
  sys.stdout.write("\n")