ParseMainArguments.cc 3.78 KB
/**
 * ParseMainArguments.cc
 *
 *
 *  Created on: 12 oct. 2012
 *      Author: AKKA IS
 */

#include <getopt.h>

#include <iostream>
#include <string>

#include "DicError.hh"
#include "ParseMainArguments.hh"
#include "AMDA-Kernel_Config.hh"

using namespace std;
using namespace log4cxx;

namespace AMDA {
namespace MainRequest {

LoggerPtr ParseMainArguments::logger(
		Logger::getLogger("AMDA-Kernel.ParseMainArguments"));

ParseMainArguments::ParseMainArguments(int argc, char *argv[])
try :
		_requesteFile(""),
		_workPath("."),
		_stopProcess(false) {
	int ret = parse(argc, argv);
	if (ret != AMDA_EXIT_OK) {
		BOOST_THROW_EXCEPTION( parseArg_error() << errno_code(ret));
	}

}
catch (...) {
	throw;
}

int ParseMainArguments::parse(int argc, char *argv[]) {

	if (logger->isInfoEnabled()) {
		string commandeLine = "";
		for (int index = 0; index < argc; ++index) {
			commandeLine += string(argv[index]) + " ";
		}
		LOG4CXX_INFO(logger, "command line :" << commandeLine.c_str());
	}

#define no_argument 0
#define required_argument 1
#define optional_argument 2

	/*static int  no_argument = 0;
	 const int  required_argument = 1;
	 const int  optional_argument = 2;*/

	const struct option longopts[] = {
			//const char *name, int has_arg, 		int *flag, int val;
			{ "version", no_argument, 0, 'v' },
			{ "help", no_argument, 0, 'h' },
			{ "work-path", required_argument, 0, 'w' },
			{ 0, 0, 0, 0 }, };

	int index = 0;
	int iarg = 0;

	//turn off getopt error message
	opterr = 1;

	if (argc == 1) {
		LOG4CXX_ERROR(logger, "Error: " << " No argument gived");
		std::cerr << "Error: " << " No argument gived" << endl;
		printHelp(argv[0]);
		return AMDA_PARSE_COMMAND_LINE_KO;
	}

	while (iarg != -1) {

		iarg = getopt_long(argc, argv, "vhw:", longopts, &index);
		switch (iarg) {
		//no more argument

		case -1 :
			break;

			//version numbre
		case 'v':

			LOG4CXX_INFO(logger, "Ask version number :" << AMDA_Kernel_VERSION);
			cout << argv[0] << " version " << AMDA_Kernel_VERSION << endl;
			_stopProcess = true;
			return AMDA_EXIT_OK;
			break;

			//help message
		case 'h':
			LOG4CXX_INFO(logger, "Ask version help");
			printHelp(argv[0]);
			_stopProcess = true;
			return AMDA_EXIT_OK;
			break;

		case 'w':
			//_workPath
			_workPath = optarg;
			LOG4CXX_INFO(logger, "work path :" << _workPath);
			break;
		case '?':
			LOG4CXX_ERROR(logger,
					"Error: " << (char) iarg << " unknown options!");
			std::cerr << "Error: " << (char) iarg << " unknown options!"
					<< endl;
			printHelp(argv[0]);
			_stopProcess = true;
			return AMDA_PARSE_COMMAND_LINE_KO;
			break;

		default:
			LOG4CXX_ERROR(logger,
					"Error: " << "?? getopt returned character code " << iarg << " ??");
			std::cerr << "?? getopt returned character code " << iarg << " ??"
					<< endl;
			_stopProcess = true;
			return AMDA_PARSE_COMMAND_LINE_KO;
			break;
		}
	}
	if (optind < argc) {
			_requesteFile = string(argv[optind]);
			++optind;
	           }
	//test option mandatory
	if (optind < argc || _requesteFile == "") {

		LOG4CXX_ERROR(logger,
				"Error: " << "one requesteFile are mandatory ");
		std::cerr
				<< "one requesteFile are mandatory "
				<< endl;
		printHelp(argv[0]);
		_stopProcess = true;
		return AMDA_PARSE_COMMAND_LINE_KO;
	}

	return AMDA_EXIT_OK;
}

/**
 * help message
 */
void ParseMainArguments::printHelp(const char *name) {
	cout << "usage: " << name
			<< "  [(--help|-h)(--version|-v)] | [(--work-path|-w) path] requesteFile"
			<< endl;
	cout << "--help|-h: this message" << endl;
	cout << "--version|-v: print version number" << endl;
	cout
			<< "--work-path|-w path: work path where file output is generated \".\" by default"
			<< endl;
	cout << "requesteFile: xml request file" << endl;
	cout << endl << "AMDA_Kernel version " << AMDA_Kernel_VERSION << endl;

}

} /* namespace Main */
} /* namespace AMDA */