Blame view

src/amdaXMLRequestorTool/ParseMainArguments.cc 3.78 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
/**
 * 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 */