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

Tree @master (Download .tar.gz)

json-dict @master

6c3731f
b8b856c
 
 
 
 
 
4878f33
b8b856c
 
c691522
b8b856c
 
05e7823
b8b856c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#!/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")