#!/usr/bin/env python

import sys
import argparse
from romanum.roman2num import r2n
from romanum.num2roman import n2r

if __name__ == '__main__':

    # First: Parse cli options.
    # -i and -o for input/output files, if not
    # for -s try parsing argument as number/roman and covert to roman/number to stdout
    # if not, parse from stdin -> stdout until EOF. Assume one number per line.

    parser = argparse.ArgumentParser(description='Parse options to decide what to convert from where.')

    parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin,
                        help='Input file. Defaults to stdin.')
    parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), default=sys.stdout,
                        help='Output file. Defaults to stdout.')
    parser.add_argument('-f', '--faster', action='store_true',
                        help="""Skip some validation of input and results. May cause parsing of 
                        nonstandard forms to fail or silently return erroneous results.""")
    parser.add_argument('-s', '--roman_string', nargs=argparse.REMAINDER, type=str)

    argtuple = parser.parse_known_args()
    args = argtuple[0]
    stringargs = argtuple[1]  # All unknown args must be strings after the -s ?

    if args.faster:
        if args.roman_string is not None:  # -s overrides infile & outfile.
            for s in args.roman_string:
                print r2n(s),
            print
        else:  # Read from specified files or defaults.
            for line in args.infile:
                for word in line.split():  # Assume space-separated numerals
                    print >> args.outfile, r2n(word),
                print

            args.infile.close()
            args.outfile.close()

    else:  # Do proper input checking
        if args.roman_string is not None:  # Parse string given as argument.
            raise NotImplementedError
        else:  # Use files or stdin/stdout.
            raise NotImplementedError
