/*
	Copyright (C) 2025 Mark Tyler

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program in the file COPYING.
*/

#include "ginbench_be.h"



int CommandLine::parse (
	int			const	argc,
	char	const * const *	const	argv
	)
{
	mtKit::Arg args ( [this]( char const * const filename )
	{
		try
		{
			m_suite.run_test_unit ( filename );
		}
		catch (...)
		{
			std::cerr << "ERROR! - Unit test failure.\n";
			return 1;
		}

		return 0;
	});

	auto print_version = []()
	{
		printf ( BIN_NAME " - Part of %s\n\n", VERSION );
		return 1;		// Stop parsing
	};

	auto print_help = [print_version]()
	{
		print_version ();
		printf ("For further information consult the man page "
			"%s(1) or the mtCellEdit Handbook.\n"
			"\n"
			, BIN_NAME );
		return 1;		// Stop parsing
	};

	int		stop = 0;
	char	const	* txt;

	args.add ( "-help",	stop, 1, print_help );
	args.add ( "-version",	stop, 1, print_version );
	args.add ( "dir",	m_output_dir );
	args.add ( "list",	[this](){ m_suite.list_units(); return 1; } );
	args.add ( "o",		m_output_sheet );
	args.add ( "patterns",	txt, [this, &txt]()
		{
			return m_suite.load_patterns ( txt );
		});
	args.add ( "shapes",	txt, [this, &txt]()
		{
			return m_suite.load_shapes ( txt );
		});
	args.add ( "total",	m_test_total, [this]()
		{
			if ( m_test_total < TEST_TOTAL_MIN ||
				m_test_total > TEST_TOTAL_MAX )
			{
				std::cerr << "Invalid test total = "
					<< m_test_total << "\n";
				return 1;		// Quit program
			}
			return 0;
		});
	args.add ( "v",		m_verbose, 1 );

	if ( args.parse ( argc, argv ) || stop )
	{
		return 1;		// Quit program
	}

	return 0;			// Continue program
}

