Blame view

src/Main/ParseMainArguments.cc 6.96 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 * 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 */