#! /usr/bin/env python3

import sys

from parsegen import parse, output, errors, options

def main(args):

	args = options.parse(args)

	# Make sure we have a file to read. Make sure the error message matches the
	# rest of the program
	if not args.input_file:
		print(errors.ParsegenError("argument error", "no input file specified"))
		sys.exit(1)

	try:
		# These two lines do all the heavy lifting
		grammar = parse.parse_file(args.input_file)
		output.write_grammar(grammar, file=args.output_file,
							 options=args.options, language=args.language)

		# Close the files when we are done with them
		if args.output_file != sys.stdout:
			args.output_file.close()
			args.input_file.close()
	except errors.ParsegenError as e:
		print(e)
		sys.exit(1)

if __name__ == '__main__':
	sys.exit(main(sys.argv[1:]))
