#!/usr/bin/env python3
# Eryn Wells <eryn@erynwells.me>

import argparse
import json
import os.path
import sys

FORMATS = {
    'bin': 2,
    'oct': 8,
    'dec': 10,
    'hex': 16,
}

def convert_number_to_all_bases(n):
    try:
        n = n.lower()
        if n.startswith('0x'):
            base = 16
        elif n.startswith('0o'):
            base = 8
        elif n.startswith('0b'):
            base = 2
        else:
            base = 10
        num = int(n, base=base)
    except ValueError:
        print(f'Error: cannot parse string as int: {n}', file=sys.stderr)
    else:
        return {
            'bin': str(bin(num)[2:]),
            'oct': str(oct(num)[2:]),
            'dec': str(num),
            'hex': str(hex(num)[2:]).upper(),
        }

def parse_args(argv, *a, **kw):
    parser = argparse.ArgumentParser(*a, **kw)
    parser.add_argument('--ascii', '-a', action='store_true')
    parser.add_argument('numbers', nargs='+')
    args = parser.parse_args(argv)
    return args

def main(argv):
    args = parse_args(argv[1:], prog=argv[0])

    output = {}

    for n in args.numbers:
        if args.ascii:
            for c in n:
                output.setdefault(c, convert_number_to_all_bases(str(ord(c))))
        else:
            output.setdefault(n, convert_number_to_all_bases(n))

    json.dump(output, sys.stdout, indent=2)
    print('\n')

    return 0

if __name__ == '__main__':
    import sys
    result = main(sys.argv)
    sys.exit(0 if not result else result)
