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

#include "ParseMainArguments.hh"
#include <iostream>
#include <getopt.h>
#include <string>
#include "DicError.hh"
#include "ParseMainArguments.hh"
#include "TimeUtil.hh"
#include "DD_time.hh"

#define VERSION "sprint3"
using namespace std;
using namespace log4cxx;

namespace AMDA {
namespace Main {

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

ParseMainArguments::ParseMainArguments(int argc, char *argv[])
try :
		_stopProcess(false),
		_formatOutputTime(FORMAT_OUTPUT_TIME_DD),
		_sampling(0),
		_samplingMode(""),
		_gapThreshold(0.0),
		_workPath(".") {
	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' },
			{ "param", required_argument, 0, 'p' },
			{ "startTime", required_argument, 0, 's' },
			{ "intervalTime", required_argument, 0, 'd' },
			{ "format", required_argument, 0, 'f' },
			{ "sampling", required_argument, 0, 'r' },
			{ "sampling-mode", required_argument, 0, 'm' },
			{ "gaps-threshold", required_argument, 0, 'g' },
			{ "calibration-info", required_argument, 0, 'c' },
			{ "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, "vhp:s:d:f:c:w:r:m:g:", longopts, &index);

		switch (iarg) {
		//no more argument
		case -1:
			//read argument end
			break;

			//version numbre
		case 'v':

			LOG4CXX_INFO(logger, "Ask version number :" << VERSION);
			cout << argv[0] << " version " << 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;

			//paramName
		case 'p':
			paramName = optarg;
			LOG4CXX_INFO(logger, "Give virtual instrument :" << paramName);
			break;

			//Start time
		case 's':
			startTime = optarg;
			LOG4CXX_INFO(logger, "Give startTime :"
					<< startTime
					<< "<=> "
					<< TimeUtil::DD2ISO_TimeDate(startTime));
			break;
			// Calibration Info adding
		case 'c':
			LOG4CXX_INFO(logger, "Given calibration info name : " << optarg);
			_calibrationInfoNameList.push_back(optarg);
			break;

		case 'd':
			//interval time
			intervalTime = optarg;
			LOG4CXX_INFO(logger, "Give intervalTime :" << intervalTime
					<< "<=> "
					<< TimeUtil::DD2ISO_TimeInterval((intervalTime)));
			break;

		case 'r':
			//sampling
			_sampling = atof(optarg);
			if ( _sampling == 0 ) {
				LOG4CXX_ERROR(logger,
					"Error: " << "_sampling must be different of 0");
				return AMDA_PARSE_COMMAND_LINE_KO;
			}
			if ( _samplingMode == "" ) { _samplingMode ="classic"; }
			LOG4CXX_INFO(logger, "Given sampling :" << _sampling);
			break;

		case 'g':
			//gap-threshold
			_gapThreshold = atof(optarg);
			LOG4CXX_INFO(logger, "Given gap-threshold :" << _gapThreshold);
			break;

		case 'm':
			//sampling
			_samplingMode = optarg;
			if ( _samplingMode == "classic") {
				LOG4CXX_INFO(logger, "Given sampling mode is: " << _samplingMode);
			} else {
				printHelp(argv[0]);
				_stopProcess = true;
				return AMDA_PARSE_COMMAND_LINE_KO;
			}
			break;

		case 'w':
			//_workPath
			_workPath = optarg;
			LOG4CXX_INFO(logger, "work path :" << _workPath);
			break;

		case 'f': {
			//interval time
			string format(optarg);
			if (format == "iso") {
				_formatOutputTime = FORMAT_OUTPUT_TIME_ISO;
				LOG4CXX_INFO(logger, "Output time format is iso.");
			} else if (format == "dd") {

				_formatOutputTime = FORMAT_OUTPUT_TIME_DD;
				LOG4CXX_INFO(logger, "Output time format is DD.");
			} else if (format == "double") {

				_formatOutputTime = FORMAT_OUTPUT_TIME_DOUBLE;
				LOG4CXX_INFO(logger, "Output time format is double.");
			}
			else {
				printHelp(argv[0]);
				_stopProcess = true;
				return AMDA_PARSE_COMMAND_LINE_KO;
			}
		}
			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;
		}
	}
	//test option mandatory
	if (!_stopProcess
			&& (paramName == "" || startTime == "" || intervalTime == "")) {

		LOG4CXX_ERROR(logger,
				"Error: " << "-i virtualInstrument -p paramName -s startTime and -d intervalTime are mandatory ");
		std::cerr
				<< "-i virtualInstrument -p paramName -s startTime and -d intervalTime 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)] | (--param|-p) paramName (--startTime|-s) startTime (--intervalTime|-d) intrvalTime (--format|-f) format [(--work-path|-w) path]"
			<< endl;
	cout << "--help|-h: this message" << endl;
	cout << "--version|-v: print version number" << endl;
	cout << "--param|-p paramName: parameter name name must reference a paramName.xlm file"
			<< endl;
	cout << "--startTime|-s startTime: start time in YYYYDDDHHMMSSMLS" << endl;
	cout
			<< "--intervalTime|-d intrvalTime: interval of time in YYYYDDDHHMMSSMLS"
			<< endl;
	cout
			<< "--format|-f iso/dd/double: format type of time in output file "
			<< endl;
	cout
			<< "--sampling|-r float: sampling in sec"
			<< endl;
	cout
			<< "--sampling-mode|-m classic: sampling mode only classic is implemented"
			<< endl;
	cout
			<< "--gaps-threshold|-g float: gap Threshold for sampling"
			<< endl;
	cout
			<< "--work-path|-w path: work path where file output is generated \".\" by default"
			<< endl;
	cout << endl << "AMDA_Kernel version " << VERSION << endl;

}

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